If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
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
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
Originally posted by rnavanee
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
hello,
can u tell which shell r u using...as this will work fine in bourne and korn shell.
#!/bin/ksh
file='filename'
for i in 1 2 3
do
a=$file$i
echo $a
done