LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 04-25-2011, 06:20 AM   #1
tripi
LQ Newbie
 
Registered: May 2006
Posts: 2

Rep: Reputation: 0
Feeding a while loop with a command output


Hello,

I am posting this just for the sake of curiosity. I am trying to feed a while loop structure with a command output, but using the bash built-in I get the error "ambiguous redirect":

Code:
while read line
do
 echo $line
done < $(sed 1d $file)
I have tried very different things like using backticks style:

Code:
done < `sed 1d $file`
but I obtained the same output. I finally came across this solution that worked out, but I don't really understand why. Maybe somebody can enlighten me:

Code:
done < <(sed 1d $file)
Thanks in advance
 
Old 04-25-2011, 06:24 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
I pipe the data in...


sed 1d $file | while read line
do
....
done

however your command you're trying to redirect also makes no sense. because of the $() bit you're trying to *execute* the contents of the file, which I assume is not what you want to do, unless the output of sed is a single line which is a valid command in itself. In the last one you are not executing the output, although in all honesty I don't recognise that format of bash syntax, it does work, and off hand I don't see it reducing to anything else I recognise. I guess that the <() syntax is putting the output into a temporary file handle which can then be opened conventionally by the first redirect < operator, as ultimately the redirect should be a file, not data.

Last edited by acid_kewpie; 04-25-2011 at 06:30 AM.
 
Old 04-25-2011, 06:45 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
The issue you are having is that there is command substitution, which is what you are using initially,
and then there is process substitution, which you have found you should have used.

To add to this, the solution presented by acid_kewpie does work, however, as piping the data in will create a subshell, any changes made to any variables
external to the loop will be lost once the loop exits. For example (from links provided):
Code:
i=0
sort list1 | while read line; do
  i=$(($i + 1))
  ...
done
echo "$i lines processed"   # FAILS
 
Old 04-25-2011, 06:47 AM   #4
tripi
LQ Newbie
 
Registered: May 2006
Posts: 2

Original Poster
Rep: Reputation: 0
Uhmmm I see what you mean. I now understand why the first two option didn't work, thanks for the explanation.
I have found another structure that works. It has been introduced in Bash 2.0/3.0

Code:
while read line
do
 echo $line
done <<< "`sed 1d $file`"
 
Old 04-25-2011, 07:14 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Yes the herestring works, but it is definitely not intended to be used in this way. Maybe this will help.
 
  


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
how to grep(1) loop output m4rtin Programming 1 03-24-2011 12:04 PM
piping output of find to another command or loop steven19782007 Linux - Newbie 6 06-25-2009 05:29 PM
Loop column output handband2 Programming 8 11-05-2008 03:40 PM
LXer: Using Bash To Feed Command Output To A While Loop Without Using Pipes! LXer Syndicated Linux News 0 08-06-2008 12:10 PM
Feeding the output of "diff" or "cat" command to dpkg --purge kushalkoolwal Debian 9 06-19-2008 07:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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