Home > Blockchain >  Understanding internal variable substitution with backticks in an sbatch --wrap statement
Understanding internal variable substitution with backticks in an sbatch --wrap statement

Time:09-21

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 :

  1. I have to escape the inline " because I'm wrapping my --wrap= command with ". The \" ensures that sbatch doesn't prematurely terminate my command.
  2. Dereferencing X and i 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 :

  1. How do I dereference the internal variable i in the above command where it is executed using backticks within an sbatch --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"
  • Related