Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 06-25-2009, 08:29 AM   #1
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Rep: Reputation: 55
read line doubt

hi all...

In the below script how the read line will come to know about the first line of the file entered as argument...

while read line
echo $line
done < $file1
Old 06-25-2009, 09:18 AM   #2
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
because on the last line, the file is being redirected into the loop upon which the while condition is dependent
Old 06-25-2009, 09:37 AM   #3
Senior Member
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Originally Posted by vinaytp View Post
In the below script, how will the "read line" come to know about the first line of the file entered as argument...
You can rewrite your script like this:
while read line ; do echo $line ; done < $file1
When it is written this way, you can see that everything from "while" to "done" is really one command. This means that "< $file1" is fed into the command.

For more information, run "help while" at a command prompt:
[user@machine:~]:help while
while: while COMMANDS; do COMMANDS; done
    Expand and execute COMMANDS as long as the final command in the
    `while' COMMANDS has an exit status of zero.
Old 06-25-2009, 10:08 AM   #4
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 404Reputation: 404Reputation: 404Reputation: 404Reputation: 404
I think that he's more concerned about the redirection.

The must-read for this is the 'REDIRECTION' section in the bash man page.

What happens with the < $file1 at the end of the loop is that that file is fed into it, becoming effectively stdin for all purposes in the context of the loop. read consumes it because it read from stdin, but it could be any other command. That's why in these cases you must be specially careful with what you put inside the loop as well.


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
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 01:29 PM
Read a file, doubt!!! webquinty Linux - Newbie 6 10-01-2008 04:10 PM
help with c program to read each line from text file, split line , process and output gkoumantaris Programming 12 07-01-2008 01:38 PM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 02:43 PM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 11:24 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:29 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration