2012-07-02 83 views
155

我想檢查文件是否包含特定字符串或不在bash中。我用這個腳本,但它不起作用:如何檢查文件是否包含使用bash的特定字符串

if [[ 'grep 'SomeString' $File' ]];then 
    Some Actions 
fi 

我的代碼有什麼問題?

+2

類似於http://stackoverflow.com/questions/4749330/how-to-test-if-string-exists-in-file-with-bash-shell – Pawel

回答

299
if grep -q SomeString "$File"; then 
    Some Actions # SomeString was found 
fi 

這裏你不需要[[ ]]。只需直接運行命令。添加-q選項,當您不需要找到它時顯示的字符串。

grep命令根據 搜索結果在退出代碼中返回0或1。 0,如果發現了什麼;否則爲1。

$ echo hello | grep hi ; echo $? 
1 
$ echo hello | grep he ; echo $? 
hello 
0 
$ echo hello | grep -q he ; echo $? 
0 

您可以指定命令作爲條件if。如果該命令的退出代碼返回0,表示條件爲真;否則爲false。

$ if /bin/true; then echo that is true; fi 
that is true 
$ if /bin/false; then echo that is true; fi 
$ 

正如你所見,你直接在這裏運行程序。沒有額外的[][[]]

+1

謝謝親愛的伊戈爾,非常好的回答和完整...我從這篇文章中學到了很多東西。 – Hakim

+0

約'grep'和可用選項asnwer http://stackoverflow.com/a/4749368/1702557 – Pawel

+1

更多信息,可能要考慮'-Fxq'參數在http://stackoverflow.com/a/4749368/544721 –

-1
grep -q "something" file 
[[ !? -eq 0 ]] && echo "yes" || echo "no" 
+1

你有一個錯字 - !?應該是$? – lzap

+0

...它不僅是意在'$',但沒有必要爲它在所有 - ?你可以只是做'如果grep的-q ...' –

+0

此外,'富&& truthy_action || falsey_action'不是一個真正的三元運算符 - 如果'truthy_action'失敗,則'falsey_action'運行_in addition_。 –

0

最短(正確的)版本:

grep -q "something" file; [ $? -eq 0 ] && echo "yes" || echo "no" 

也可以寫成

grep -q "something" file; test $? -eq 0 && echo "yes" || echo "no" 

,但你不需要明確地測試它在這種情況下,因此同樣有:

grep -q "something" file && echo "yes" || echo "no" 
+0

'if grep -q something file;然後迴應是的;否則回顯否; fi'。沒有理由混淆'$?'。 –

0

我這樣做,似乎工作正常

if grep $SearchTerm $FileToSearch; then 
    echo "$SearchTerm found OK" 
else 
    echo "$SearchTerm not found" 
fi 
21

除了其他的答案,它告訴你如何做你想要什麼,我試圖解釋什麼是錯的(這是你想要的。

在Bash中,if後跟一個命令。如果此命令的退出碼等於0,則執行零件then,否則執行else零件(如果有的話)。

你可以做到這一點的任何命令在其他的答案解釋:if /bin/true; then ...; fi

[[是專門爲一些測試,如文件的存在,可變比較內部的bash命令。類似地,[是一個外部命令(通常位於/usr/bin/[),其執行的測試大致相同,但需要]作爲最終參數,這就是爲什麼]必須在左側用空格填充,而]]則不是這種情況。

在這裏您不需要[[[

另一件事是你引用事物的方式。在bash中,只有一對引用嵌套的情況,它是"$(command "argument")"。但在'grep 'SomeString' $File'只有一個字,因爲'grep '是帶引號的單元,這是連接在一起SomeString,然後再次' $File'串聯。由於使用了單引號,變量$File甚至沒有被它的值所取代。正確的方法是grep 'SomeString' "$File"

7
##To check for a particular string in a file 

cd PATH_TO_YOUR_DIRECTORY #Changing directory to your working directory 
File=YOUR_FILENAME 
if grep -q STRING_YOU_ARE_CHECKING_FOR "$File"; ##note the space after the string you are searching for 
then 
echo "Hooray!!It's available" 
else 
echo "Oops!!Not available" 
fi 
+0

更改目錄通常不是一個好主意(在這裏完全沒有必要,只需在目標目錄中限定文件名即可)。然後,你所擁有的與接受的答案完全相同,只是用較少的細節。 – Mat

+0

感謝您的建議墊 –

1
if grep -q [string] [filename] 
then 
    [whatever action] 
fi 

if grep -q 'my cat is in a tree' /tmp/cat.txt 
then 
    mkdir cat 
fi 
3
grep -q [PATTERN] [FILE] && echo $? 

如果該圖案被發現退出狀態爲0(真);

0

如果你想checkif字符串的整條生產線相匹配,如果它是一個固定的字符串,你可以這樣來做

grep -Fxq [String] [filePath] 

例如

searchString="Hello World" 
file="./test.log" 
if grep -Fxq "$searchString" $file 
    then 
      echo "String found in $file" 
    else 
      echo "String not found in $file" 
fi 

從man文件:

-F, --fixed-strings 

      Interpret PATTERN as a list of fixed strings, separated by newlines, any of 

which is to be matched. 
      (-F is specified by POSIX.) 
-x, --line-regexp 
      Select only those matches that exactly match the whole line. (-x is specified by 

POSIX.) 
-q, --quiet, --silent 
      Quiet; do not write anything to standard output. Exit immediately with zero 

status if any match is 
      found, even if an error was detected. Also see the -s or --no-messages 

option. (-q is specified by 
      POSIX.) 
0

試試這個:

if [[ $(grep "SomeString" $File) ]] ; then 
    echo "Found" 
else 
    echo "Not Found" 
fi 
相關問題