Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash script unable execute after a commented line

Tags:

bash

unix

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?

"$PSQL_HOME"/psql -h $HOST_NM     \
                      -p $PORT    \
                      -U postgres \
                      -v v1=$1    \
                      -v v2=$_load \
#                     -f Test.sql
                      -f Test2.sql
like image 408
noober Avatar asked Dec 15 '22 14:12

noober


2 Answers

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

"$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
like image 77
rmmh Avatar answered Dec 30 '22 08:12

rmmh


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.

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.

like image 40
jlliagre Avatar answered Dec 30 '22 07:12

jlliagre