Hello, please assist:
users="test1 test2"
keytest1="abcd"
keytest2="dbcd"
for i in $users
do
echo "$key${i}" > fileout
done
So, my objective is to take the current user (ie test1) in loop and echo its associated keyname (ie keytest1) variable to a file.
The echo redirecting value of keytest* to file.
Thanks
This can be achieved in standard shell (and all its derivates) with
users="test1 test2"
keytest1="abcd"
keytest2="dbcd"
for i in $users
do
eval echo "\${key${i}}"
done
BTW eval - if a part of its arguments is imported - bares a security risk.
If you have bash, you can do
users="test1 test2"
keytest1="abcd"
keytest2="dbcd"
for i in $users
do
key=key${i}
echo "${!key}"
done
Or use arrays
users=(test1 test2)
key=(abcd dbcd)
index=0
for i in ${users[@]}
do
echo "${key[$index]}"
index=$((index+1))
done
Often you can organize your data in pairs,
and the shell can elegantly process them
user_keys="\
test1 abcd
test2 dbcd"
while read i key
do
echo "${key}"
done <<< "$user_keys"
And a standard shell can run this as
user_keys="\
test1 abcd
test2 dbcd"
echo "$user_keys" |
while read i key
do
echo "${key}"
done
I really appreciate the feedback.
Code#2 worked for me but I'm sure the others will also do the trick.
Thanks again!:)