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-01-2010, 03:28 AM   #16
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757

Quote:
Originally Posted by cioraneanu View Post
Having problems with reading from file. This is my script.

Code:
nr=0
cat lista_ip.txt | while read a
do

if [ xxx]
then

echo "yyyyyyyy"
nr=$(($nr+1))
echo "NR=$nr"
fi

done

echo "NR=$nr"
It does what i needed, counts the number of lines but after it exits the while no values were saved. My $nr variable is back to 0 in the end. Any ideas?
Hi,

the problem is that the pipe is executed in a subshell. Therefor changes made in a subshell are not visible in the parent shell. Here is more info on that matter:
http://mywiki.wooledge.org/BashFAQ/024

PS: Please read also pixellany's post one prior to yours and do not post in old threads.
 
Old 04-18-2013, 12:06 PM   #17
xiaodown
Member
 
Registered: Jun 2003
Location: Virginia
Distribution: Redhat, Centos, Fedora
Posts: 37

Rep: Reputation: 15
Sorry to necromancer this thread; however, it is the top search result on Google from linuxquestions.org for 'read whole line bash'.

So, in the interest of helping out googlers, like myself, I'm going to post the solution to this here. This will hopefully help others, but mainly, it will help me the next time I forget how to do this and I consider changing my IFS.

Given a file with lines that contain spaces, such as this:

Code:
[user@box ~]$ cat blah
thislinehasnospaces
this line has spaces
these
are
separate
lines
This is another line with a variety of spaces
you can read in whole lines using "read -r", thusly:

Code:
[user@box ~]$ while read -r sillyvariable; do echo "$sillyvariable"; done < blah
thislinehasnospaces
this line has spaces
these
are
separate
lines
This is another line with a variety of spaces
So, there it is.

Again, sorry to necro the thread.
 
  


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
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
bash: read a line from file zokik Linux - General 6 12-10-2008 09:24 AM
Read a line in a txt file with bash orgazmo Programming 5 05-03-2005 07:10 AM
Read a line in a txt file with bash orgazmo Linux - Newbie 3 05-03-2005 04:16 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

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

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