shell script Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script Variable
# 1  
Old 08-07-2008
Bug shell script Variable

hi all,

i want use the variable value as a new variable name. And i want to use this new variable value


for i in COMPUTER1 COMPUTER2
do
flag_name=${i}_FLAG
eval ${flag_name}=123
echo $i'_FLAG'
done

output is
COMPUTER1_FLAG
COMPUTER2_FLAG

i need output as 123
# 2  
Old 08-07-2008
You need an eval around the echo too. And a literal dollar sign in front of the variable.

Code:
eval echo \$${i}_FLAG

It will print 123 twice, of course, when the loop runs twice.
# 3  
Old 08-07-2008
Its Working. Thank U Smilie
# 4  
Old 08-07-2008
having problem with if condiation

hi era,

your given code (eval echo \$${i}_FLAG) is woking fine.

now i want to use the variable value to in if condiation..
is following code is right. please help


for i in COMPUTER1 COMPUTER2
do
flag_name=${i}_FLAG
eval ${flag_name}=1
if [ `eval echo \$${i}_FLAG` -eq 1 ]
then
echo yes
else
echo no
fi
done
# 5  
Old 08-07-2008
With the backticks, you will be requiring even more backslashes. But I'd recommend against using backticks, and "if test -eq".

Code:
eval 'case $'${i}'_FLAG in 1) echo yes;; *) echo no;; esac'


Last edited by era; 08-07-2008 at 03:24 AM.. Reason: Oops, fix quoting
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

3. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

4. Shell Programming and Scripting

How to read * in a variable in shell script??

hi, i have a text file which conatins some fields delimited by space. some fields contains * as entries. cron_file.txt 0 * * * * 0 3 * * * i want to read each line 1 by 1 and store each field in seperate variables n a shell script. i am unable to read the field that contains a *. how... (3 Replies)
Discussion started by: Little
3 Replies

5. Shell Programming and Scripting

Passing variable from shell script to python script

I have a shell script main.sh which inturn call the python script ofdm.py, I want to pass two variables from shell script to python script for its execution. How do i achieve this ????? Eg: main.sh a=3 b=3; c= a+b exec python ofdm.py ofdm.py d=c+a Thanks in Anticipation (4 Replies)
Discussion started by: shashi792
4 Replies

6. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

7. Shell Programming and Scripting

Help Needed with Shell Script Variable

ok, so, i want to do something like this where i store results of codings in a variable, but when i run it, it doesn' twork: example HOT=34 BOSS=`john=133 if ; then ADDIT=`expr 8 + 6` case $HOT in 34) expr 4 + 5 ;; esac fi` it seems shell scripts have a problem with having ``... (2 Replies)
Discussion started by: SkySmart
2 Replies

8. Shell Programming and Scripting

How to return a value of a variable from shell script to perl script

HI , Is there any way to return a value of variable from shell to perl script. Code: === Perl file my $diff1=system("sh diff.sh"); my $diff2=system("sh diff1.sh"); I need exit status of below commands i.e 0 and 1 respectively. Since in both the cases diff is working so system... (3 Replies)
Discussion started by: srkelect
3 Replies

9. Shell Programming and Scripting

transfer a variable in a shell script?

Hey guys, seems I can't transfer the $ip into gawk in the following bash script, where the problem is? #!/bin/bash while read ip do gawk '$1~"$ip"' /tmp/source done < /tmp/ip ++ cat /tmp/ip 192.16.1.1|192.168.1.2|192.168.1.3 10.1.1.1|10.1.1.2 10.8.1.0|10.8.1.1|10.8.1.2 -- cat... (11 Replies)
Discussion started by: fedora
11 Replies

10. UNIX for Dummies Questions & Answers

How to Pass variable to shell Script

Hi , i am beginner to Unix, I have one small script which execute java programme,it has java command input output structure . Right now i have given Input output structure manually that is on same directory, now how can i pass that by commandline #!/bin/sh java Classjava input.txt... (5 Replies)
Discussion started by: sam70
5 Replies
Login or Register to Ask a Question