
/dev/null is a special device file that discards everything written to it and can be seen as a black hole, it is equivalent to just writing a file, everything written to it disappears, trying to read or output from it will not produce any results, likewise, /dev/null is very useful on both the command line and in scripts
<h1 class="pgc-h-arrow-right" data-track="2" > use</h1>
/dev/null is often used to drop unwanted output streams, or as empty files for input streams, and these operations are usually done by redirection, to which any data you want to discard can be written to
<h1 class="pgc-h-arrow-right" data-track="4" > discard standard output</h1>
When writing a shell script, you only want to execute the logic behind the command through the result of the command, and do not want to have a large number of intermediate result outputs during the command execution, at this time you can write all the input in the command execution process to /dev/null
The existing a.sh script, which has the function of determining whether the incoming system command exists, and the script content is as follows
Execute the ./a.sh top command with the following output:
Description: Command -v command name is the path that looks for the command name specified, and if it does, outputs the path to the specified command name, otherwise, does not make any output
$? Represents the result of the execution of the previous command, 0 indicates success, and the other indicates failure
In the execution result of the script, the path of the top command is output first, followed by the log of the existence of the top command
Redirecting the result of command -v $1 to /dev/null masks the output of the top command path, and the a.sh after the adjustment is as follows
Execute the ./a.sh top again, and the result is as follows
As can be seen from the execution result, after redirecting the intermediate result of command -v $1 to /dev/null, the path to the top command is no longer printed
Moreover, the result of the modified script execution is the same as the original result
<h1 class="pgc-h-arrow-right" data-track="19" > discards the standard error output</h1>
In the shell script, when deleting a file, you need to determine whether the file exists before you can perform the delete operation, otherwise the deletion will output an error, and the general deletion file script is as follows:
You can avoid the output error message by redirecting the output of the delete command to /dev/null, and do not have to determine whether the file exists, and the content of the deleted script after the adjustment is as follows:
Execute the commands ./d.sh t1.txt. and ./d.sh t2 .txt respectively, as follows:
t1.txt file is located in the current directory, t2 .txt does not exist, from the execution results can be seen, whether it is deleted existing files or non-existent files will not have error output information
<h1 class="pgc-h-arrow-right" data-track="27" > empty the contents of the file</h1>
There are many ways to empty the contents of a file, here is a method of using /dev/null to empty the contents of a file, the specific example is as follows:
<h1 class="pgc-h-arrow-right" data-track="30" > log processing</h1>
In the script, in order to facilitate debugging, often add some log printing logic, sometimes this debugging log is still more, after the script test passed, these debugging logs may be deleted or commented out
Here's a little trick to neither delete nor comment out the logs, and execute the script without outputting the debug logs
For example, if there is a log .txt in the current directory, the debug log of the script will be written to this file in the form of echo "this is debug log" >> log .txt
Now that the script function test has passed, the debug log does not need to be written to the log .txt
You can do this: the original script does not move, first delete the log .txt locally, and then execute the ln -s /dev/null ./log .txt command, which establishes a log.txt to /dev/nulll soft connection, and everything that is written to log.txt is actually written to /dev/null, and everything written to /dev/null is discarded
If you need to debug the script again later, delete the link