I'm trying to loop a python script through a bunch of files. The python script looks at two types of files each run: a XX.nii.gz file and an associated XX_aparc aseg.nii.gz file. The XX is a number identifier of the file.
The python script has the following format:
./pythonscript.sh -s XX -t XX.nii.gz -a XX_aparc aseg.nii.gz -o output_dir
. The -s flag creates a folder in the output_dir with the specified name (I would like the name to be the number identifier of the file XX). The -t flag looks at the specified XX.nii.gz file. The -a flag looks at the specified XX_aparc aseg.nii.gz file.
I've tried for f in *.nii.gz; do ./pythonscript.sh -s XX -t XX.nii.gz -a XX_aparc aseg.nii.gz -o output_dir; done
. I know that wouldn't work because of how the files are named, but I can't quite figure out how to specify the values that I would like for each of the flags whilst only iterating through the relevant files. I would like the python script to run on only the XX.nii.gz files whilst specifying the associated XX_aparc aseg.nii.gz file. There are also other types of files and subfolders in the directory related to the execution of the python script that I would not like the python script to look at.
CodePudding user response:
Notice that this has very little to to with either Python or Terminal, and everything to do with the shell you are using, which is probably zsh if you're on a reasonably modern Mac.
In that case, you could do something like
for f in *.nii.gz(:s/.nii.gz//); do
./pythonscript.sh -s $f -t ${f}.nii.gz -a ${f}_aparc aseg.nii.gz -o output_dir
done
This magic uses the :
glob qualifier to invoke the s///
history modifier to delete part of the matched filename. (This is also described in the zshexpn(1) manpage that you can read by running man zshexpn
.)