如何在 bash 中的单行 for 循环中运行两个命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36340299/
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 14:26:33  来源:igfitidea点击:

How do I run two commands in a single line for loop in bash?

bashshell

提问by Ryan

Why can't I run two commands within a single line bash loop?

为什么我不能在一行 bash 循环中运行两个命令?

$ for i in {1..100} do printf %s "$(date)" ; mysql -uroot -e "SHOW SLAVE STATUS\G" | grep "Seconds_Behind_Master" ; sleep 10 ; done
-bash: syntax error near unexpected token `mysql'

But this simple version works:

但这个简单的版本有效:

for i in {1..3}; do echo $i ; ls ; done

回答by Eric Renouf

You need a ;after your brace expansion. You have it in the simple example, but not in the "broken" one:

你需要一个;在你的大括号扩展之后。您在简单的示例中拥有它,但在“损坏的”示例中没有:

for i in {1..100}; do printf %s "$(date)" ; mysql -uroot -e "SHOW SLAVE STATUS\G" | grep "Seconds_Behind_Master" ; sleep 10 ; done
                 ^ this one