rnavanee
07-19-03, 05:42
| Assume that there are 3 variables $file1, $file2, $file3 I want to access the values of these variables inside a loop for i in 1 2 3 do a=$file$i ... done But this is not working as expected ... Please give me a solution |
View Full Version : shell script - dynamic variables
| Assume that there are 3 variables $file1, $file2, $file3 I want to access the values of these variables inside a loop for i in 1 2 3 do a=$file$i ... done But this is not working as expected ... Please give me a solution |
| oh dear where to start .. I do not believe the previous suggestion will work for you. I could not make it work with the bash shell. if what you are trying to accomplish is simple in that the are always three variables something like the following will work. for I in "$file1" "$file2" "$file3" ; do echo $I ; done If have a more dynamic situation, look into bash shell support of array variables. Try this from a bash shell. If you are using bash 2 it supports array variables try this code snippet. # Define the FILES array FILES[1]="file1" FILES[2]="file2" FILES[4]="file4" echo ${FILES } #Display all elements of the array # show the files of array elements 0-4 I=0 while [ $I -le 4 ] ; do a=${FILES[$I]} echo FILES[$I]=$a I=$(( $I+1 )) done |