daZza daZza - 5 months ago 9
Bash Question

Calling shell script via bash using a path as an argument

I am trying to execute a shell script via bash while also trying to pass arguments for said script.

Up to now I've never had a problem with this and commands like

/opt/scripts/start_import.sh user pass "Some name with spaces"
always executed fine. The three arguments were passed just fine and could be used within the start_import script.

Today I wrote a new script, let's call it set_properties.sh. I am executing as usual with the command
/opt/scripts/set_properties.sh /opt/subfolder/subfolder user pass stringwithoutspaces stringwithoutspaces2
.

However, this time around I am getting bombed with error messages:

/opt/scripts/set_properties.sh: line 7: /opt/subfolder/subfolder=: No such file or directory
/opt/scripts/set_properties.sh: line 13: user=: command not found
/opt/scripts/set_properties.sh: line 19: pass=: command not found
/opt/scripts/set_properties.sh: line 56: syntax error: unexpected end of file


I suspect that passing a path (/opt/subfolder/subolder) as an argument is causing this error. However, I haven't found a solution to this yet...

I guess the shell is trying to evaluate the path instead of just passing it along as an argument. I tried enclosing the path with double quotes, which should stop any evaluation of the enclosed string, but no luck.

What am I doing wrong? Is there any solution for passing paths as arguments when calling a script? Thanks in advance!




Edit: Content of set_properties.sh:

set +v

error_txt=""
ret_code="0"

#check input parameters
if $1=""
then
error_txt="Invalid parameter for 'XXXX'."
ret_code="1"
fi

if $2=""
then
error_txt="Invalid parameter for 'XXXX'."
ret_code="1"
fi

if $3=""
then
error_txt="Invalid parameter for 'XXXX'."
ret_code="1"
fi

if $4=""
then
error_txt="Invalid parameter for 'XXXX'."
ret_code="1"

if $5=""
then
error_txt="Invalid parameter for 'XXXX'."
ret_code="1"
fi

# create new .properties based on input parameters
echo "################################################################################" > $1/subfolder1/subfolder2/my_property_file.properties
echo "#xxxxyyyyzzzz" >> $1/subfolder1/subfolder2/my_property_file.properties
echo "#created automatically by xxxx processing on `date`" >> $1/subfolder1/subfolder2/my_property_file.properties
echo "#xxxxyyyyzzzz" >> $1/subfolder1/subfolder2/my_property_file.properties
echo "propertyName=^$4" >> $1/subfolder1/subfolder2/my_property_file.properties
echo "#xxxxyyyyzzzz" >> $1/subfolder1/subfolder2/my_property_file.properties
echo "propertyName=$2" >> $1/subfolder1/subfolder2/my_property_file.properties
echo "#xxxxyyyyzzzz" >> $1/subfolder1/subfolder2/my_property_file.properties
echo "propertyName=$3" >> $1/subfolder1/subfolder2/my_property_file.properties
echo "#xxxxyyyyzzzz" >> $1/subfolder1/subfolder2/my_property_file.properties
echo "propertyName=$5" >> $1/subfolder1/subfolder2/my_property_file.properties
echo "#xxxxyyyyzzzz" >> $1/subfolder1/subfolder2/my_property_file.properties
echo "propertyName=N" >> $1/subfolder1/subfolder2/my_property_file.properties
echo "################################################################################" >> $1/subfolder1/subfolder2/my_property_file.properties

ret_code=$?

echo $error_txt
exit $ret_code

Answer

This is invalid:

if $1=""

You need to use the test command to compare variables. Pay close attention to the whitespace as well:

if test "$1" = "" ;

This can also be written more briefly as

if test -z "$1" ;