LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 07: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, 07:24 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
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 07:30 AM.
 
Old 04-25-2011, 07:45 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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, 07: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, 08:14 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

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


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


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