LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-03-2007, 05:15 AM   #16
d.anitkumar
LQ Newbie
 
Registered: Oct 2007
Posts: 1

Rep: Reputation: 0
I am not getting value outside the loop


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
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-03-2007, 06:34 AM   #17
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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
 
Old 04-28-2008, 02:05 PM   #18
dpoper1
LQ Newbie
 
Registered: Mar 2008
Posts: 3

Rep: Reputation: 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
 
Old 08-10-2009, 06:39 AM   #19
lbt
LQ Newbie
 
Registered: Mar 2004
Location: UK
Distribution: Debian, Ubuntu, Mer
Posts: 3

Rep: Reputation: 2
A simple solution to the subject line:

cat file |
(
while read line
do
echo process $line
done
)
 
Old 08-10-2009, 07:47 AM   #20
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you are digging up an old thread. check the date of post next time. also, no need to use cat. its useless
 
Old 08-10-2009, 08:14 AM   #21
lbt
LQ Newbie
 
Registered: Mar 2004
Location: UK
Distribution: Debian, Ubuntu, Mer
Posts: 3

Rep: Reputation: 2
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...

 
1 members found this post helpful.
Old 02-03-2010, 02:10 PM   #22
thehog
LQ Newbie
 
Registered: Feb 2010
Posts: 1

Rep: Reputation: 0
Code:
#!/bin/bash

IFS=$'\12'

for i in $(cat somefile.txt); do 
  echo "line: $i";
done

Last edited by thehog; 02-03-2010 at 02:12 PM.
 
Old 05-15-2010, 02:18 AM   #23
kvmreddy
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Rep: Reputation: 3
Check bellow link, there I posted 5 methods to parse a file line by line
Process a file line by line

Last edited by kvmreddy; 05-17-2010 at 07:21 AM.
 
Old 05-15-2010, 03:02 AM   #24
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by kvmreddy View Post
Check bellow link, there I posted 5 methods to parse a file line by line
Process a file line by line
The link is slightly mangled. Here's unmangled: Process a file line by line.

Nice blog

Regards request for correction ...

Regards "Bash can sometimes start a subshell in a PIPED "while-read" loop", what are the circumstances under which bash does not do so?

Regards
Code:
FILENAME=$1
...
done < $FILENAME
That will fail if $1 includes embedded IFS charcters; safer to use double quotes: done < "$FILENAME".

Regards "exec 3<&0 Now all of the keyboard and mouse input is going to our new file descriptor 3" it is only the keyboard input, not the mouse input.

Regards "while read LINE Using File Descriptor" an alternative, that does not require saving fd 0 and restoring it is to assign the file to, say, fd 3 and use read's -u option to read from fd3.

Regards typos, "The file descriptors for stdin,stdout, and stderr are 0,1, and 2, respectively" it is more correctly "The file descriptors for stdin, stdout, and stderr are 0, 1, and 2, respectively", that is with a space after the fist commas in the list.

The test timings are very useful showing how much faster awk is at processing a large file but it would be nice to see the other case -- the case when processing a single line. You would have to process the same line many times to get the timing and of course this would buffer both the input file and awk in RAM but it would still be interesting. I understand that the shell's fork-exec to run awk uses a lot of resource which will more than offset awk's much greater file IO and string handling efficiency.

Last edited by catkin; 05-15-2010 at 03:04 AM. Reason: typodynamics
 
Old 05-17-2010, 07:19 AM   #25
kvmreddy
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Rep: Reputation: 3
I have posted 5 different methods to process a file line by line in a shell script. check bellow link
Different ways to process a file line by line
 
Old 05-17-2010, 11:13 AM   #26
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Besides the quoting issue that catkin mentioned, you should also use read -r in all the examples. You almost never want to not use -r. Also, use "printf" instead of "echo -e". The exact behavior of "echo -e" can change a lot from system to system.
 
Old 05-17-2010, 12:41 PM   #27
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Why is everyone responding to this >6 year old thread?
 
Old 10-16-2010, 09:46 AM   #28
Abid Malik
LQ Newbie
 
Registered: Sep 2010
Posts: 25

Rep: Reputation: -1
Question array within function

hello everyone!

i am passing an array in function. I want to read the first content again and again until it is empty so

function calltoarray
{
read $1[0]
while [ ! -n "$1[0]" ]
do
echo empty
read $1[0]
done
}
but the code is not working. please help me out.
 
Old 10-20-2010, 06:04 AM   #29
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
howto read file shell

Okay first off I realize this thread is old... very old. However the complexity that these guys are going about it is driving me nuts so I have to post this to enlighten them (and anyone who would be searching for this solution).

First off reading a file in shell is extremely simple.

lets make a simple file called mycat and let it's contents be...
Code:
#!/bin/bash
while read line;do
  echo $line
done
That will read from stdin so you can read a file by simply...
Code:
chmod 755 ./mycat
./mycat < somefiletoread
The other thing is this guy is basically going about it the hard way because this can easily be done with a one liner.
Quote:
Originally Posted by dpoper1 View Post
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
And here it is...

Code:
find /path/to/tmp2 -type f -not -name 'tmp3' -print0 | xargs -0 grep -ih 'remote-host' > tmp3
Just thought I'd post in case someone else was dealing with a similar issue...

SAM

Last edited by sag47; 10-20-2010 at 06:17 AM.
 
1 members found this post helpful.
Old 03-14-2011, 02:20 PM   #30
jthan24
LQ Newbie
 
Registered: Jan 2010
Posts: 1

Rep: Reputation: 0
Hi, i test my script with a file and is excellent!!


for (( i = 0; i < `awk '{x++}END{ print x}' test.out`; i++))
do
echo `head -n $i test.out | tail -n 1` #return the line
done


where test.out is the file to scann!!
 
  


Reply

Tags
content, do, file, from, how, name, possibilities, read, script, write



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 10 12-07-2010 01:42 PM
bash: read a line from file zokik Linux - General 6 12-10-2008 09:24 AM
shell script that read each line separatly xpucto Programming 6 09-20-2005 08:06 AM
Shell Script to read 500files from the command line saravanan1979 Programming 1 09-22-2004 09:44 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 01:52 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