在bash中命令“如果file.txt中有$ string ...就行......”

时间:2011-12-18 11:24:55

标签: linux bash while-loop debian

需要有关while命令的帮助。如果在playlist.txt中有$ sitename,我想运行一个命令

这是我的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

我暂时放了很多if命令:(

ifmorelinks ifmorelinks ifmorelinks ifmorelinks ifmorelinks

2 个答案:

答案 0 :(得分:2)

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

如果$string的字面意思是$string,而不是string变量的值,请使用:

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

答案 1 :(得分:1)

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