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"
#!/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
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.
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 ----------
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$