![]() |
|
Ampersands and File Descriptors in Bash - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Computers (https://sickgaming.net/forum-86.html) +--- Forum: Linux, BSD & Unix (https://sickgaming.net/forum-88.html) +--- Thread: Ampersands and File Descriptors in Bash (/thread-88675.html) |
Ampersands and File Descriptors in Bash - xSicKxBot - 02-13-2019 Ampersands and File Descriptors in Bash In our quest to examine all the clutter ( Code: &Code: |Code: ;Code: >Code: <Code: {Code: [Code: (Code: ]Code: }Code: &Last time, we saw how you can use Code: &In the previous tutorials on angle brackets, you saw how to use Code: >Code: ls > list.txtto pipe the output from Code: lsNow we see that this is really shorthand for Code: ls 1> list.txtAnd that Code: 1Code: stdoutIn a similar fashion Code: 2Code: stderrCode: ls 2> error.logall error messages are piped to the error.log file. To recap: Code: 1>Code: stdoutCode: 2>Code: stderrThere is a third standard file descriptor, Code: 0<Code: stdinCode: <Code: 1Code: 2Code: >What are the standard file descriptors good for? If you are following this series in order, you have already used the standard output ( Code: 1>Code: >Things like Code: stderrCode: 2Code: mkdir newdirand if newdir/ already exists, Code: mkdirCode: mkdir newdir 2> /dev/nullThis is not just a matter of “let’s not show ugly and irrelevant error messages because they are annoying,” as there may be circumstances in which an error message may cause a cascade of errors elsewhere. Say, for exapmple, you want to find all the .service files under /etc. You could do this: Code: find /etc -iname "*.service"But it turns out that on most systems, many of the lines spat out by Code: findCode: findInstead, you can do this: Code: find /etc -iname "*.service" 2> /dev/nullAnd you get only the results you are looking for. A Primer on File Descriptors There are some caveats to having separate file descriptors for Code: stdoutCode: stderrCode: find /etc -iname "*.service" 1> services.txtwould work fine because Code: 1>But herein lies a problem: what if you *do* want to keep a record within the file of the errors along with the non-erroneous results? The instruction above won’t do that because it ONLY writes the correct results from Code: findCode: find /etc -iname "*.service" 2> services.txtwill ONLY write the errors. How do we get both? Try the following command: Code: find /etc -iname "*.service" &> services.txt… and say hello to Code: &We have been saying all along that Code: stdinCode: stdoutCode: 1Code: stderrCode: 2So, 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: Code: find /etc -iname "*.service" 1> services.txt 2>&1This pipes Code: stderrCode: stdoutCode: stdoutAnd there it is again: the Code: &Code: 1Another 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. It looks like it has been written the wrong way around. You may be reading it like this: “pipe the output to a file and then pipe errors to the output.” It would seem the error output comes to late and is sent when Code: 1But that is not how file descriptors work. A file descriptor is not a placeholder for the file, but for the input and/or output channel to the file. In this case, when you do Code: 1> services.txtCode: 1If you still think it is the wrong way around, try this: Code: find /etc -iname "*.service" 2&>1 1>services.txtAnd notice how it doesn’t work, notice how errors get piped to the terminal and only the non-erroneous output (that is Code: stdoutCode: services.txtThat is because Bash processes every result from Code: findCode: 2&>1Code: stdoutCode: 1Code: findCode: 2Code: 1Then at the end of the command, Bash sees you want to open Code: stdoutCode: 1By contrast, in Code: find /etc -iname "*.service" 1>services.txt 2>&1Code: 1Code: services.txtCode: 2Code: 1Code: services.txtIn any case, as mentioned above Code: &>Code: 2>&1This is probably all a bit much, but don’t worry about it. Re-routing file descriptors here and there is commonplace in Bash command lines and scripts. And, you’ll be learning more about file descriptors as we progress through this series. See you next week! |