LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-12-2002, 10:50 AM   #1
CBenner
LQ Newbie
 
Registered: Jun 2002
Location: USA - Maryland
Posts: 3

Rep: Reputation: 0
Shell variable in Linux is reset after while loop


Recently I had to move some shell scripts from and AIX box to a Linux box and some of the code is giving giving me different results. Specifically, a variable incremented inside of a while loop reverted back to the value that it was set to before the loop. In AIX the value of the variable after the loop remained the highest value it had achived within the loop. I have provided an example below; it is not the actual code. Any assistance would be appreciated.

#!/bin/ksh
ls -l * 2>/dev/null | awk '{print $9}' > /tmp/dirlist$$
lcount=0
echo "Info: $lcount"
cat /tmp/dirlist$$ | while read filename
do
let lcount=$lcount+1
echo "Info: $lcount $filename"
done
echo "Info: $lcount"
rm /tmp/dirlist$$
exit
---------
The final "echo" displays a value of 0 in Linux, and the "correct" value in AIX.

Curt...
 
Old 06-12-2002, 08:35 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
I pasted your example into a file and executed it on my slack box. This was my final line:
Code:
Info: 36
Worked correctly. The only think that I can think of is that you do not have a true korn shell on your linux box. Perhaps /bin/ksh is a link to /bin/bash???

Try replacing the line "let lcount=$lcount+1" with:
Code:
lcount=`expr $lcount + 1`
And see if is any different. I wouldn't think so since it's incrementing anyway.

I was also wondering why you were using ls -l and pulling out field 9 instead of just using ls? Is there some odd condition you are working around. And do you really need the tmp file?

Code:
#!/bin/sh

COUNT=0

echo "Info: $COUNT"
for FILE in `ls`; do
   COUNT=`expr $COUNT + 1` 
   echo "Info: $COUNT $FILE"
done
echo "Info: $COUNT"
 
Old 06-13-2002, 06:56 AM   #3
CBenner
LQ Newbie
 
Registered: Jun 2002
Location: USA - Maryland
Posts: 3

Original Poster
Rep: Reputation: 0
I orignally used the "expr" command, but have read that using "let" is faster and more efficient. Either way the results were the same (and incorrect). Also, In the real script, I am using awk to "pull" and manipulate other fields as well as field 9.
 
Old 06-13-2002, 09:31 AM   #4
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Yes 'let' is faster since it does not have to spawn a process.

I just tried your script on another linux box, AIX and Sun box and they all worked correctly. One thing I noticed, however; when you do the 'ls -l *' it displays directory names and contents of the directory at the first level. When awk gets a hold if it, it produces blank lines in the output, and even counts the blank lines.
 
Old 06-13-2002, 02:30 PM   #5
CBenner
LQ Newbie
 
Registered: Jun 2002
Location: USA - Maryland
Posts: 3

Original Poster
Rep: Reputation: 0
Changing
cat /tmp/dirlist$$ | while read filename
to
for filename in `cat /tmp/dirlist$$`
produced the correct results! Strange!!!

I'll just change the script and stop wasting time trying to figure out why it wouldn't work as written our Linux box ( ksh version - @(#)PD KSH v5.2.14 99/07/13.2).

BTW - I comment out the rm of any /tmp files when I am debugging my scripts. There is probably a better way, but it works for me.

Thanks,

Curt...
 
Old 07-07-2002, 05:51 PM   #6
kervin
Member
 
Registered: Jan 2002
Location: Melbourne, FL.
Distribution: redhat
Posts: 168

Rep: Reputation: 31
I've realized that if you want anything *near* portable shell script one has to work with bourne, ie. '/bin/sh'

but even that isn't foulproof, as most distros use bash as their bourne shell and there are a few differences between bash in bourne mode and most other bourne implementations.
 
Old 05-19-2006, 09:13 AM   #7
cool244
LQ Newbie
 
Registered: Mar 2005
Posts: 7

Rep: Reputation: 0
variables losing value after while

Quote:
Originally Posted by kervin
I've realized that if you want anything *near* portable shell script one has to work with bourne, ie. '/bin/sh'

but even that isn't foulproof, as most distros use bash as their bourne shell and there are a few differences between bash in bourne mode and most other bourne implementations.

I am also facing same problem with while loop. I may not be able to use for, because I have multiple variables.

for example:

vmstat 1 6 | while read a b c d e f g h

anyone knows how to use for in place of while in above situation.

For me also variables are getting reset.

with regards
 
Old 05-19-2006, 09:46 AM   #8
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
What exactly do you want to do with the vmstat output?

I don't really understand your problem.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with simple shell script for loop abefroman Programming 2 10-25-2005 08:26 PM
incrementing variable in an until loop jeffreybluml Programming 2 05-12-2005 07:45 AM
getting a variable out of a WHILE loop. fhinkle Programming 8 02-22-2005 04:43 AM
[c shell] simple for loop saiz66 Programming 1 09-28-2004 07:02 PM
parsing a variable using linux shell philco Programming 6 09-04-2003 06:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration