使用IF语句时,如何将shell命令分成多行?

本文翻译自:How can I split a shell command over multiple lines when using an IF statement?

How can I split a command over multiple lines in the shell, when the command is part of an if statement? 当命令是if语句的一部分时,如何在外壳中将命令拆分为多行?

This works: 这有效:

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

This doesn't work: 这不起作用:

# 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

More importantly, what is missing from my understanding of Bash that will help me understand this and similar issues in the future? 更重要的是,我对Bash的了解缺少什么,这将有助于我将来了解此问题和类似问题?


#1楼

参考:https://stackoom.com/question/1G2dL/使用IF语句时-如何将shell命令分成多行


#2楼

The line-continuation will fail if you have whitespace (spaces or tab characters) 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

Some detail promoted from the comments: the line-continuation backslash in the shell is not really a special case; 评论中提出了一些细节:shell中的行继续反斜杠并不是真正的特殊情况; 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. 但是除此之外,机制是相同的。 And the backslash only quotes the immediately-following character; 反斜杠仅引用紧随其后的字符; if that character is a space or tab, you just get a quoted space or tab, and any subsequent newline remains unquoted. 如果该字符是空格或制表符,则只会得到一个带引号的空格或制表符,而所有后续换行符都将不加引号。


#3楼

For Windows/WSL/Cygwin etc users: 对于Windows / WSL / Cygwin等用户:

Make sure that your line endings are standard Unix line feeds, ie \\n (LF) only. 确保您的行尾是标准Unix换行符,即\\n (LF)。

Using Windows line endings \\r\\n (CRLF) line endings will break the command line break. 使用Windows行尾\\r\\n (CRLF)行尾将破坏命令行中断。


This is because having \\ at the end of a line with Windows line ending translates to \\ \\r \\n . 这是因为具有\\在行与Windows线年底结束转换为\\ \\r \\n
As Mark correctly explains above: 正如Mark正确解释的那样:

The line-continuation will fail if you have whitespace after the backslash and before the newline. 如果您在反斜杠之后和换行符之前有空格,则行继续将失败。

This includes not just space ( 这不仅包括空间( ) or tabs ( \\t ) but also the carriage return ( \\r ). )或制表符( \\t )以及回车符( \\r )。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值