linux - 高级 while 控制语句

标签 linux scripting

我正在开发一个 posix 脚本 (Linux),它获取一个网页,将内容存储在一个变量中并查找字符串“SUCCESS”。如果找到字符串,则不执行循环内容,如果没有找到字符串,则反复执行循环,直到找到为止。我试图让所有 curl 和 grep 的东西都发生在 while 循环的控制语句中,但我不确定这是否可能,到目前为止没有成功:

while [ -z "site_status=`curl http://www.example.com/index.html | grep -o 'SUCCESS'`"     ];
do
if [ `echo "$site_status" | grep -o 'WAIT'` ]; then
    sleep 10s
elif [ `echo "$site_status" | grep -o 'RETRY'` ]; then
    sleep 10s
else
    exit 1
done

我希望脚本尽可能干净,所以我尽量避免创建将网页内容保存在全局变量中的函数,并在 while 循环中使用该变量。

关于如何使上述 while 循环工作的任何建议?或者更好的清洁解决方案?

最佳答案

这个似乎可以满足您对我的所有要求:

until { t=`curl http://www.example.com 2> /dev/null` && echo $t | grep -q SUCCESS; };
    do (echo $t | grep -q "\(WAIT\|RETRY\)" && sleep 10) || exit 1
done

关于linux - 高级 while 控制语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11581695/

相关文章:

regex - 在 bash 中交换文件中的 2 个单词的最佳方法是什么

linux - 如何从目录中删除 ACL 并恢复到通常的访问控制?

c - 在设置 subreapers 时,是否有一些关于如何使用 prctl() 的简短示例?

python - 如何在 CentOS 5.5 中手动安装 Python 模块?

function - 如何更正 "Function definitions are not permitted at the prompt or in scripts"

linux-shellscript- "$#"

Apache 日志轮转脚本

windows - 为什么使用管道在 powershell 中不起作用?

scripting - Windows 批处理脚本的良好替代方案?

linux - 如何终止 Flash 孤立进程?