how to shift array value in bash
To answer the question in the title, you can "shift" an array with the substring/subarray notation. (shift
itself works with just the positional parameters.)
$ a=(a b c d e)
$ a=("${a[@]:1}")
$ echo "${a[@]}"
b c d e
Similarly, to 'pop' the last item off the array: a=("${a[@]:0:${#a[@]} - 1}" )
or unset "a[${#a[@]}-1]"
So if you wanted to, you could do this:
a=(foo bar doo)
b=(123 456 789)
while [ "${#a[@]}" -gt 0 ] ; do
echo "$a $b"
a=("${a[@]:1}")
b=("${b[@]:1}")
done
But it trashes the arrays, so just indexing as usual might be better. Or maybe use an associative array instead:
declare -A arr=([foo]=123 [bar]=456 [doo]=789)
A general remark. It does not make sense to define an array like this:
folder_mount_point_list="sdb sdc sdd sde sdf sdg"
folderArray=( $folder_mount_point_list )
You would do this instead:
folderArray=(sdb sdc sdd sde sdf sdg)
Now to your question:
set -- sdb sdc sdd sde sdf sdg
for folder_name; do
mkdir "/data/$folder_name"
done
or
set -- sdb sdc sdd sde sdf sdg
while [ $# -gt 0 ]; do
mkdir "/data/$1"
shift
done
You can simply loop over all values, no shifting needed:
folderArray=(sdb sdc sdd sde sdf sdg)
for folder in "${folderArray[@]}"; do
mkdir "/data/$folder"
done