Argument passing in .sh scripts

51,445

Solution 1

No you can't. The # at the beginning of the line makes it so that the $2 won't be replaced by the argument to the script. The way to do what you're trying to do is

qsub foo.sh -N <name>

Solution 2

Works fune for me.

I don't know what the -N command means, but

#!/bin/bash -l
#$ -S /bin/bash
#$ -N $2
echo $1
echo $2

when called by sh foo.sh a b promptly echoes

a
b
Share:
51,445
user1137731
Author by

user1137731

Updated on March 14, 2020

Comments

  • user1137731
    user1137731 over 4 years

    I have a shell script foo.sh which is a qsub job with content:

        #!/bin/bash -l
        #$ -S /bin/bash
        #$ -N $2
        echo $1
    

    I would like to pass two arguments. If I call qsub foo.sh a b the first argument gets correctly processed and echoed to the command line as 'a'. However, I do not know how to pass an argument in the second case starting with '#$ -N'. In this case $2 does not get evaluated to 'b' but actually '$2' is set. Help would be much appreciated.

  • user1137731
    user1137731 over 12 years
    Thanks for your reply. But I already mentioned that argument passing to the echo command works fine. However, in the second case '#$ -N $2' $2 does not get evaluated as 'b'. To give some context: this is qsub script for the sun grid engine and the '-N' option sets the name of the job. So in this case the job would be set as '$2' and not as desired as 'b'. So the general question is how do I pass parameters to '#$ -SomeParameterOption' $2? Thank you.