LinuxQuestions.org
Help answer threads with 0 replies.
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

Tags used in this thread
Popular LQ Tags , , , , , , , , ,

Reply
 
Thread Tools
Old 10-03-2007, 06:15 AM   #16
d.anitkumar
LQ Newbie
 
Registered: Oct 2007
Posts: 1
Thanked: 0
I am not getting value outside the loop


[Log in to get rid of this advertisement]
Hi This is my script like..
while read line
do
myval="DAKS"
done
echo $myval



i am not getting the value output outside the while loop
help me out if u havr some others
d.anitkumar is offline     Reply With Quote
Old 10-03-2007, 07:34 AM   #17
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,810
Blog Entries: 5
Thanked: 114
Quote:
Originally Posted by Darren[UoW] View Post
I am using the following code to read line by line from a file, however my problem is that $value=0 at the end of the loop possibly because bash has created a subshell for the loop or something similar. How can I solve this.


value=0;

while read line
do
value=`expr $value + 1`;
echo $value;
done < "myfile"

echo $value;


Note: This example just counts the number of lines, I actually desire to do more complex processing than this though, so 'wc' is not an alternative, nor is perl im afraid.


Thanks Darren.
most of the problems i need to solve doesn't involve the use of loops at all. sometimes, using while loops sometime could be slower than just using tools like awk.
if you want to do more complex processing, why not try awk.

Code:
awk '{
     # some processing with file
     }
END { print "number of lines is " NR }' file
ghostdog74 is offline     Reply With Quote
Old 04-28-2008, 03:05 PM   #18
dpoper1
LQ Newbie
 
Registered: Mar 2008
Posts: 3
Thanked: 0
loop

hi,

I am trying to do a loop, the main puporse of the loop is that it will search in a multiple text files stored on tmp2 a pattern /remote-host so it would output the pattern into tmp3.

The code that I have is the following:

for i in $( tmp2 ) ; do
sed -n '/remote-host/p' tmp2 >> tmp3
done



and obviously is not working, I would appreciate any help.

regards,

mike
dpoper1 is offline     Reply With Quote
Old 08-10-2009, 07:39 AM   #19
lbt
LQ Newbie
 
Registered: Mar 2004
Location: UK
Distribution: Debian, Ubuntu, Mer
Posts: 2
Thanked: 1
A simple solution to the subject line:

cat file |
(
while read line
do
echo process $line
done
)
lbt is offline     Reply With Quote
Old 08-10-2009, 08:47 AM   #20
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,810
Blog Entries: 5
Thanked: 114
you are digging up an old thread. check the date of post next time. also, no need to use cat. its useless
ghostdog74 is offline     Reply With Quote
Old 08-10-2009, 09:14 AM   #21
lbt
LQ Newbie
 
Registered: Mar 2004
Location: UK
Distribution: Debian, Ubuntu, Mer
Posts: 2
Thanked: 1
Quote:
Originally Posted by ghostdog74 View Post
you are digging up an old thread. check the date of post next time. also, no need to use cat. its useless
The only reason I posted is because such an old thread was the top search result on google and had no answers that a new user could, IMHO, understand.

Using cat rather than a redirect here (again IMO) makes the answer crystal clear and hopefully will educate rather than confuse

Code:
(
  while read line
  do
    echo process $line
  done
) < file
does work but it isn't clear as you start to read the code without realising that, potentially many lines later, a file will be directed into the sub shell.

If you are after an optimised solution feel free to use exec with a fd numeric redirect and read -u too :

Code:
exec 9<file
while read -u9 line
do
  echo process $line
done
Explanation left as an exercise for the reader...

lbt is offline     Reply With Quote
Thanked by:

Reply

Bookmarks


Thread Tools

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: read every line in the files and use the line as parameters as another program tam3c36 Programming 9 10-18-2009 12:05 PM
bash: read a line from file zokik Linux - General 6 12-10-2008 10:24 AM
shell script that read each line separatly xpucto Programming 6 09-20-2005 09:06 AM
Shell Script to read 500files from the command line saravanan1979 Programming 1 09-22-2004 10:44 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 11:24 PM


All times are GMT -5. The time now is 12:29 PM.

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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration