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.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
user1137731
  • 133
  • 1
  • 2
  • 6
  • 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. – Celada Jan 09 '12 at 00:00
  • 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: http://www.clusterresources.com/torquedocs/commands/qsub.shtml. True, normally # is used for comments. But not when followed when used in the syntax #$ -Argument Value. Thanks. – user1137731 Jan 09 '12 at 00:03
  • 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. – Celada Jan 09 '12 at 00:16
  • 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. – Celada Jan 09 '12 at 00:17
  • 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? – user1137731 Jan 09 '12 at 00:20
  • 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. – user1137731 Jan 09 '12 at 00:22
  • 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: http://stackoverflow.com/a/8777264/226201 – Samus_ Jan 09 '12 at 00:26
  • 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. – user1137731 Jan 09 '12 at 00:29
  • 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. – Celada Jan 09 '12 at 00:31
  • 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... – user1137731 Jan 09 '12 at 00:38

2 Answers2

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
Saptamus Prime
  • 228
  • 1
  • 2
  • 7
  • 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. – user1137731 Jan 08 '12 at 23:57
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>
dbeer
  • 6,963
  • 3
  • 31
  • 47