foor loop is not working while assigning to variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting foor loop is not working while assigning to variable
# 1  
Old 10-23-2008
foor loop is not working while assigning to variable

Hi All,

While executing following for loop in unix, it is working fine.

sum=0
tot_amt=`for i in `cat amt`
do
sum=`echo "$sum + $i"|bc`
done`

But if i assing this loop in a variable it is not working. The error is so and done is unexpected.

tot_val=`sum=0;tot_amt=`for i in `cat amt` do sum=`echo "$sum + $i"|bc` done`

can someone tell me where i have made an mistake. i think it is with " ` ".

Regards,

Amit
# 2  
Old 10-23-2008
sorry..

tot_val=`sum=0; for i in `cat amt` do sum=`echo "$sum + $i"|bc` done`

this is not working...
# 3  
Old 10-23-2008
Without really looking into this at all...have you tried

Code:
tot_val=`sum=0;tot_amt=`for i in $(cat amt) do sum=`echo "$sum + $i"|bc` done`

# 4  
Old 10-23-2008
Hi Deco

tot_val=`sum=0;tot_amt=`for i in $(cat amt) do sum=`echo "$sum + $i"|bc` done``

it says teletype mismatch. So i have declare the variable before for loop.

tot_val=`sum=0;tot_amt=0;tot_amt=`for i in $(cat amt) do sum=`echo "$sum + $i"|bc` done``

It says: command i not found. I do not know wht it is taking i as a command.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning value to a variable

Unable to get the value to a variable. set -x cd $HOME echo "Enter the server name" read a echo $a i=4 j=1 k = ps -ef | awk '/server1/{ print $4 }' | tail -$i | head -$j` echo $k When I do the same in command line it works, however the same does not work when I provide that in the... (1 Reply)
Discussion started by: venkidhadha
1 Replies

2. Shell Programming and Scripting

Assigning values to 2 variables within for loop

Hi All, Is it possible to grep for two files and assign their names to two separate variables with for loop? I am doing the below currently: if then for fname in $( cd $dirA ; ls -tr | grep "^Ucountry_file$") do InFile=$dirA/$fname ... (4 Replies)
Discussion started by: swasid
4 Replies

3. Shell Programming and Scripting

[Solved] Assigning a value to a variable name then running a loop on these values

Hi, I was wondering if anyone could assist me for (what is probably) a very straightforward answer. I have input files containing something like File 1 Apples Apples Apples Apples File 2 Bananas Bananas Bananas Bananas (4 Replies)
Discussion started by: hubleo
4 Replies

4. Shell Programming and Scripting

problem in assigning value to variable have value fo other variable

my script is some thing like this i11="{1,2,3,4,5,6,7,8,9,10,11,}" echo "enter value" read value ..............suppose i11 x="$value" echo "$($value)" .............the echo should be {1,2,3,4,5,6,7,8,9,10,11,} but its showing "i11" only. plz help me out to get desired... (10 Replies)
Discussion started by: sagar_1986
10 Replies

5. Solaris

For loop variable not assigning

Hi , i am using below code print the PATH value inside for loop but it's printing just PATHD & not the original value which i defined. #!/usr/bin/bash PATHD1=/spp/sp/LIVE export PATHD1 PATHD2=/spp/sp/TEST export PATHD2 j=2 for (( i = 1 ; i <= j ; i++ )) do PATH_DIR=PATH$i echo PATHD$i... (2 Replies)
Discussion started by: panneer76
2 Replies

6. Shell Programming and Scripting

For Loop and assigning Value using Sed

Hi, Please help me writing for loop to fetch data and store in variable from a text file which contains data as below: 12 46 56 5466 111 There are 40 lines of data...!!!! I jus need all data from line no 4 to 40 ie data for 4,6,8......40. (2 Replies)
Discussion started by: fidelis
2 Replies

7. Shell Programming and Scripting

Removing a character from a variable and assigning it to another variable?

Hi folks. I have this variable called FirstIN that contains something like this: 001,002,003,004... I am trying to assign the content of this variable into ModifiedIN but with the following format : 001 002 003 004...(changing the commas for spaces) I thought about using sed but i am not... (17 Replies)
Discussion started by: Stephan
17 Replies

8. Shell Programming and Scripting

retreiving and assigning values and manipulating string in a for loop

Hi I am new to shell scripting and i am preparing a script. for now i am work on a sub part of it..but i am unable to make it work. --- the test code that i am working on -------------------------- IFS="" Sample_eve=`psg proc_s | grep tY` n=0 for line in $Sample_eve... (41 Replies)
Discussion started by: Anteus
41 Replies

9. Shell Programming and Scripting

Assigning values to an array via for/while loop

I need to do something like this: for i in 1 2 3 4 5; do arr=$(awk 'NR="$i" { print $2 }' file_with_5_records) done That is, parse a file and assign values to an array in an ascending order relative to the number of record in the file that is being processed on each loop. Is my... (2 Replies)
Discussion started by: fiori_musicali
2 Replies

10. Shell Programming and Scripting

Assigning inside for loop

If I have 3 variables and I want to check if any of these is null. If one of them is null then it should be assigned a value of 0.I have the following code below. The output should be 0 is A, 11 is B, 33 is C $a= $b=11 $c=33 $echo $a $b $c $11 33 for i in "${a}" "${b}" "${c}"; do ... (2 Replies)
Discussion started by: thana
2 Replies
Login or Register to Ask a Question