LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 08-23-2009, 02:58 PM   #1
Lazybaer
LQ Newbie
 
Registered: Aug 2009
Posts: 2

Rep: Reputation: 0
a question to a special ls


Hi

Can anyone explain me in detail the follwing instructions (line 1 and 2 only).

ls | \
while IFS= read filename
do
(something)
done

Thanks in advance.
Regards
Lazy
 
Old 08-23-2009, 03:08 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Hi and welcome to LQ.

The code you've posted executes a while loop taking input from the ls command. This is a method to read a block of text line by line and execute some actions over each line. More in details the ls command list the files in the current directory and pipe this list to the while loop. The backslash at the end of the line is not really necessary. In shell programming it simply means that the statement continues on the next line, but the pipe acts in the same way.

The while loop just reads the input and assigns every line to the shell variable "filename". The syntax is:
Code:
while command
do
  <commands here>
done
where the command in the code above is:
Code:
IFS= read filename
the IFS= is an assignment to an environment variable, whose value will be used just for the execution of the command itself, without affecting other statements. IFS is the Input Field Separator and its default value is space/tab/newline. The statement
Code:
IFS=
just assigns the null string to IFS. This is a method to manage file names containing blank spaces. A little more clear now?
 
Old 08-23-2009, 04:01 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by colucix View Post
This is a method to manage file names containing blank spaces.
A null (=empty) IFS works just the same as the default IFS so the sample code effectively ensures that any non-standard value of IFS does not cause things to go awry. The only reason for using it would be if IFS had been set to some other value earlier which the programmer wanted to keep for later. File names with spaces will be assigned to $filename by the read command with both IFS null and the default IFS.
 
Old 08-23-2009, 04:37 PM   #4
Lazybaer
LQ Newbie
 
Registered: Aug 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks guys for the prompt replies - it's quite clear now. In the mean time my recursive script works as it should. Regards - Lazy
 
  


Reply


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
[SOLVED] Special Characters Question alelb22 Slackware 6 06-04-2009 07:19 AM
Getting the 'special buttons' to work? - a standard laptop question Richard Rahl Linux - Laptop and Netbook 8 08-27-2006 09:22 PM
A lot of special question to ask for a dummy. wood General 3 11-07-2003 01:21 AM
how to echo special character question ArnaudVR Linux - Software 1 06-29-2003 12:11 PM
emacs - special - question j-ray Linux - Software 1 03-17-2003 04:59 AM


All times are GMT -5. The time now is 05:05 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration