2016-02-28 54 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