linux - Bash Shell Script Inner loop of Nested Loops not working -
in attempting rename set of files variations of (1-26), left/right , png/bmp have following script
#!/bin/bash number_list=$(seq -f "%03g" 1 26) ext_list=("png" "bmp") side_list=("right" "left") number in $number_list side in $side_list ext in $ext_list old="${side}_${number}.${ext}" new="${number}_${side}.${ext}" mv $old $new done done done
only files designated right , bmp renamed. looks iterating through outermost loop, , not iterating through inner loops (just uses first element).
i've looked elsewhere on net having trouble finding relevant.
any guesses might wrong?
thanks, jeff
your first assignment
number_list=$(seq -f "%03g" 1 26)
assigns string created output of seq
number_list
. next 2 assignments
ext_list=("png" "bmp") side_list=("right" "left")
create arrays. array variable, $ext_list
equivalent ${ext_list[0]}
, is, it's first element of array. iterate on values of array, use
for ext in "${ext_list[@]}"
note outermost loop works because iterating on whitespace-separated strings stored in number_list
. is, if number_list
0 1 2 3
, number
set in order 0, 1, 2, 3. same apply faulty attempt @ loop. consider
ext_list=("png bmp" "gif tif") ext in $ext_list;
ext_list
expand png bmp
, , since expansion not quoted, ext
assigned png
, bmp
. if incorrectly wrote
for ext in ${ext_list[@]};
then ext
take png
, bmp
, gif
, tif
values. equally incorrect
for ext in "$ext_list";
would produce single iteration, ext
assigned value png bmp gif tif
.
Comments
Post a Comment