Automatic variable assignment inside a for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Automatic variable assignment inside a for loop
# 1  
Old 04-26-2011
Question Automatic variable assignment inside a for loop

Code:
cs1=`echo "scale=8;($css1/$css0)*100"|bc`
cs2=`echo "scale=8;($css2/$css0)*100"|bc`
cs3=`echo "scale=8;($css3/$css0)*100"|bc`
cs4=`echo "scale=8;($css4/$css0)*100"|bc`
cs5=`echo "scale=8;($css5/$css0)*100"|bc`
cs6=`echo "scale=8;($css6/$css0)*100"|bc`
cs7=`echo "scale=8;($css7/$css0)*100"|bc`
cs8=`echo "scale=8;($css8/$css0)*100"|bc`
cs9=`echo "scale=8;($css9/$css0)*100"|bc`

Instead of using the above code, if I use the below to reduce the script size, it is not working. Please help

Code:
for i in 1 2 3 4 5 6 7 8 9
do
eval cs$i=`echo "scale=8;($css'$i'/$css0)*100"|bc`
done

# 2  
Old 04-26-2011
in your for loop try to replace with the line (there are 6 simple quote,2 back quote and 2 double quote... don't forget some...)

Code:
eval 'cs'$i'=`echo "scale=8;($css'$i'/$css0)*100"|bc`'

Otherwise
Code:
for i in 1 2 3 4 5 6 7 8 9
do
t=$(eval echo "\(\$css$i/\$css0\)*100")
eval cs$i=`echo "scale=8;$t"|bc`
done


Last edited by ctsgnb; 04-28-2011 at 11:50 AM..
# 3  
Old 04-26-2011
Since you can't eval in eval:
Code:
for i in {1..8}
do
    tmp="cs$i=\$(echo \"scale=8;(\$css$i/$css0)*100\"|bc)"
    eval $tmp
    tmp2="echo cs$i is \$cs$i"
    eval $tmp2
done

# 4  
Old 04-26-2011
I tested & fixed my previous post, use simple quoting for the one line eval :

Give a try to the following :

Code:
for i in 1 2 3 4 5 6 7 8 9
do
eval 'cs'$i'=`echo "scale=8;($css'$i'/$css0)*100"|bc`'
done

(don't forget any of the 6 simple quote)

Last edited by ctsgnb; 04-28-2011 at 11:52 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Variable inside while loop got reset

hi, I am using hp unix server and not getting variable output present inside the while loop. I have tried changing the code and need to verify whether it is proper practice of code. I am expecting the output of varible RUN_FILE 3 to TRUE which i get inside the while loop. RUN_FILE 1=TRUE... (8 Replies)
Discussion started by: gowthamsoft
8 Replies

2. UNIX for Beginners Questions & Answers

Variable and awk inside for loop

Thanks all for taking time out and reading this thread and big Thanks to all who have come forward for rescue. Background: I have a variable "nbrofcols" that has number of columns from a data file. Now, using this count in for loop, I am trying to get the maximum length of each column present... (7 Replies)
Discussion started by: svks1985
7 Replies

3. Shell Programming and Scripting

Variable assignment inside awk

Hi, Was hoping someone could help with the following: while read line; do pntadm -P $line | awk '{if (( $2 == 00 && $1 != 00 ) || ( $2 == 04 )) print $3,$5}'; done < /tmp/subnet_list Anyone know if it is possible to assign $3 and $5 to separate variables within the {} brackets? Thanks... (14 Replies)
Discussion started by: CiCa
14 Replies

4. Shell Programming and Scripting

Use one loop get the variable assignment

for var in {1..3} do lspci -nn |awk -v i=$var '/8086:10a7/{split($1,a,""); print "0x"a}' done it will output 0x01 0x00 0x1 I want to let bus=0x01 slot=0x00 fun=0x1, How to modify this loop? I know it can be get by bus=`lspci -nn |awk... (4 Replies)
Discussion started by: yanglei_fage
4 Replies

5. Shell Programming and Scripting

awk variable assignment-- inside braces or not?

So, in awk, I've always put my variable assignments inside of the curly braces, just like dad, and grandpa, and the 26 generations before them. But today I came upon an awk statement that had them outside the braces-- blasphemy! Seriously, though, is there any practical difference? I was... (3 Replies)
Discussion started by: treesloth
3 Replies

6. Shell Programming and Scripting

How to give a variable output name in a shell script inside a for loop

Hi all I run my program prog.c in the following way : $ ./prog 1 > output.txt where 1 is a user defined initial value used by the program. But now I want to run it for many a thousand initial values, 1-1000, and store all the outputs in different files. Like $ ./prog 1... (1 Reply)
Discussion started by: alice06
1 Replies

7. Shell Programming and Scripting

how to pass a variable to an update sql statement inside a loop

hi all, i am experiencing an error which i think an incorrect syntax for the where clause passing a variable was given. under is my code. sqlplus -s ${USERNAME}/${PASSWORD}@${SID} << END1 >> $LOGFILE whenever sqlerror exit set serveroutput on size 1000000 declare l_rc ... (0 Replies)
Discussion started by: ryukishin_17
0 Replies

8. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

9. Shell Programming and Scripting

Variable assignment from a for loop values.

Guys I am trying to assignthe values to variables from a for loop. s1:/tmp> for i in `cat test` > do > echo $i > done Sdosanjh 2 6 Now, I want is NAME=Sdosanjh CURRENT=2 SPECIFIED=6 there are multiple lines in the "test" file. So next time when for loop picks values from next... (1 Reply)
Discussion started by: sdosanjh
1 Replies

10. IP Networking

automatic ip assignment

hello guyz, I am working on project of automatic ip adress assignment to a local network when I send a packet with Mac adress I want to get a reply from the other machine with its ip address ,so how can I achieve that? (2 Replies)
Discussion started by: netsavy
2 Replies
Login or Register to Ask a Question