2011-12-18 13 views
1

需要關於while命令的幫助。我想運行一個命令,如果有一個在playlist.txt

這裏,$站點名是我的bash:

cat playlist.txt | grep -e "$sitename" | sed -e "s/^.*\(http:.*"$sitename".*flv\).*$/\1/g" | sort | uniq > checklist.txt 
cat checklist.txt 

while [ " While can find $sitename in checklist.txt" ]; do 
       qa 
     done 

這就是我曾嘗試已經

while [ -n "$echopl" ]; do 
        qa 
      done 

while [ 'grep -q $string checklist.txt >/dev/null' ]; do 
       qa 
     done 

while [ "grep -q "$string" checklist.txt" ]; do 
       qa2 
     done 
while grep -q "$string" checklist.txt; do 
    qa 
done 

while grep -q '$string' checklist.txt; do 
    qa 
done 


if grep -q "$string" checklist.txt ; then 
    qa 
fi 

暫時我把很多,如果命令: (

ifmorelinks ifmorelinks ifmorelinks ifmorelinks ifmorelinks

回答

1
if grep "$string" checklist.txt >/dev/null 2>&1 ; then 
    qa 
fi 
+0

奏效。謝謝! – 2011-12-18 12:17:10

+0

來自man grep(1):_Portability註釋:與GNU grep不同,第7版Unix grep不符合POSIX,因爲它缺少-q和-s選項的行爲與GNU grep的-q選項相同。 USG風格的grep也缺少-q,但它的-s選項表現得像GNU grep。可移植shell腳本應避免-q和-s,並應將標準錯誤輸出重定向到/ dev/null ._ – 2011-12-18 16:18:49

+1

此解決方案存在問題:如果$ string包含正則表達式元字符,它將被解釋!所以,如果你有,在你的字符串中說'a.com',它會匹配清單中的'fatcomparse.org'。你最好使用'fgrep'或者'grep -F'。另外,請考慮'-w'選項。 – fge 2011-12-18 16:33:20

2
while grep -q "$string" checklist.txt; do 
    qa 
done 

如果$string意味着string可變的字面$string而不是值,使用:

while grep -q '$string' checklist.txt; do 
    qa 
done 
+0

不起作用。已經嘗試過了。即使checklist.txt中沒有「$ string」,qa也會運行 – 2011-12-19 10:33:45

+0

好的,查看更新 – unbeli 2011-12-19 10:35:44

+0

相同...進入循環。 – 2011-12-19 14:12:26

相關問題