Reputation: 2069
Below is the sample code,
top_dir=(network_dir telecomm_dir)
network_dir=(dijkstra patricia)
telecomm_dir=(CRC32 FFT adpcm gsm)
for bench in ${top_dir[@]}; do
for subdir in ${$bench[@]}; do
make -C $subdir
done
done
What I have is two directories, each of them has subdirectories. I want to iterate each directory and do make
. When I run this script, it give me the error message,
./run.sh: line 21: ${$bench[@]}: bad substitution
Is it possible to let bash use variable ''bench'' to access network_dir and telecomm_dir? Thanks!
Upvotes: 0
Views: 72
Reputation: 17614
one option is to use dereferencing
top_dir=(network_dir telecomm_dir)
network_dir=(dijkstra patricia)
telecomm_dir=(CRC32 FFT adpcm gsm)
for bench in "${top_dir[@]}"; do
arr="${!bench}"
for subdir in "${arr[@]}"; do
make -C "$subdir"
done
done
also use more quotes
another option is to use associative arrays:
declare -A dirs=(
[network_dir]="dijkstra patricia"
[telecomm_dir]="CRC32 FFT adpcm gsm"
)
for bench in "${!dirs[@]}"; do
for subdir in ${dirs[$bench]}; do # requires subdirs with no spaces
make -C "$subdir"
done
done
Upvotes: 1