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

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

问题描述

当命令是 if 语句的一部分时,如何在 shell 中将命令拆分为多行?

How can I split a command over multiple lines in the shell, 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?

推荐答案

如果在反斜杠之后和换行符之前有空格(空格或制表符1),则续行将失败.没有这样的空格,你的例子对我来说很好用:

The line-continuation will fail if you have whitespace (spaces or tab characters1) after the backslash and before the newline. With no such whitespace, 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

评论中提出的一些细节:shell 中的行继续反斜杠并不是真正的特例;它只是一般规则的一个实例,即反斜杠引用"紧随其后的字符,防止它通常受到的任何特殊处理.在这种情况下,下一个字符是换行符,被阻止的特殊处理是终止命令.通常,一个带引号的字符会按字面意思包含在命令中;反斜杠的换行符被完全删除.但除此之外,机制是相同的.最重要的是,反斜杠只引用紧随其后的字符;如果该字符是空格或制表符,您只会得到一个文字空格或制表符(这将是它自己的单独参数,因为反斜杠阻止空格折叠到分隔其他参数的周围未加引号的空格中);反斜杠对后续的换行符没有影响.

Some detail promoted from the comments: the line-continuation backslash in the shell is not really a special case; it is simply an instance of the general rule that a backslash "quotes" the immediately-following character, preventing any special treatment it would normally be subject to. In this case, the next character is a newline, and the special treatment being prevented is terminating the command. Normally, a quoted character winds up included literally in the command; a backslashed newline is instead deleted entirely. But otherwise, the mechanism is the same. Most importantly, the backslash only quotes the immediately-following character; if that character is a space or tab, you just get a literal space or tab (which will then be its own separate argument, since the backslash stops the whitespace from being collapsed into the surrounding unquoted whitespace that separates the other arguments); the backslash will have no effect on the subsequent newline.

1 或回车.Bash 不兼容 Windows 格式的文本文件,甚至在 WSL 中也不兼容.或 Cygwin,但至少他们的 Bash 端口添加了一个 set -o igncr 选项,您可以设置该选项以使其支持回车.

1 or carriage returns, for that matter, as Czechnology points out. Bash does not get along with Windows-formatted text files, not even in WSL. Or Cygwin, but at least their Bash port has added a set -o igncr option that you can set to make it carriage-return-tolerant.

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

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