2016-02-28 59 views
2

在我正在研究的bash脚本中,我需要添加一个grep选项才能在命令行接受,以便只从与模式匹配的文件中读取单词。系统会提示用户输入书籍,作者,发布者并发布年份,并且该列表以book1〜author1〜pub1〜date1格式存储在文件簿中,每个集合都放在一个单独的行中。如果在命令行(bookinfo print)传递了“print”,则将“book”内容文件放入Book:book1(\ n) Author:author1等中的book_print文件中。格式。我想要做的是添加一个grep选项,以便当在命令行中使用-f选项指定字符串时,只有包含该模式的“books”文件中的行被放入“book_print”文件中。例如,如果命令是“bookinfo -f”author2“”,则只有包含author2的“books”中的行将被放入book_print文件中。 (bookinfo是脚本的名称)Unix将grep选项添加到Bash脚本

这是我到目前为止。我开始使用-f选项代码,但不知道从哪里开始。

#!/bin/bash 
n=${1:-1} 
#while getopts f name 
#do 
#  case $name in 
#    f)dopt=1;; 
#    *) echo "Invalid arg";; 
#  esac 
#done 
if [[ $1 == "print" ]] 
then 
    printf "Booktitle: \t\t %s\n" `awk -F '~' '{print $1}' books` >> book_print 
    printf "Author(s): \t\t %s\n" `awk -F '~' '{print $2}' books` >> book_print 
    printf "Publisher: \t\t %s\n" `awk -F '~' '{print $3}' books` >> book_print 
    printf "Year of Publication: \t %s\n" `awk -F '~' '{print $4}' books` >> book_print 
else 
    for ((i = 1; i < n + 1; i++)) 
    do 
     echo -n "Booktitle: " 
     read b 
     book=$b 
     echo -n $book >> books 
     echo -n "~" >> books 
     echo -n "Author(s): " 
     read a 
     author=$a 
     echo -n $author >> books 
     echo -n "~" >> books 
     echo -n "Publisher: " 
     read p 
     publisher=$p 
     echo -n $publisher >> books 
     echo -n "~" >> books 
     echo -n "Year of publication: " 
     read y 
     year=$y 
     echo $year >> books 
    done 
fi 

编辑 - 我改变while循环代码以执行以下操作:

while getops ":f" opt; 
do 
    case $opt in 
     f) 
      grep "$OPTARG" books 
      ;; 
     *) 
      echo "Invalid argument." 
      ;; 
    esac 
done 

我的书文件包含线A〜B〜C〜d和E〜F〜G〜小时。当我运行命令./bookinfo -f“A”时,显示的是整个图书文件,而不仅仅是包含A的行。

回答

2

好像你是朝着正确的方向前进的,这里是你需要的:

#!/bin/bash 

while getopts "f:" opt; 
do 
    case $opt in 
    f) 
     echo "Found pattern: $OPTARG" 
     ;; 
    *) 
     echo "Wrong arg" 
     # Call the usage function here 
    esac 
done 

您可能想了解getops tutorial以进一步了解getop如何工作。

+0

任何时候,我使用getopts的(这是很多),我添加了一个'-h'选项记录什么有效的选项是。 –

+0

@glennjackman是的,这是一个很好的做法,而不是问题中提出的问题。 :) – ffledgling

+0

感谢您的帮助,我改变了我的while循环并编辑了OP。我的脚本仍然不能正常工作,我认为这可能是我的grep命令的结果。 – tfreiner

1

不是一个答案,但一个快速的改写,使你的代码更紧一点:

print() { 
    # doing this in a single awk command is much more efficient 
    # the default search pattern is "non-empty line" 
    awk -F '~' -v pattern="${1:-.}" ' 
     $0 ~ pattern { 
      printf "Booktitle: \t\t %s\n", $1 
      printf "Author(s): \t\t %s\n", $2 
      printf "Publisher: \t\t %s\n", $3 
      printf "Year of Publication: \t %s\n", $4 
     } 
    ' books >> book_print 
} 

populate() { 
    while true; do 
     # use read -p to incorporate the prompt, 
     # and just use one variable per item 
     read -p "Booktitle (blank to quit): " book 
     [[ -z "$book" ]] && break 
     reap -p "Author(s): " author 
     read -p "Publisher: " publisher 
     read -p "Year of publication: " year 
     # critically important to quote the variables here: 
     printf "%s~%s~%s~%s\n" "$book" "$author" "$publisher" "$year" 
    done >> books 
} 

# magic getopts here to set the search pattern, say in $search variable, 
# and a flag to indicate print versus populate 

if [[ "$do_search" -eq 1 ]]; then 
    print "$search" 
else 
    populate 
done