2014-10-08 38 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 {}"