bash脚本在注释行后无法执行

时间:2022-02-04 18:03:54

Why am I unable to complete my bash script with this #-comment? My script doesn't execute passed the commented line. Does it have to do with using \ backlashes in the preceding line?

为什么我无法使用此#comment完成我的bash脚本?我的脚本没有执行通过注释行。是否与前一行中使用\ backlashes有关?

"$PSQL_HOME"/psql -h $HOST_NM     \
                      -p $PORT    \
                      -U postgres \
                      -v v1=$1    \
                      -v v2=$_load \
#                     -f Test.sql
                      -f Test2.sql

4 个解决方案

#1


You can't do that. \ joins the current line to the next one, so what bash sees is:

你不能这样做。 \将当前行加入下一行,所以bash看到的是:

"$PSQL_HOME"/psql ... -v v1=$1 -v v2=$_load # -f Test.sql
                      -f Test2.sql

You can move the comment to the last line in this particular case:

在这种特殊情况下,您可以将注释移动到最后一行:

"$PSQL_HOME"/psql -h $HOST_NM     \
                      -p $PORT    \
                      -U postgres \
                      -v v1=$1    \
                      -v v2=$_load \
                      -f Test2.sql
#                     -f Test.sql

#2


Technically, the first six lines of your script are a single line for the shell. A comment stop the interpretation of the current line until its end so there is no way to have the line resuming after the # comment.

从技术上讲,脚本的前六行是shell的一行。注释会停止对当前行的解释直到结束,因此在#comment之后无法恢复该行。

Should you want to keep the order of options for some reason, you might use that syntax:

如果由于某种原因想要保留选项的顺序,可以使用该语法:

"$PSQL_HOME"/psql -h $HOST_NM     \
                      -p $PORT    \
                      -U postgres \
                      -v v1=$1    \
                      -v v2=$_load \
                      $(: -f Test.sql) \
                      -f Test2.sql

The $( ... ) is replacing a portion of a command by the execution of what is inside the parenthesis. The : is kind of a null command, somewhat similar to a # but unlike it, it doesn't end the current line so the outer command line can resume after it.

$(...)通过执行括号内的内容来替换命令的一部分。 :是一种空命令,有点类似于#但不像它,它不会结束当前行,所以外部命令行可以在它之后恢复。

#3


Store the options in an array instead. It's easier to comment out arbitrary elements of an array than to deal with line continuations the way you currently are.

将选项存储在数组中。注释掉数组的任意元素比以当前的方式处理行连续更容易。

psql_options=(
     -h "$HOST_NM"
     -p $PORT
     -U postgres
     -v v1="$1"
     -v v2="$_load"
     # -f Test.sql
     -f Test2.sql
)

"$PSQL_HOME"/psql "${psql_options[@]}"

#4


A backslash, in this case, escapes a newline. When \ is the last character, the shell keeps reading from the next line to complete reading of the command.

在这种情况下,反斜杠会转义换行符。当\是最后一个字符时,shell将继续从下一行读取以完成对命令的读取。

/home/atul/myBash> cat -vte tst.sh 
echo one\$
two\$
#three\$
four$
/home/atul/myBash> chmod +x tst.sh
/home/atul/myBash> ./tst.sh
onetwo#threefour
/home/atul/myBash> 

As per man bash
COMMENTS
... a word beginning with # causes that word and
all remaining characters on that line to be ignored.

根据man bash COMMENTS ...以#开头的单词会导致该单词和该行上的所有剩余字符被忽略。

Slighting modifying the tst.sh file

略微修改tst.sh文件

/home/atul/myBash> cat -vte tst.sh
echo one\$
two \$
# three\$
four$
/home/atul/myBash> ./tst.sh
onetwo
./tst.sh: line 4: four: command not found
/home/atul/myBash>

Note the space after two - it makes the # start a word. The line is interpreted as a comment. The word four is taken as start of another command.

注意两个后面的空格 - 它使#start成为一个单词。该行被解释为注释。第四个字被视为另一个命令的开始。

#1


You can't do that. \ joins the current line to the next one, so what bash sees is:

你不能这样做。 \将当前行加入下一行,所以bash看到的是:

"$PSQL_HOME"/psql ... -v v1=$1 -v v2=$_load # -f Test.sql
                      -f Test2.sql

You can move the comment to the last line in this particular case:

在这种特殊情况下,您可以将注释移动到最后一行:

"$PSQL_HOME"/psql -h $HOST_NM     \
                      -p $PORT    \
                      -U postgres \
                      -v v1=$1    \
                      -v v2=$_load \
                      -f Test2.sql
#                     -f Test.sql

#2


Technically, the first six lines of your script are a single line for the shell. A comment stop the interpretation of the current line until its end so there is no way to have the line resuming after the # comment.

从技术上讲,脚本的前六行是shell的一行。注释会停止对当前行的解释直到结束,因此在#comment之后无法恢复该行。

Should you want to keep the order of options for some reason, you might use that syntax:

如果由于某种原因想要保留选项的顺序,可以使用该语法:

"$PSQL_HOME"/psql -h $HOST_NM     \
                      -p $PORT    \
                      -U postgres \
                      -v v1=$1    \
                      -v v2=$_load \
                      $(: -f Test.sql) \
                      -f Test2.sql

The $( ... ) is replacing a portion of a command by the execution of what is inside the parenthesis. The : is kind of a null command, somewhat similar to a # but unlike it, it doesn't end the current line so the outer command line can resume after it.

$(...)通过执行括号内的内容来替换命令的一部分。 :是一种空命令,有点类似于#但不像它,它不会结束当前行,所以外部命令行可以在它之后恢复。

#3


Store the options in an array instead. It's easier to comment out arbitrary elements of an array than to deal with line continuations the way you currently are.

将选项存储在数组中。注释掉数组的任意元素比以当前的方式处理行连续更容易。

psql_options=(
     -h "$HOST_NM"
     -p $PORT
     -U postgres
     -v v1="$1"
     -v v2="$_load"
     # -f Test.sql
     -f Test2.sql
)

"$PSQL_HOME"/psql "${psql_options[@]}"

#4


A backslash, in this case, escapes a newline. When \ is the last character, the shell keeps reading from the next line to complete reading of the command.

在这种情况下,反斜杠会转义换行符。当\是最后一个字符时,shell将继续从下一行读取以完成对命令的读取。

/home/atul/myBash> cat -vte tst.sh 
echo one\$
two\$
#three\$
four$
/home/atul/myBash> chmod +x tst.sh
/home/atul/myBash> ./tst.sh
onetwo#threefour
/home/atul/myBash> 

As per man bash
COMMENTS
... a word beginning with # causes that word and
all remaining characters on that line to be ignored.

根据man bash COMMENTS ...以#开头的单词会导致该单词和该行上的所有剩余字符被忽略。

Slighting modifying the tst.sh file

略微修改tst.sh文件

/home/atul/myBash> cat -vte tst.sh
echo one\$
two \$
# three\$
four$
/home/atul/myBash> ./tst.sh
onetwo
./tst.sh: line 4: four: command not found
/home/atul/myBash>

Note the space after two - it makes the # start a word. The line is interpreted as a comment. The word four is taken as start of another command.

注意两个后面的空格 - 它使#start成为一个单词。该行被解释为注释。第四个字被视为另一个命令的开始。