我如何使用IF语句时拆分为多行的bash命令? [英] How can I split a Bash command over multiple lines when using an IF statement?

查看:189
本文介绍了我如何使用IF语句时拆分为多行的bash命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何能比我在猛砸,当命令是如果语句的一部分?

How can I split a command over multiple lines in Bash, when the command is part of an if statement?

虽然这个作品:

if ! fab --fabfile=.deploy/fabfile.py --forward-agent --disable-known-hosts deploy:$target; then                                                                                 
  rc=1                                                                                                                                                                           
fi   

这是行不通的:

# does not work:                                                                                                                                                             
if ! fab --fabfile=.deploy/fabfile.py \                                                                                                                                         
  --forward-agent \                                                                                                                                                             
  --disable-known-hosts deploy:$target; then   
  rc=1                                                                                                                                                                           
fi 

而不是整个命令执行的,我得到:

Instead of the whole command executing, I get:

./script.sh: line 73: --forward-agent: command not found

更重要的是,什么是从我的Bash的理解缺失,这将有助于我了解这一点,类似问题在未来?

More importantly, what is missing from my understanding of Bash that will help me understand this and similar issues in the future?

推荐答案

您确定你没有这些反斜杠后的任何空白?你的榜样正常工作对我来说:

You sure you don't have any whitespace after those backslashes? Your example works fine for me:

$ cat test.sh
if ! fab --fabfile=.deploy/fabfile.py \
   --forward-agent \
   --disable-known-hosts deploy:$target; then
     echo failed
else
     echo succeeded
fi

$ alias fab=true
$ . ./test.sh
succeeded
$ alias fab=false
$ . ./test.sh
failed

这篇关于我如何使用IF语句时拆分为多行的bash命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆