Can someone tell me why this does not work? I'm playing around with file descriptors, but feel a little lost.
#!/bin/bash
echo "This"
echo "is" >&2
echo "a" >&3
echo "test." >&4
The first three lines run fine, but the last two error out. Why?
Can someone tell me why this does not work? I'm playing around with file descriptors, but feel a little lost.
#!/bin/bash
echo "This"
echo "is" >&2
echo "a" >&3
echo "test." >&4
The first three lines run fine, but the last two error out. Why?
It's failing because those file descriptors don't point to anything! The normal default file descriptors are the standard input
0
, the standard output1
, and the standard error stream2
. Since your script isn't opening any other files, there are no other valid file descriptors. You can open a file in bash usingexec
. Here's a modification of your example:And now we'll run it:
As you can see, the extra output was sent to the requested files.
File descriptors 0, 1 and 2 are for stdin, stdout and stderr respectively.
File descriptors 3, 4, .. 9 are for additional files. In order to use them, you need to open them first. For example:
For more information take a look at Advanced Bash-Scripting Guide: Chapter 20. I/O Redirection.
It's an old question but one thing needs clarification.
While the answers by Carl Norum and dogbane are correct, the assumption is to change your script to make it work.
What I'd like to point out is that you don't need to change the script:
It works if you invoke it differently:
which means to redirect file descriptors 3 and 4 to 1 (which is standard output).
The point is that the script is perfectly fine in wanting to write to descriptors other than just 1 and 2 (stdout and stderr) if those descriptors are provided by the parent process.
Your example is actually quite interesting because this script can write to 4 different files:
Now you have the output in 4 separate files:
What is more interesting about it is that your program doesn't have to have write permissions for those files, because it doesn't actually open them.
For example, when I run
sudo -s
to change user to root, create a directory as root, and try to run the following command as my regular user (rsp in my case) like this:I get an error:
But if I do the redirection outside of
su
:(note the difference in single quotes) it works and I get:
which are 4 files owned by root in a directory owned by root - even though the script didn't have permissions to create those files.
Another example would be using chroot jail or a container and run a program inside where it wouldn't have access to those files even if it was run as root and still redirect those descriptors externally where you need, without actually giving access to the entire file system or anything else to this script.
The point is that you have discovered a very interesting and useful mechanism. You don't have to open all the files inside of your script as was suggested in other answers. Sometimes it is useful to redirect them during the script invocation.
To sum it up, this:
is actually equivalent to:
and running the program as:
is the same as:
The number 1 is just a default number and it is stdout.
But even this program:
can produce a "Bad descriptor" error. How? When run as:
The output will be:
Adding
>&-
(which is the same as1>&-
) means closing the standard output. Adding2>&-
would mean closing the stderr.You can even do a more complicated thing. Your original script:
when run with just:
prints:
But you can make descriptors 3 and 4 work, but number 1 fail by running:
It outputs:
If you want descriptors both 1 and 2 fail, run it like this:
You get:
Why? Didn't anything fail? It did but with no stderr (file descriptor number 2) you didn't see the error messages!
I think it's very useful to experiment this way to get a feeling of how the descriptors and their redirection work.
Your script is a very interesting example indeed - and I argue that it is not broken at all, you were just using it wrong! :)