Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
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
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`"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.