0

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
5
  • Could you provide the content of your script ? Commented Jun 27, 2016 at 15:00
  • Fix your if-tests if [ "x{$1}" -eq 'x' ]; then Commented Jun 27, 2016 at 15:09
  • wrap with quote of the path as argument Commented Jun 27, 2016 at 15:16
  • The changed if statements don't work. Instead of "command not found" it says "integer expression expected" now. Line numbers stay the same. Regarding wrapping with quotes: I already tried that (see OP) and it didn't make a difference. Commented Jun 27, 2016 at 15:19
  • The brackets are important, and if you get an error like 'integer expression expected', does it not occur to you to check the bash documentation and try == instead of -eq? If not, why not? Commented Jun 27, 2016 at 15:37

2 Answers 2

2

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" ;
Sign up to request clarification or add additional context in comments.

1 Comment

That fixed it, thanks. Strange thing is I used the same if-tests in my other script that worked fine, but I'll just replace it all with the test -z command
0

You may need to provide what is happening in the code on that line, but from what I can tell you are trying to do something like this in your script:

some/path=$1

you can't do that. "/" is a special character and cannot be part of a bash var name.

1 Comment

My bad, I thought this was solely a problem with the call of the scripts and not the script itself. I'll update the OP in a few minutes. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.