LinuxQuestions.org
Register a domain and help support LQ
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 11-19-2009, 05:29 AM   #1
logan960
LQ Newbie
 
Registered: Nov 2009
Distribution: Ubuntu (latest)
Posts: 2

Rep: Reputation: 0
shell script returning counter value from a while loop - lost upon completion


Hi all

I need to know how to return a counter variable from a while loop.
Variable value is lost when exiting the while loop.

This is a stupid exacmple just to demonstrate the problem.

### ---------------------------- begin test.sh
typeset -x -i v=0

cat .bashrc | grep "^#alias" | while read line
do
v=`expr $v + 1`
echo "found $v $line"
done

echo "Total occurrences: $v"
### ---------------------------- end test.sh

When you run it, you'll get:

$ . test.sh
found 1 #alias ll='ls -l'
found 2 #alias la='ls -A'
found 3 #alias l='ls -CF'
Total occurrences: 0

Any help would be appreciated.
 
Old 11-19-2009, 05:41 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
A classic bash "gotcha"!

When bash runs a pipeline it runs all but the first command in subshell(s) and any variables set in them are lost when the pipeline completes. Explained in more detail with workarounds here.
 
Old 11-19-2009, 06:30 AM   #3
logan960
LQ Newbie
 
Registered: Nov 2009
Distribution: Ubuntu (latest)
Posts: 2

Original Poster
Rep: Reputation: 0
Smile Thx catkin

This indeed does the trick.

# POSIX
linecnt=0
cat /etc/passwd |
{
while read -r line ; do
linecnt=$((linecnt+1))
done
echo "total number of lines: $linecnt"
}
 
  


Reply

Tags
counter, loop, return, script, shell, value, while


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 script , while loop ykc Programming 5 03-30-2009 07:50 AM
Shell Script skipping a loop dnvikram Programming 2 01-23-2009 02:29 PM
Loop in Shell Script delamatrix Programming 4 07-24-2008 05:20 PM
bash script: how do I refer to a counter i in a "for i in" loop frankie_DJ Programming 5 07-24-2007 08:45 AM
Shell Script: A Broadcast Packet Counter BigglesZX Programming 12 06-15-2006 02:23 PM


All times are GMT -5. The time now is 01:52 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration