0

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!

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.