While command in bash “if there's a $string in file.txt… do…”

喜欢而已 提交于 2019-12-13 17:24:49

问题


Need help with the while command. I want to run a command if there's a $sitename in playlist.txt

Here's my 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

that's what i have tried already

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

Temporarily I put many if commands :(

ifmorelinks ifmorelinks ifmorelinks ifmorelinks ifmorelinks


回答1:


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



回答2:


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

If $string means literally $string and not the value of string variable, use this:

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


来源:https://stackoverflow.com/questions/8551382/while-command-in-bash-if-theres-a-string-in-file-txt-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!