not looping


I feel I am abusing of this message board but I tried to create my batch file to loop through all my participants without success.
My script does create the new output directories and creates in the subject output directory the for the first participant and then stops.

I am copying here the first part and the last part omitting part of the preprocessing to make the reading easier. The preprocessing does work on its own for a single subject. That should not be the problem.

Also if you know of any text editor that helps with coding syntax, I’d appreciate it!

Again, thank you to for the help!


# --------------------------------------------------
# note fixed top-level directories such as  /main/location/of/all/data
  set data_root = /Users/nens.lab/Documents/PEN802_Spring2019/Scans
  set input_root = $data_root/
  set output_root = $data_root/subject_preprocessing

# --------------------------------------------------
# chose one of the two options and comment unwanted option
# get a list of subjects, or just use one (consider $argv)
  cd $input_root
  set subjects = ( sub-* )
  cd -

# or perhaps just process one subject?
# set subjects = ( sub-017 )

# --------------------------------------------------
# process all subjects
  foreach subj_id ( $subjects )
     	  cd $input_root/$subj_id
          set session = (ses-T1) #change here for T1 and T2
	  # --------------------------------------------------
          # note input and output directories
          set subj_indir = $input_root/$subj_id/$session
          set subj_outdir = $output_root/$subj_id/$session

          # --------------------------------------------------
          # if output dir exists, this subject has already been processed
          if ( -d $subj_outdir ) then
             echo "** results dir already exists, skipping subject $subj_id"

          # --------------------------------------------------
          # otherwise create the output directory, write an
          # command to it, and fire it up

          mkdir -p $subj_outdir
          cd $subj_outdir

          # create a run.afni_proc script in the directory where this script is saved
          cat > run.afni_proc << EOF -subj_id $subj_id                          \
              -blocks tshift align tlrc volreg blur mask scale 	  \
              regress 											  \
              -copy_anat $data_root/$subj_id/$session/anat/sub-0??_ses-T?_T1w.nii.gz \
              -dsets                                              \
                  $data_root/$subj_id/$session/func/sub-0??_ses-T?_task-Sub_run-0?_bold.nii.gz \
             ......  			                  \
              -regress_stim_times                                 \
                  $data_root/$subj_id/$session/stim/ETrue.*       \
                  $data_root/$subj_id/$session/stim/HTrue.*       \
                  $data_root/$subj_id/$session/stim/EFalse.*      \
                  $data_root/$subj_id/$session/stim/HFalse.*      \
                  $data_root/$subj_id/$session/stim/control_s.*   \
             -prefix stats.MoreContrasts               \

          # EOF denotes the end of the run.afni_proc command

          # now run the analysis (generate proc and execute)
          tcsh run.afni_proc

# end loop over subjects

Hi, Ilaria-

I think the issue is a tcsh one; long story short-- the “EOF” that ends your “cat << EOF” line cannot have space in front of it-- don’t indent it.

You can see the difference of output in the $output file in each of these two cases-- the first loops through all 10 iterations, whereas the latter doesn’t, and hte only difference is having space to the left of hte EOF:

Case A


set output = TESTFILE_A.txt

# clear any previous content from the $output file
printf "" > $output

foreach ii ( `seq 1 1 10` ) 
cat  << EOF >> $output
    My favorite number now is: ${ii}

Case B


set output = TESTFILE_B.txt

# clear any previous content from the $output file
printf "" > $output

foreach ii ( `seq 1 1 10` ) 
cat  << EOF >> $output
    My favorite number now is: ${ii}

Note that I also changed the order of hte ‘cat << EOF’ and redirecting its output-- in your code, you can still have redirect with overwrite:

cat  << EOF > $output

as opposed to redirect with appending to the file:

cat  << EOF >> $output

… that is a separate issue-- either is programmatically valid, it’s just a question of your preferred behavior for that.


I’m going to add on to this that a simpler copy of the script would be simpler or even simpler take the generated script and use that for each subject just adding a subject ID to the command line for that script. The generated script usually has some lines like this:

the user may specify a single subject to run with

if ( $#argv > 0 ) then
set subj = $argv[1]
else …

That means take the first argument as the subject.

Thank you so much for taking the time to clarify these basic coding questions. It is running and has done 4 participants… finger crossed!