rbashslurmsingularity-container

Syntax error in R code executed with Slurm and Singularity


This might be a very stupid question but I can't find a solution. I trying to use srun from SLURM which run in a singularity container a simple R script like so :

project_dir=$HOME/project_directory/
img=$HOME/custom_R_image.img

srun \
    --job-name=job_03 \
    --output="$project_dir/logs/job_03.out" \
    --ntasks=1 \
    --time=01:00:00 \
    --cpus-per-task=2 \
    --mem=8G \
    --error="$project_dir/logs/job_03.err" \
    --mail-type=END \
    --mail-user=user.email@example.com \
    bash -c '
      module load singularity
      singularity exec --bind '"$HOME"' '"$img"' Rscript -e "print('$HOME/project_directory/')"'

this returns me this error :

Error: unexpected '/' in "print(/"
Execution halted

Ideally I would like to do something like that :

singularity exec --bind '"$HOME"' '"$img"' Rscript -e "print($root_dir)"'

and get this result :

[1] "/home/project_directory/"

Solution

  • 1. Variable will be expanded between double-quotes

    And double-quotes between double-quotes could be escaped with a preceding backslash:

    echo "echo \"echo 'hello world'\";cd $HOME"
    
    echo "echo 'hello world'";cd /home/user
    

    Note: Variable $HOME is expanded and both quotes and double-quotes are printed.

    Try to replace last line by this:

    bash -c "module load singularity
      singularity exec --bind '$HOME' '$img' Rscript -e \"print('/home/project_directory/')\""
    

    2. With syntax $'...', you could print both quotes and double-quotes.

    echo $'echo "echo \47hello world\47";cd $HOME'
    
    echo "echo 'hello world'";cd $HOME
    

    Note: Variable $HOME is NOT expanded! But both quotes and double-quotes are still printed.

    Then, maybe something like:

    bash -c 'module load singularity
      singularity exec --bind "'"$HOME"'" "'"$img"$'" Rscript -e "print(\47/home/project_directory/\47)"'