LinuxQuestions.org
Review your favorite Linux distribution.
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 12-05-2005, 09:21 AM   #1
sajith
Member
 
Registered: Sep 2005
Location: kannur
Posts: 59

Rep: Reputation: 15
shell programming


hai sir

i have a doubt in shell programming

when i do shell program

in that declare a variable count=0

and use this variable inside a loop and incremented each when specified condition of the loop is satisfied

and last print the value of variable oustide the loop

at this it display the value of variable as 0(zero,initialization value)

but inside the loop the incremented each time
but when print the final value it will not display it correctly

sir please give me solution for this problem
 
Old 12-05-2005, 09:26 AM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
I'd have to see a script to comment on your problems, but the script below works as you describe.

Code:
#!/bin/sh

COUNT=0

while [ $COUNT -lt 5 ]; do
   echo $COUNT
   COUNT=`expr $COUNT + 1`

done

echo "Fishished: $COUNT"
Code:
# ./loop.sh
0
1
2
3
4
Fishished: 5
 
Old 12-05-2005, 09:40 AM   #3
sajith
Member
 
Registered: Sep 2005
Location: kannur
Posts: 59

Original Poster
Rep: Reputation: 15
shell programming

hai sir

here i am give the code

but it will not work properly

sir's code is working
but i implement the same thing inside a while loop and if statement

but it will not give the correct final value of the variable

code is


#!/bin/bash
count=0
ls -lS $1>tmp
echo -e filename \\t filesize

tail +2 tmp|while read line
do
f_size=`echo $line|cut -f5 -d" " `
if [ $f_size -gt 1000 ]
then


echo -e ` echo $line|cut -f9 -d" " ` \\t ` echo $line|cut -f5 -d" " `
count=` expr $count + 1 `
echo $count

fi



done
 
Old 12-05-2005, 09:45 AM   #4
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
You are not looping based on the condition of 'count'. You are looping based on what is read from a file. So when the count condition is met the loop may/may not continue.

I'd suggest not looping on the read and do something similar to what I've previously posted.
 
Old 12-05-2005, 10:53 AM   #5
LasseW
Member
 
Registered: Oct 2004
Distribution: Fedora 7, OpenSuse 10.2
Posts: 108

Rep: Reputation: 15
Hi Sajith,

The problem is that the pipe will spawn a new shell and the count increment is local to that shell. Write the while loop like this instead:

while
...
done <tmp

To get rid of the total line before the loop:

ls -lS $1 | tail +2 >tmp
 
  


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
shell programming qerf Linux - Newbie 5 11-06-2005 05:28 PM
programming shell tuls Slackware 3 08-24-2005 05:58 PM
Shell Programming aman_goel Linux - General 1 11-25-2004 08:50 PM
shell programming shadowsurfer Linux - Newbie 3 09-09-2004 09:40 PM
shell programming athenerx Programming 1 10-28-2001 04:33 AM

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

All times are GMT -5. The time now is 11:09 PM.

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