使用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 )。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章