0

I want to convert images from .img to .nii.gz using the function fslchfiletype.

These images are stored in Controls and Patients folders, each one of this folders have several folders belonging to each one of the individuals, like C01, C02, , etc. Specifically, each one of the individuals has the .img files inside a folder called rs_roi, which is inside another folder called ROIS2. This is what I have:

DIR="/media/Roy"; cd "$DIR/Analysis"
for group in Controls Patients; do
    for case in $group; do
        mkdir $DIR/Analysis/$group/$case/Cortical_masks 
                
        for file in $DIR/Analysis/$group/$case/ROIS2/rs_roi/.img; do
        fslchfiletype NIFTI_GZ "$file"
        done;
    done;
done;       

Notice how I also want to create a folder called Cortiical_maks inside each and one of the individuals.

This gives me the next error:

mkdir: cannot create directory ´media/Roy/Analysis/Controls/Controls/Cortical_masks´: No such file or directory.
Cannot open volume media/Roy/Analysis/Controls/Controls/ROIS2/rs_roi/ for reading!
mkdir: cannot create directory ´media/Roy/Analysis/Patients/Patients/Cortical_masks´: No such file or directory.
Cannot open volume media/Roy/Analysis/Patients/Patients/ROIS2/rs_roi/ for reading!

It´s iterating two times the Controls Patients folder: Control/Control. Maybe the problem is here for case in $group; do? Thx

c69
  • 19,951
  • 7
  • 52
  • 82
RoyBatty
  • 107
  • 3
  • DON'T use the `script` tag. Every SO question is about scripts one way or another. It's no better than adding `code` as a tag. That tag is just noise, it was deleted in the past and will be deleted very soon again. – Panagiotis Kanavos Apr 20 '21 at 15:47

2 Answers2

0

Once you have your directory name assigned to a variable, you have to glob the directory to get its contents. Otherwise, as you saw, $group is not expanded, and you're only looping over that single term itself.

You might also like to check that each entry is indeed a directory before you traverse into it.

for case in $group/*; do
    [ -d $case ] || continue
    mkdir $DIR/Analysis/$group/$case/Cortical_masks 
            
    for file in $DIR/Analysis/$group/$case/ROIS2/rs_roi/.img; do
    fslchfiletype NIFTI_GZ "$file"
    done;
done;

You should probably also quote your variables in more places, unless you are sure they don't need quoting. I always lint my scripts with shellcheck to get that kind of advice.

Noah
  • 417
  • 1
  • 5
  • 10
  • Thx for the answer, it´s been useful. However bash was still iterating two times over `$group`, so I deleted it in both path functions. I wrote an answer with a script that works fine. – RoyBatty Apr 18 '21 at 15:14
0

I made a small change to @Noah answer, this script works fine:

for group in Controls Patients; do
    for case in $group/*; do
        [ -d $case ] || continue
        mkdir $DIR/Analysis/$case/Cortical_masks 
            
        for file in $DIR/Analysis/$case/ROIS2/rs_roi/*.img; do
        fslchfiletype NIFTI_GZ "$file"
        done;
    done;
done;   

I deleted $group in both paths, and added * in *.img which i forgot.

RoyBatty
  • 107
  • 3
  • [When to wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Apr 21 '21 at 03:36