bashsyntaxcommand-line-argumentshpc

Passing a string argument to bash causes syntax error


I am trying to run a bash script for rescaling fMRI data using AFNI's 3dcalc.

One of the arguments for this function is "expr" which will "Apply the expression - within quotes - to the input datasets (dnames), one voxel at time, to produce the output dataset". When trying to launch this job, I am met with a syntax error as follows:

-bash: -c: line 0: syntax error near unexpected token `('
-bash: -c: line 0: `apptainer run --cleanenv /Users/user44/fMRI/my_images/afni-binaries.sif 3dcalc -a /Users/user44/fMRI/PPMI/derivatives/afni_blur/sub-3102/sub-3102+tlrc.BRIK -b /Users/user44/fMRI/PPMI/derivatives/afni_scale/sub-3102/sub-3102_desc-scalestats+tlrc.BRIK -c /Users/user44/fMRI/PPMI/derivatives/fmriprep/sub-3102/func/sub-3102_task-rest_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz -expr c*min(200,a/b*100)*step(a)*step(b) -prefix /Users/user44/fMRI/PPMI/derivatives/afni_scale/sub-3102/sub-3102_task-rest_space-MNI152NLin2009cAsym_desc-preprocScaled_bold.nii '

My bash script is attached here:

#!/bin/bash

for fn in /Users/user44/fMRI/PPMI/derivatives/fmriprep/sub*/; do
    id=$(echo $fn | cut -d '/' -f 8)
    qsub -b y -q $QUEUE_NAME -pe orte 8 -l h_vmem=8G -N $id apptainer run --cleanenv ~/fMRI/my_images/afni-binaries.sif 3dcalc \
        -a ~/fMRI/PPMI/derivatives/afni_blur/${id}/${id}+tlrc.BRIK \
        -b ~/fMRI/PPMI/derivatives/afni_scale/${id}/${id}_desc-scalestats+tlrc.BRIK \
        -c ~/fMRI/PPMI/derivatives/fmriprep/${id}/func/${id}_task-rest_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz \
        -expr "c*min(200,a/b*100)*step(a)*step(b)" \
        -prefix ~/fMRI/PPMI/derivatives/afni_scale/${id}/${id}_task-rest_space-MNI152NLin2009cAsym_desc-preprocScaled_bold.nii
    break
done

I have tried replacing the quoted expression with various other syntactical representations, but my bash experience is a bit lacking and I haven't been able to figure out quite what the problem is. Attempted solutions included:

-expr "'c*min(200,a/b*100)*step(a)*step(b)'" \
-expr "\'c*min(200,a/b*100)*step(a)*step(b)\'" \
-expr "$('c*min(200,a/b*100)*step(a)*step(b)')" \

I'm sure I'm violating a very simple syntax rule but would appreciate any help identifying said violation!


Solution

  • qsub concatenates all its arguments into a shell command. The shell treats parenthese specially. You need to escape or quote the () characters in the expr.

            -expr "'c*min(200,a/b*100)*step(a)*step(b)'" \