Modellics
Modellics

Reputation: 1

For loop in script with arrays and script/terminal inconsistency

I want to loop through a list of directories (a subset of the directories in a folder) and do operations with them. However, for some reason it is not working. Here is the code where I am just echoing all of them:

#!/bin/bash                                                                                                                                                                   
                                                                                                                                                                              
cd images                                                                                                                                                                     
array=$(ls -d *)                                                                                                                                                              
selection=(${array[@]:1:12})                                                                                                                                                  
cd ..                                                                                                                                                                         
                                                                                                                                                                              
for sub in ${selection[@]}                                                                                                                                                    
do                                                                                                                                                                            
    echo $sub                                                                                                                                                                 
    mkdir $HOME/Projects/PhD/0_project/fMRI/scans/temp/temp_$sub                                                                                                              
done    

The ouptut I get for the echo command is:

04
306
307
3

And the folders: temp_3, temp_04, temp_306, temp_307

HOWEVER, if I run each single line in bash in the termiinal (interactive mode, no script) I do get the correct output for the echo command:

306
307
308
309
310
311
312
314
317
318
323
324

And for the mkdir command: temp_306, temp_307... temp_324

Any idea about this strange and inconsistent behaviour? What am I missing?

Thanks for your help.

Upvotes: 0

Views: 95

Answers (1)

fver1004
fver1004

Reputation: 311

result of $(ls -d *) is a string.
you are slicing a string not an array.

remove :1:12.

#!/bin/bash

cd images || exit
array=(*/) # result is array of subdirectories with / on end of each
selection=("${array[@]%/}") # remove trailing slashes from that array.
selection_sliced=("${selection[@]:1:12}") # if you want to slice.
cd .. || exit

for sub in "${selection_sliced[@]}"
do
    echo "$sub"
    mkdir "$HOME/Projects/PhD/0_project/fMRI/scans/temp/temp_$sub"
done

Upvotes: 1

Related Questions