Passing Shell variable from file to another command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing Shell variable from file to another command
# 1  
Old 02-17-2015
Passing Shell variable from file to another command

Hi all,
I have a file looks like
Code:
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

Code:
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

Last edited by Don Cragun; 02-17-2015 at 03:56 AM.. Reason: Add CODE tags.
# 2  
Old 02-17-2015
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.
Code:
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

Last edited by RavinderSingh13; 02-17-2015 at 03:44 AM.. Reason: Added one more solution
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 02-17-2015
Quick'n'dirty...
Eval is used so be aware!
Hardcoded as 6 variables for simplicity.
Longhand using OSX 10.7.5, default bash terminal.
Code:
#!/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:-
Code:
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> _

# 4  
Old 02-17-2015
Keep it basic:
Code:
while read var1 var2
do
another_command # This will find var1 and var2 in its environment.
done < in_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Passing Shell Variable to awk

Hello All, May i please why my shell variable is not getting passed into awk script. #!/bin/bash -vx i="1EB07C50" /bin/awk -v ID="$i" '/ID/ {match($0,/ID/);print substr($0,RSTART,RLENGTH)}' /var/log/ScriptLogs/keys.13556.txt Thank you. (1 Reply)
Discussion started by: Ariean
1 Replies

2. Shell Programming and Scripting

Passing a variable as input to another shell

I have a shell program that calls another shell program the following code works . chkTimeFormat.sh "10/9/12 17:51:19:783."|read c but when I am passing the the time in a variable like in the code below, the shell chkTimeFormat.sh is not returning proper value time="10/9/12... (9 Replies)
Discussion started by: swayam123
9 Replies

3. Shell Programming and Scripting

Passing a variable to kill command

I have a script that kicks off several processes in the background and stored their pids in a variable as follows: PID_DUMP_TRAN=$PID_DUMP_TRAN" "$! so I then have a list of pids If I echo $PID_DUMP_TRAN I get back a list of pids e.g. 8210 8211 8212 However I then want to kill all these... (5 Replies)
Discussion started by: sjmolloy
5 Replies

4. Shell Programming and Scripting

Passing perl variable to shell command

Can we pass perl variable to shell commands. If yes, please give some example. (2 Replies)
Discussion started by: Anjan1
2 Replies

5. Shell Programming and Scripting

Passing Variable Parameters (C shell)

I am trying to execute a copy command via shell script. However, on occassion, 2 or more files need to copied. How do I code for the multiple arguments? Does it matter how the files are delimited? Example: I have a script to copy files from 1 dir to another called duplicate.csh In most... (1 Reply)
Discussion started by: CKT_newbie88
1 Replies

6. UNIX for Dummies Questions & Answers

Passing a Shell Variable to awk

Hello, I have a file with 4 columns. An arbitrary example is shown below: a Tp 10 xyz b Tq 8 abc c Tp 99 pqr d Tp 44 rst e Tr 98 efg Based on the values in col 2 and col 3, I will execute another program. I have been running this:... (5 Replies)
Discussion started by: Gussifinknottle
5 Replies

7. Shell Programming and Scripting

Shell to Python variable passing

Hi, I had to create a new thread as the old thread had to much of confusion I have two files shashi.sh and py.py I want to pass a variable from shashi.sh to py.py. How do i achieve that ?. shashi.sh export X=12 echo "$("pwd")" echo "$X" exec python py.py "$(X)" py.py... (0 Replies)
Discussion started by: shashi792
0 Replies

8. UNIX for Dummies Questions & Answers

Passing a command in a variable

I need to set up a strange system through which an arbitrary command is sent to a number of different servers (well, actually, VPS accounts). We have a command "vpass" that "passes" a command from the root level to resident VPS accounts. Suppose I wanted each VPS to do some trivial thing, like... (3 Replies)
Discussion started by: treesloth
3 Replies

9. Shell Programming and Scripting

Passing shell variable to NAWK

I am trying to make a simple script in which i take input from shell and then forward the value to nawk (BEGIN). but when i run below mention script so it give no output. echo "Enter TRUNK GROUP:" read TGR cat /omp-data/logs/5etr/080422.APX | nawk -F"|" -v P=$TGR ' BEGIN { TG=P;... (1 Reply)
Discussion started by: wakhan
1 Replies

10. Shell Programming and Scripting

passing value to shell variable

dear all How to passing the parameters or results to outside of the AWK program? anyone can help awk '{print $1}' file1 > $a ? (2 Replies)
Discussion started by: trynew
2 Replies
Login or Register to Ask a Question