虽然 bash 中的命令“如果 file.txt 中有 $string... 做...”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8551382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:11:25  来源:igfitidea点击:

While command in bash "if there's a $string in file.txt... do..."

linuxbashwhile-loopdebian

提问by Crazy_Bash

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

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

Here's my bash:

这是我的 bash:

   cat playlist.txt | grep -e "$sitename" |  sed -e "s/^.*\(http:.*"$sitename".*flv\).*$//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 :(

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

ifmorelinks ifmorelinks ifmorelinks ifmorelinks ifmorelinks

ifmorelinks ifmorelinks ifmorelinks ifmorelinks ifmorelinks

采纳答案by Andrew Schulman

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

回答by unbeli

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

If $stringmeans literally $stringand not the value of stringvariable, use this:

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

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