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
how to bypass this and get the for loop running, or
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.