返回顶部

收藏

使用文件作为数据库

更多

简单的例子,你如何可以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

发表评论