Shell 脚本:在多行逻辑运算符之间散布注释
Shell scripting: intersperse comments between multiline logical operators
我有一个像这样的多行条件:
command1 \
&& command2 \
&& command3
这很好,但我想在每个命令后添加注释,如下所示:
command1 \ # Comment for command 1.
&& command2 \ # Comment for command 2.
&& command3 # Comment for command 3.
但脚本因解析错误而中断(在 Bash 和 Zsh 中)。
有没有办法在多行条件之间添加注释?
注意:我意识到我可以为每个命令创建一个函数,然后在其函数定义中为每个命令添加注释;但我想尽可能避免这种情况。
无需转义注释开头。您可以像这样使用内嵌注释:
pwd && # comment1
date && # comment2
tty # comment3
我在我的点文件中使用像这样的片段在多行调用中嵌入注释:
function e() {
setsid -f $(: "fork into separate session") \
emacsclient \
--create-frame $(: "force new gui window") \
--alternate-editor='' $(: "launch emacs if daemon is missing") \
"$@"
}
:
是 true
的标准 shorthand,它在忽略参数时成功。这就是为什么 $(: "foo")
被评估为空的原因。
我有一个像这样的多行条件:
command1 \
&& command2 \
&& command3
这很好,但我想在每个命令后添加注释,如下所示:
command1 \ # Comment for command 1.
&& command2 \ # Comment for command 2.
&& command3 # Comment for command 3.
但脚本因解析错误而中断(在 Bash 和 Zsh 中)。
有没有办法在多行条件之间添加注释?
注意:我意识到我可以为每个命令创建一个函数,然后在其函数定义中为每个命令添加注释;但我想尽可能避免这种情况。
无需转义注释开头。您可以像这样使用内嵌注释:
pwd && # comment1
date && # comment2
tty # comment3
我在我的点文件中使用像这样的片段在多行调用中嵌入注释:
function e() {
setsid -f $(: "fork into separate session") \
emacsclient \
--create-frame $(: "force new gui window") \
--alternate-editor='' $(: "launch emacs if daemon is missing") \
"$@"
}
:
是 true
的标准 shorthand,它在忽略参数时成功。这就是为什么 $(: "foo")
被评估为空的原因。