LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 09-18-2013, 01:19 PM   #1
basusimply
LQ Newbie
 
Registered: Jun 2013
Posts: 7

Rep: Reputation: Disabled
how to print a variable inside while, outside of the loop.


My input file and script look like:-

vi input
chg//okm/entk

vi find.sh
#########
forward=/
count=0
cat /root/script/input | while read -n1 char
do
if [ "$char" == "$forward" ];then
count=$(($count + 1))
fi
done
echo "$count"
#########

but the problem is, when I try running the script it prints 0. But It should print 3.Kindly help me please...
 
Old 09-18-2013, 01:48 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Help us to help you. Provide a sample input file (10-15 lines will do). Construct a sample output file which corresponds to your sample input and post both samples here. With "Before and After" examples we can better understand your needs and also judge if our proposed solution fills those needs.

Daniel B. Martin
 
Old 09-18-2013, 02:36 PM   #3
basusimply
LQ Newbie
 
Registered: Jun 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
thanks daniel...

My intention of writing this script was to count the no. of forward slashes in a particular line or a file(whatever it may be) which contains some forward slashes.
I'm not getting any specific output from this script.
There may be some way to print a variable(which is inside a while loop) outside the while loop.Here in this script its printing the previously declared $count value, not the count value(which got updated inside while loop).So I am seeking help.
 
Old 09-18-2013, 02:51 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
the pipe cuases while to be a subshell, so that subshell's local vars. are lost


replace

Code:
cat /root/script/input | while read -n1 char
do
if [ "$char" == "$forward" ];then
count=$(($count + 1))
fi
done
with

Code:
while read -n1 char;do
if [ "$char" == "$forward" ]; then
 count=$(($count + 1))
fi
done <<< /root/script/input
that should behave as you expect it to

Edit: see post 6 below, added the red to code above

Last edited by Firerat; 09-18-2013 at 03:07 PM.
 
1 members found this post helpful.
Old 09-18-2013, 02:51 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by basusimply View Post
There may be some way to print a variable(which is inside a while loop) outside the while loop.Here in this script its printing the previously declared $count value, not the count value(which got updated inside while loop).So I am seeking help.
Firerat has it...

Last edited by astrogeek; 09-18-2013 at 02:53 PM. Reason: Updated
 
Old 09-18-2013, 03:04 PM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
hmm,

is /root/script/input a file?
I think you wanted echo and not cat..
and you seldom need cat ( do a websearch for "useless cat" or "useless use of cat"
anyway, even with echo it won't work, due to the pipe involking a subshell

try this

Code:
done <<< /root/script/input
<<< is passing what follows to stdin, it could be the output of a subshell
example
Code:
done <<< $(echo /root/script/input)
but as you will see, no need for that subshell in this case
 
Old 09-18-2013, 03:10 PM   #7
basusimply
LQ Newbie
 
Registered: Jun 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
thanks a lot Firerat....its working as I wanted.....again many many thanks..
 
Old 09-18-2013, 03:22 PM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
the "Herestring:" ( <<< )
is touched upon here
http://mywiki.wooledge.org/BashGuide/InputAndOutput
some good stuff http://mywiki.wooledge.org/BashGuide/

other reading
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html
http://www.tldp.org/LDP/abs/html/

many more, but my bookmarks are a mess
 
  


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
Sleep inside for loop inside while loop causing issues. TheOnlyQ Programming 13 12-19-2012 12:59 PM
[SOLVED] shell script : for loop to print all elements of PATH variable daudiam Linux - Newbie 9 06-12-2011 02:57 AM
[SOLVED] [bash] while loop ..always print the first variable. ridwanfi Programming 5 06-10-2011 08:12 AM
[SOLVED] exporting a variable defined inside a for loop musonio Linux - General 6 11-06-2009 08:10 PM

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

All times are GMT -5. The time now is 07:26 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