2014-10-08 45 views
-3

我需要在搜索單詞後顯示文件內容。如果找到該單詞,則顯示該文件。在搜索單詞後顯示文件內容完成

我的代碼如下:

GNU納米2.2.6文件:工作

#!/bin/bash 


while read -p "Welcome what would you like to do (S) to search or (Q) to quit " option 
do 
case $option in 
     "S") echo "What is the name of the file you would like to search for?" 
       read file 
       echo "What word would you like to find in the file?" 
       read word 
       grep -q $word $file 
       if [ $? -eq 0 ]; then 
       echo "$word found in $file" 
       cat $file 
       else 
       echo "$word NOT found in $file" 
       fi 
       ;; 

     "Q") echo "Goodbye!" 
       exit ;; 

      *) echo "invalid option" ;; 
esac 
done 
+1

你的問題是不可理解的壞英文。 – dr0i 2014-10-08 12:52:56

+0

即時對不起,我有不良記憶 – 2014-10-08 12:54:32

+1

好的。所以試着用另一種方式重新描述或描述你的問題。不要害羞成爲多餘的。 – dr0i 2014-10-08 12:56:15

回答

1

更換

echo $file 

cat $file 
0

我相信你正在尋找的命令cat $file。將其粘貼在if區塊內。

0

我需要裝載了什麼文件,進行加載該文件說。

沒有訪問文件的情況下無法訪問文件的內容。

0
grep -l word file | xargs -r cat 

顯示文件內容,如果找到了單詞。這也顯示文件的名稱

grep -l word file | xargs -r -i bash -c "echo {}:; cat {}"