Argument passing in .sh scripts
Asked Answered
P

2

8

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.

Prebend answered 8/1, 2012 at 23:39 Comment(10)
I think you need to explain what you mean by #$ -N $2. If your script is a bourne shell script then this is a comment (it begins with #) but you seem to mean it as something else.Limewater
I already tried to explain it, see comment to Saptamus Prime. This option is used to set the job name in a cluster environment, e.g. see: clusterresources.com/torquedocs/commands/qsub.shtml. True, normally # is used for comments. But not when followed when used in the syntax #$ -Argument Value. Thanks.Prebend
If the question concerns shell metasyntax specific to qsub, you should have mentioned that in the question. Anyway, the link you sent talks about a #PBS directive but it doesn't mention #$ at all, so I still don't know what that means. In any case, it sounds like those qsub directives are interpreted by qsub itself before the script is run, so it would make sense that arguments which are passed to the script once it gets executed don't enter into the processing of such directives. I think you're supposed to supply a static string for the name of the job.Limewater
Also, the use of -l in the shebang line of the script is suspicious. -l forces a login shell, which will normally invoke /etc/profile functionality designed for interactive shells. No shell script should need that.Limewater
Thanks, Celada. I did not realize this behaviour is specific to qsub. I guess I do not know sh-behaviour well enough. If I pass a static string it works. So you are saying given the behaviour of qsub I cannot pass any parameter to the script foo.sh but I have to hard-code the argument names for qsub as static strings?Prebend
I kept the '-l' option because I thought it sets up the shell script. I already had removed all further commands to create a minimum example.Prebend
be aware that #!/bin/sh means something different than #!/bin/bash and also, when you do sh foo.sh you're bypassing that specification completely. see: https://mcmap.net/q/421695/-what-is-the-use-meaning-of-quot-bin-sh-quot-in-shell-scriptingImpermeable
I am actually calling qsub foo.sh param1 param2. But as mentioned above I am not sure whether the actual qsub information is relevant because I thought '#$ -Argument Value' was standard shell script syntax. And, I was trying to pass an argument value dynamically in this case.Prebend
Someone who knows something about qsub will answer your question about qsub parameter passing. As for the -l option, you shouldn't need it. If your script doesn't work without it then something should be corrected.Limewater
Thanks Celada. So unless somebody knows how it works, I will generate n script files with all $2 parameter values, and the iterate through them. Not very beautiful but well...Prebend
A
2

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>
Arzola answered 13/3, 2012 at 22:29 Comment(0)
C
4

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
Cleodell answered 8/1, 2012 at 23:50 Comment(1)
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.Prebend
A
2

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>
Arzola answered 13/3, 2012 at 22:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.