How to use conditional OR in MRtrix script

Hi all,
I’m trying to apply a mask to an image, I’m running the following two lines of code in the terminal:
for_each ${pth}/*/preprocessing : mrcalc IN/[0-9]*MPRAGE*n4.nii IN/t1_mask.nii -mult -force IN/t1_brain.nii

for_each ${pth}/*/preprocessing : mrcalc IN/[0-9]*FSPGR*n4.nii IN/t1_mask.nii -mult -force IN/t1_brain.nii

As you can see, the only thing that change is the name of the image I want to match, in some folders the image’s name is MPRAGE and in other is FSPGR, so running the code twice as I did is not a good strategy because I’m getting lots of errors. Thatś why I’d like to add a conditional OR to the first image name and just to do everything in one line of code.

Something like:
for_each ${pth} : mrcalc (image_MPRAGE.nii OR image_FSPGR.nii) mask.nii -mult output.nii



So if it is the case that in each folder there is either only one or the other you can do something like this:

if [ -e image_MPRAGE.nii ]; then IMAGE=image_MPRAGE.nii; else IMAGE=image_FSPGR.nii; fi
This line of code checks if the MPRAGE file exists. If it does, that it sets the $IMAGE variable to that file. It if doesn’t exist, then it assumes that the subject has the FSPGR file. Then you can run the the mrcalc command with mrcalc $IMAGE ..........., and it will use the name of the file that exists. Does this make sense?


Hi @Steven, thanks for your answer.

Well, that’s not working. Sorry, probably I wasn’t clear. The images don’t have the same name, that’s why I’m using the match [0-9]*MPRAGE*n4.nii or [0-9]*FSPGR*n4.nii (it means that the image name starts with numbers, followed by anything, then the MPRAGE followed by anything and it finishes by n4.nii, same for the FSPGR image). So when using what you proposed I don’t know how to use these matches, If I just change it the regular expressions are not interpreted so nothing is found.

And I think a similar problem will happen with the else because if I understand well, that’s not looking for the image, but just assigning the FSPGR name because the MPRAGE image was not found, but the image names always change in every folder (and each folder also has more images that I’m not interested in at this moment), that’s the reason for using the match.

I’m not expert in bash, so I’m a bit lost.
I’ll appreciate your help.


Is it at least unique that the image you are looking for ends in n4.nii, and that the n4.nii suffix is unique to that file in that directory? If so you can just do something like IMAGE=*n4.nii and then run the mrcalc command with $IMAGE as an input.

No, there are more images that ends with n4.nii. The images I want start with numbers, have MPRAGE or FSPGR in the middle and finish with n4.nii. Any of these alone don’t guarantee the uniqueness. I tried IMAGE=[0-9]MPRAGEn4.nii but is not working

okay, then you can try

if [ -e *MPRAGE*n4.nii ]; then IMAGE=*MPRAGE*n4.nii; else IMAGE=*FSPGR*n4.nii; fi

this should work as long as a file containing either MRPAGE or FSPGR and ending with n4.nii is uniquely the file you want to work with.

]I tried this:

for file in ${pth}/*/preprocessing/*
	if [ -e [0-9]*MPRAGE*n4.nii ]; then IMAGE=[0-9]*MPRAGE*n4.nii; else IMAGE=[0-9]*FSPGR*n4.nii; 
	echo $IMAGE

But the echo I’m getting is literally [0-9]*FSPGR*n4.nii
So it’s not working.
Is there a problem in how I am specifying the path?

(I add the [0-9] because the image name starts with numbers, to avoid matching other images that have exactly the same name but starting with the letter c)

  1. If your code is not being run in the directory where the folders are being stored, then you will have to specify the full path to these files, as opposed to just the relative paths which is what is currently there.
  2. Is this the only operation you want to do in this loop? If so, it might be easiest (although not most efficient) to just do two loops, one for file in ${pth}/*/preprocessing/*MPRAGE*n4.nii and the other for file in ${pth}/*/preprocessing/*FSPGR*n4.nii. Then you can check if the first character of the filename is something you want to avoid and skip the file if it is.

Ok, I understand :slight_smile: I think two loops is better.

Thank you so much!!

1 Like