2017-04-06 52 views
-2

我試圖用這個怎麼做才能多在grep的if語句慶典

if `$(egrep '(cinta|aduh|siapa)' $inputFile)` ; then 
    filter='love' 
else `$(egrep '(kok|aku|dan)' $inputFile)` ; then 
    filter='jangan' 
else `$(egrep '(mari)' $inputFile)`; then 
    filter='mari' 
else `$(egrep '(kerumah|bebeb)' $inputFile)`; then 
    filter='bebeb' 
else `$(egrep '*' $inputFile)`; then 
    filter='other' 
fi 

,但我得到結果

./amosv5: line 175: syntax error near unexpected token `then' 
./amosv5: line 175: ` then' 

爲什麼會出現這種錯誤。

我需要像這樣在PHP

if (strpos($inputfile, "cinta") || strpos($inputfile, "aduh") || strpos($inputfile, "siapa") { 
$filter = 'love'; 
} else { $filter = 'other'; } } 
+1

使用'http:// www.shellcheck.net /'來解決這些微不足道的語法問題 – Inian

+1

你有什麼樣的輸入?如果文件中有「kerumah」,過濾器應該是'bebeb',但如果將「kok」添加到文件中,它會變成'jangan',這似乎很奇怪。您的輸入文件是否受到限制? (例如,只有一個字)。如果輸入很簡單,那麼可以用一個很好的case語句來完成。 –

+0

我很困惑修復它。請幫我 – Amos

回答

4

的語法是普通

您可以直接在bash條件中使用shell實用程序的大部分的返回碼。在第一個條件egrep與多個模式匹配cinta|aduh|siapa將返回true,如果它能夠找到任何的模式。同樣,這可以爲其餘部分完成。

if egrep 'cinta|aduh|siapa' "$inputFile" ; then 
    filter='love' 
elif egrep 'kok|aku|dan' "$inputFile" ; then 
    filter='jangan' 
.. 
+0

嘿,朋友,我得到這樣的結果 – Amos

+0

./amosv5:行173:行173:附近意外的標記'然後」 – Amos

+0

./amosv5語法錯誤' egrep'(kok | aku | dan)'$ inputFile;然後' – Amos