The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reading variable from file variable values sagii Shell Programming and Scripting 5 06-27-2009 01:06 PM
Not access variable outside loop when a reading a file ricardo.ludwig Shell Programming and Scripting 4 05-23-2009 11:14 PM
reading more than one variable into a for loop starsky UNIX for Dummies Questions & Answers 3 10-24-2008 12:01 PM
How to make a loop base on reading a file? JohnBalayo UNIX for Dummies Questions & Answers 3 04-02-2008 06:36 AM
How to make a loop base on reading a file? JohnBalayo HP-UX 3 04-01-2008 11:58 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-20-2009
babusek babusek is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 7
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 ,

Quote:
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"
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.
can any1 help me please.
  #2 (permalink)  
Old 07-20-2009
palsevlohit_123 palsevlohit_123 is offline
Registered User
  
 

Join Date: Aug 2008
Location: India-Chennai
Posts: 120
Updated code below..
Code:
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
Code:
 i Value =5
 Count Value = 15
  #3 (permalink)  
Old 07-20-2009
babusek babusek is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 7
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
  #4 (permalink)  
Old 07-20-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
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.
Code:
#!/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"
  #5 (permalink)  
Old 07-20-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
Did you try this ?
Code:
#!/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
  #6 (permalink)  
Old 07-20-2009
palsevlohit_123 palsevlohit_123 is offline
Registered User
  
 

Join Date: Aug 2008
Location: India-Chennai
Posts: 120
Code:
> 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
  #7 (permalink)  
Old 07-20-2009
babusek babusek is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 7
hii Palse,
sorry the variable values are not updating , let me post the code which i have written ( as u said)
Code:
#!/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 ?

Last edited by babusek; 07-20-2009 at 07:17 AM.. Reason: code tags, PLEASE!
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0