ITworld. com – Send in your Unix questions today. Last week's column looked at how we can read from and write to files by associated the files with user-defined file descriptors. This week, we take this idea a bit further. Closing File Descriptors In last week's column, we looked at using file descriptors to read and write files. After associating a file descriptor with a file, you might want to close the file descriptors. To do this, you could exit the shell (at which point all files that it had open will be closed) or you can issue commands such as these:.
exec 4<&-. Notice that, in closing file descriptors, you don't use ">" in place of "<" when the file descriptor was used to write to a file. We close the file descriptor for the $outfile (the file we were writing to) and $infile (the file we were reading from) using the same command. Saving and Restoring File Descriptors. Another way to read and write files using file descriptors is to associate standard in and standard out with files.
. & &>/dev/null >/dev/null 2>&1 and their portability with. a number 2 = standard error. Capturing data from a Fluke 1620a via Bash /dev/tcp file. Tried all sorts of ways to redirect both stdout and stderr to /dev/null without any success. I have almost my entire life run bash which i've never had this issue.
. how do I redirect error messages to /dev/null? You can send. backup &>/dev/null. Redirect both standard error and. bash script: exec 2>/dev/null. . null, and something is. prints to standard output, and sometimes prints to standard error. This question was for Bash but. Bash send stderr to dev null. Redirect stderr to dev/null in bash env. direct actual error message to dev/null exit 1;;. Redirect stdout and stderr.
When you do this, the commands that you would normally run to read from standard in and write to standard out (like those you run all the time!) will read and write from files instead. While in last week's column, I said this was something we would generally avoid, there are some advantages to doing this if you're careful. In the code shown below, for example, we preserve file descriptor 0 (standard in) by associating it with file descriptor 5 and later restore it when we are done by re-associating it with its normal file descriptor, 0. While file descriptor is associated with $infile, we read three lines from the file. Let's look at the output from this script: After file descriptor 0 was reassociated with standard in, we were able to answer the prompt with a "yes".
This technique simplifies our read statements since we don't have to include the file descriptor in each read command. We can also do the same thing with standard out as shown here: This script saves and reassigns both standard in and standard out. It then prepares an HTML ordered list from the contents of the file provided as argument 1 and writes it to the file provided as argument 2. The "cat" line in the middle of the script does the bulk of the work. The "cat -" portion of the line reads from standard in (now associated with the input file, $1) and the awk command writes to standard out (now associated with the output file $2). This means that this entire script is equivalent to this command:.
$ "cat infile awk '' > outfile. Suppressing Script Errors. Another very handy use of file descriptors is to associate standard error with the special file, /dev/null. If we do this, we will not have to add the "2> /dev/null" redirects at the ends of commands in our scripts to keep error output from intimidating our users. In the script segment shown below, for example, the find command will not display "Permission denied" errors even if the user does not have permission to read the contents of some of the directories being searched, as would certainly be the case were he to start his search at /.
#!/bin/bash With standard error redirected to /dev/null by the association of file descriptor 2 with that file, the user will see only what we want him to see. One interesting side effect of associating standard error with /dev/null was that one of my scripts stopped displaying the prompt for a ckyorn command. Obviously, this command uses standard error to send the prompt to the user. With standard error being sent to /dev/null, our prompt was also sent to /dev/null, so you may not want to use this technique in scripts which prompt the use for information with ckyorn or similar commands.