I am working on a CentOS 8.1 cluster running SLURM 20.02.4. I am trying to understand how variables are dereferenced within an sbatch --wrap=
command. I suspect that I may be reaching the limit of usefulness of such --wrap
commands.
This works :
$ X=2
$ sbatch --wrap="for((i=0; i<3; i )); do TMP=\"echo ${X} \$i\"; echo \${TMP}; done"
It outputs :
$ cat slurm-2105323.out
echo 2 0
echo 2 1
echo 2 2
I get this statement, I understand that :
- I have to escape the inline
"
because I'm wrapping my--wrap=
command with"
. The\"
ensures thatsbatch
doesn't prematurely terminate my command. - Dereferencing
X
andi
is different. I can accept this as a consequence of having the need to differentiate between external and internal variables to the--wrap=
command.
Now let's try something more complex. Let's try using backticks to execute a command and assign it to a variable.
$ X=2
$ sbatch --wrap="for((i=0; i<3; i )); do TMP=`echo \"${X} \$i\"`; echo \${TMP}; done"
It outputs
$ cat slurm-2105326.out
2
2
2
Clearly it successfully dereferenced the X
, but not the i
. I've tried various combinations of escape characters, additional $
, and braces. I just can't seem to get it to dereference the i
.
Question :
- How do I dereference the internal variable
i
in the above command where it is executed using backticks within ansbatch --wrap=
statement?
CodePudding user response:
You need to escape the backticks, just like you escaped the quotes and the $
before internal variables. Otherwise they'll be interpreted by the original shell, not the shell run by sbatch
, and the original shell will
$ sbatch --wrap="for((i=0; i<3; i )); do TMP=\`echo \"${X} \$i\"\`; echo \${TMP}; done"
BTW, in modern shell scripting it's preferable to use $()
rather than backticks. It's easier to see, they nest properly (you need to use additional escaping to nest backticks) and readers are less likely to mistake them for quotes. You still need to escape it to make it run in the sbatch
shell.
$ sbatch --wrap="for((i=0; i<3; i )); do TMP=\$(echo \"${X} \$i\"); echo \${TMP}; done"