Bash file descriptor tutorial
We have been saying all along that stdin 0 , stdout 1 , and stderr 2 are file descriptors. A file descriptor is a special construct that points to a channel to a file, either for reading, or writing, or both. This comes from the old UNIX philosophy of treating everything as a file. Want to write to a device? Treat it as a file.
Want to write to a socket and send data over a network? Want to read from and write to a file? Well, obviously, treat it as a file. So, when managing where the output and errors from a command goes, treat the destination as a file. Hence, when you open them to read and write to them, they all get file descriptors. This has interesting effects.
You can, for example, pipe contents from one file descriptor to another:. This pipes stderr to stdout and stdout is piped to a file, services. Another thing with the standard file descriptors is that, when you pipe from one to another, the order in which you do this is a bit counterintuitive. Take the command above, for example. They are particularly suited to all of these because they allow complexity without requiring it.
In this tutorial we would mainly concentrate on how to use the wide variety of commands of Linux commands to handle files, directories, processes etc.
These tutorials are created using Ubuntu version Please see the associated text box of individual spoken tutorials on the website to decide the versions of Linux OS to which it is applicable. Jump to: navigation , search. Navigation menu Personal tools Log in. Namespaces Page Discussion. It's an old question but one thing needs clarification. What I'd like to point out is that you don't need to change the script :! Your example is actually quite interesting because this script can write to 4 different files:.
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: su rsp -c '..
But even this program:! When run as:. You can even do a more complicated thing. Your original script:! If you want descriptors both 1 and 2 fail, run it like this:. V interesting response.. Thanks — Gunith D. Typo fixed. Thanks for pointing it out. This is a fantastic response. Is there a way to check if a file exists? Here's a modification of your example:! Carl Norum Carl Norum k 30 30 gold badges silver badges bronze badges. Is there a way I could have it write the out put to the terminal?
I want to be able to be able to see it all in terminal, but want to be able to send the output where I want. Trcx, if you want to write to the terminal, use stdout or stderr. Why would you need or want to use other files for that?
The scripts that I am trying to make compatible with crontab need to write multiple files out, but crontab does not allow for files to be written to from scripts because of it lacks support from stdout However I can use crontab to write the output of the scripts to a file. I was thinking that I could have the scripts write to the various outputs, and then have crontab separate everything into the appropriate files.
I was just looking for a cleaver way to write to the files with out using stdout. But thanks to you guys I found out that I was overthinking it. Calling the script without any redirections yields:. AGipson AGipson 1 1 silver badge 5 5 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.
Post as a guest Name.
0コメント