danilo
danilo

Reputation: 21

cd argument made of variable made of variable

I have a variable that contain a path. Particularly, I need that this variable read the path from a second file (input). That why I am using awk. Variable is something like this:

path_dock1=`awk 'NR==33 {print $1}' input`

In addition to path_dock1, there are other path_dock variable that are declared in the same way.

path_dock1=`awk 'NR==33 {print $1}' input`
path_dock2=`awk 'NR==34 {print $1}' input`
path_dock3=`awk 'NR==35 {print $1}' input`
...

I would like that such variable were used in a for loop. Particularly, I want to use this variable as argument of cd command. To do this, I am using a for loop variable that runs from 1 to n, where n is total number of path_dock variables.

for t in $(seq 1 1000)
do
    cd $path_dock$t
    .... 
done

Of course, this strategy is just an attempt and it doesn't work. Do you have any suggestion to pass a argument to cd command made in this way?

Thank you in advance.

Upvotes: 1

Views: 60

Answers (2)

choroba
choroba

Reputation: 241758

Use an array instead of many numbered variables

path_dock=()
for i in {1..100} ; do
    path_dock+=($(awk 'NR=='$i' {print $1}' input))
done
...
for i in {1..100} ; do
    cd "${path_dock[i]}"
    ...
done

Upvotes: 1

max
max

Reputation: 686

Use parameter expansion:

for t in $(seq 1 1000)
do
    tmp=path_dock${t}
    cd ${!tmp}
    .... 
done

Upvotes: 1

Related Questions