返回顶部

收藏

使用文件作为数据库

更多

简单的例子,你如何可以list, add, remove,search,基于文件。教你一点如何使用grep function 和 file I/O。

#!/bin/sh
# Access AddressBook file and use it as a database

# Variables
name=$1
number=$2
db=AddressBook

### AddressBook File Example ###
# Maria Daher   (27)5564-4392  #
# Nathan Campos (27)3325-9732  #
# Thiago B. (32)5302-4892  #
# Lucas Pinher  (23)0982-2389  #
# Eduardo V.    (75)9234-8234  #
################################

# Listing what's on the file
cat $db

# Searching inside the file for the term inputted as the first argument
grep "$name" $db

# Adding someone on the file
echo "$name $number" >> $db    # Add a new input on the file
sort -o $db $db    # Organize the db/file content

# Deleting records from the file
grep -v "$name" $db > /tmp/$$    # Create the new file without the choosen record on /tmp/(PID)
mv /tmp/$$ $db    # Overwrite the file with the one without the record removed
#该片段来自于http://outofmemory.cn

标签:shell,基础

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2012-11-05 21:59:42java获得随机数代码 by 怪兽狂殴奥特曼
  2. 2014-04-24 10:59:58Shell创建空文件 by 古殇
  3. 2014-06-05 10:12:43批处理安装配置GCC,MSYS环境 by owensh
  4. 2014-08-01 10:42:55shell shift 用法 by owensh
  5. 2014-09-06 11:16:39PPP连接检查 by Smile_love
  6. 2014-11-11 11:41:42java web 项目发布脚本(Tomcat 版) by zjbpku
  7. 2014-04-25 21:09:33方阵的行和、列和 by zjbpku
  8. 2014-05-19 12:47:46删除Dreamweaver打开文件历史 by owensh
  9. 2014-05-27 10:48:47Powershell并行循环 by 玖夏
  10. 2014-05-30 10:25:11win ngins php 启动重启关闭脚本 by zjbpku
  11. 2014-07-26 09:32:06HAproxy-Keepalived一键安装脚本 by xfk

发表评论