Hi all,
I have a file looks like
AAAA 111
BBBB 222
CCCC 333
need to pass variable value like var1=AAAA
and var2=111
to another command for three times with next values.
stuck over here
cat file | while read line
do
export var1=`awk '{print $1}'`
echo $var1
export var2=`cat file |awk '{print $2}'`
echo $var2
--------
for i in file
command (value of var1) (value of var2)
done
--------
done
HELP
Hello Rakesh,
Following may help you in same. Please use code tags for commands/code/Inputs you use in your posts as per forum rules.
while read var1 var2
do
First_var=$var1;
Second_var=$var2;
echo "First Var: " $First_var;
echo "Second Var: " $Second_var;
done < "Input_file"
OR
while read var1 var2
do
echo "First Var: " $var1;
echo "Second Var: " $var2;
done < "Input_file"
You can take variables and do operations as per your need in place of printing them(which I have done for an example).
Hope this helps.
Thanks,
R. Singh
1 Like
Quick'n'dirty...
Eval is used so be aware!
Hardcoded as 6 variables for simplicity.
Longhand using OSX 10.7.5, default bash terminal.
#!/bin/bash
echo 'AAAA 111
BBBB 222
CCCC 333' > /tmp/text
count=0
text=( `cat /tmp/text` )
while [ $count -lt 6 ]
do
eval var$count="${text[$count]}"
count=$((count+1))
done
echo "$var0 $var1 $var2 $var3 $var4 $var5"
Results:-
Last login: Tue Feb 17 08:19:45 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> chmod 755 variables.sh
AMIGA:barrywalker~/Desktop/Code/Shell> ./variables.sh
AAAA 111 BBBB 222 CCCC 333
AMIGA:barrywalker~/Desktop/Code/Shell> _
Keep it basic:
while read var1 var2
do
another_command # This will find var1 and var2 in its environment.
done < in_file