0

I am trying to for-loop through a list of images in a folder to apply a thresholding using fsl maths. However, I have been stuck on the for-loop for some hours and have not found a solution (that I could understand, being a beginner in linux).

I am wondering if someone could provide some help so I could learn a bit more of basic bash scripting through this problem.

The problem seems to be due to spaces in the filename, so I am wondering

  1. how to bypass this and get the for loop running, or

  2. as an alternative, how to rename all files in a folder to eliminate spaces?

Both would be useful things I want to learn.

I have been trying something like:

FILES=$(ls *.nii)
echo "$FILES" > FILES.txt

for image in `cat FILES.txt`; do
echo $image
fslmaths $ELEMENT -thr 0.5 -bin.
done

However, I can already see on the echo $image that the files are not being presented properly, and that a filename will spread over multiple lines, possibly because of spaces in the filename.

ls *nii

or

cat FILES.txt

both give me the list I want, looking like this:

CIT168toMNI152_prob_atlas_bilat_1mm__(volume 8).nii
harvardoxford-cortical_prob_Postcentral Gyrus.nii
harvardoxford-cortical_prob_Precuneous_Cortex.nii
juelich_prob_GM Primary somatosensory cortex BA1 L.nii
juelich_prob_GM Primary somatosensory cortex BA1 R.nii
juelich_prob_GM Primary somatosensory cortex BA2 L.nii
juelich_prob_GM Primary somatosensory cortex BA2 R.nii
juelich_prob_GM Primary somatosensory cortex BA3a L.nii
juelich_prob_GM Primary somatosensory cortex BA3a R.nii
juelich_prob_GM Primary somatosensory cortex BA3b L.nii
juelich_prob_GM Primary somatosensory cortex BA3b R.nii
juelich_prob_Left_Premotor_BA6.nii
juelich_prob_Left_Primary_motor_BA4a.nii
juelich_prob_Left_Primary_motor_BA4p.nii
juelich_prob_Right_Premotor_BA6.nii
juelich_prob_Right_Primary_motor_BA4a.nii
juelich_prob_Right_Primary_motor_BA4p.nii
mni_prob_Insula.nii
red_nucleus.nii
talairach_label_Left_Brainstem_Midbrain.nii
talairach_label_Left_Brainstem_Pons.nii
talairach_label_Left_Brainstem_Red_Nucleus.nii
talairach_label_Left Cerebrum.Frontal Lobe.Paracentral Lobule.Gray Matter.Brodmann area 5.nii
talairach_label_Left_Lateral_Dorsal_Nucleus.nii
talairach_label_Left_Lateral_Posterior_Nucleus.nii
talairach_label_Left_Ventral_Lateral_Nucleus.nii
talairach_label_Right_Brainstem_Midbrain.nii
talairach_label_Right_Brainstem_Pons.nii
talairach_label_Right Cerebrum.Frontal Lobe.Paracentral Lobule.Gray Matter.Brodmann area 5.nii
talairach_label_Right_Red_Nucleus.nii
talairach_label_Right_Thalamus_Lateral_Dorsal_Nucleus.nii
talairach_label_Right_Thalamus_Lateral_Posterior_Nucleus.nii
talairach_label_Right_Thalamus_Ventral_Lateral_Nucleus.nii

However, when I run the for-loop

for image in `cat FILES.txt`; do
echo $image
done

I get something like this (shortened here) :

juelich_prob_GM
Primary
somatosensory
cortex
BA1
L.nii
juelich_prob_GM
Primary
somatosensory
cortex
BA1
R.nii
juelich_prob_GM
Primary
somatosensory
cortex
BA2
L.nii
juelich_prob_GM
Primary
somatosensory
cortex
BA2
R.nii
juelich_prob_GM
Primary
somatosensory
cortex
BA3a
L.nii
juelich_prob_GM
Primary
somatosensory
cortex
BA3a
R.nii
juelich_prob_GM
Primary
somatosensory
cortex
BA3b
L.nii
juelich_prob_GM
Primary
somatosensory
cortex
BA3b
R.nii
juelich_prob_Left_Premotor_BA6.nii
juelich_prob_Left_Primary_motor_BA4a.nii
juelich_prob_Left_Primary_motor_BA4p.nii
juelich_prob_Right_Premotor_BA6.nii
juelich_prob_Right_Primary_motor_BA4a.nii
juelich_prob_Right_Primary_motor_BA4p.nii

I have also tried:

echo "${FILES[*]}"

which gives me the list I want, but when I for loop it:

for ELEMENT in "${FILES[@]}"
do
echo $ELEMENT
fslmaths $ELEMENT -thr 0.5 -bin
done

It will not echo the whole filename, only the part until a space.

I'd really appreciate any help.

AndreasV
  • 1
  • 1
  • 4
    Btw.: [Why *not* parse `ls`?](http://unix.stackexchange.com/questions/128985/why-not-parse-ls) – Cyrus Dec 19 '21 at 10:16
  • 3
    A correct method is `for file in ./*.nii; do fslmaths "$file" -thr 0.5 -bin; done`. You may consider reading [Bash Pitfalls #1](https://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29) – M. Nejat Aydin Dec 19 '21 at 10:30
  • For more options, read [BashFAQ #20: "How can I find and safely handle file names containing newlines, spaces or both?"](http://mywiki.wooledge.org/BashFAQ/020) Also, you should almost always double-quote variable references. [shellcheck.net](https://www.shellcheck.net/) is good at pointing out this mistake (and a bunch of other common scripting errors). – Gordon Davisson Dec 19 '21 at 17:46

1 Answers1

0

Just use find

find . -name '*.nil' -print -exec fslmaths '{}' -thr 0.5 -bin. \;
kirjosieppo
  • 617
  • 3
  • 16