ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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"
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.
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.
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.
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'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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.