How to get the modified value of variable outside the while loop reading from a file

Hi Friends ,
Sorry if this is a repeated question ,

The input file contains 5 lines , so the the values of the variables i and count should b
i=5;
count=15

but the variables are not updating , the value of variables showing i=0 and count =0 only.:mad:
can any1 help me please.

Updated code below..

count=0
i=0
while read line
do
i=`expr "$i" + 1`
count=`expr "$count" + "$i"`
done < input.txt
echo " i Value =$i "
echo " Count Value = $count"

Output

 i Value =5
 Count Value = 15

count= `expr $count + $i`
=>
count=`expr $count + $i`

Setting variable must the whole set to be in 1st argument.

Which shell you are using ?
If you have not old bsh, then you can write without using external expr.
Always add the correct shell to the 1st line in script, no need to take care of your interactive "keyboard" shell and for us it's easier to give solution.

#!/bin/ksh
count=0
i=0
while read line
do
     (( i=i+1 ))    # or  (( i+=1 ))
     (( count = count + i ))
done < input.txt

echo " i Value =$i "
echo " Count Value = $count" 

Hi Palse,
Thanks For u r reply , i tried the same but the variables are not updated.
i value = 0
count value = 0

and iam using the bash shell

Did you try this ?

#!/bin/bash
count=0
i=0
while read line
do
     (( i=i+1 ))    # or  (( i+=1 ))
     (( count = count + i ))
done < input.txt

echo " i Value =$i "
echo " Count Value = $count"
echo "my input file:"
cat input.txt
> cat temp.bash
count=0
i=0
while read line
do
i=`expr "$i" + 1`
count=`expr "$count" + "$i"`
done < input.txt
echo " i Value =$i "
echo " Count Value = $count"

 
> bash temp.bash
 i Value =5
 Count Value = 15

Sorry, i able to execute the same code in bash shell too.
Note : Did you given the variables with in double quote, Pls check

hii Palse,
sorry the variable values are not updating , let me post the code which i have written ( as u said)

#!/bin/bash
count=0
 i=0

 while read line
 do
   i=`expr "$i" + 1`
   count=`expr "$count" + "$i"`
   done < input.txt
echo " i value = $i";
echo " count value = $count";

and output is :
-bash-3.00$ sh readLine.sh
i value = 0
count value = 0

What do u say ?

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

chmod a+rx  readLine.sh

and execute it.
When you say
sh script, you will use sh, not script 1st line shell, and it looks that your sh is something, not bash or ksh.

If you like to run using bash then call
bash readLine.sh

Two method to run script:

  1. add exucute priviledges for script file and 1st line include the interpreter path and excute syntax (sh, bash, bash, ksh, awk, perl, php, ...)
chmod a+rx somescript

1st line:

#!/usr/bin/some_interpreter

ex. to run:
./somescript

  1. method is to start interpreter and give commands in textfile, commandline syntax is not same for every interpreter, but for shells it's always:
interpreter inputfile

ex. bash bashscript

Thanks dude.
check this

my shell is bash

    echo $SHELL
    /bin/bash

so iam using bash shell , and u said i changed the permissions to the file as a+x , still the output not showing the updated variables .
but when i run the script with

"bash readLine.sh"

It is showing the correct output .

correct me if i'm wrong.

---------- Post updated 07-21-09 at 01:04 PM ---------- Previous update was 07-20-09 at 04:57 PM ----------

Thanks Dude

i use bash and your original script works fine. are you sure "input.txt" contains lines? or does it even exists?

-bash-3.2$ ./test
 i Value =5
 Count Value = 15
-bash-3.2$ cat test
count=0
i=0
while read line
do
i=`expr $i + 1`
count=`expr $count + $i`
done < input.txt

echo " i Value =$i "
echo " Count Value = $count"
-bash-3.2$ ./test
 i Value =5
 Count Value = 15
-bash-3.2$

Thanks buddy,
ya input.txt has lines , and u r code working fine ..
Thanks a lot guys .. keep the spirit