Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of / dev/null in shell

2025-03-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is the use of / dev/null in shell". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the use of / dev/null in shell"?

Think of / dev/null as a black hole. It is very equivalent to a write-only file. Everything written to it will be lost forever. If you try to read from it, you can't read anything. However, / dev/null is very useful for both the command line and scripts.

Use

/ dev/null is usually used to discard unwanted output streams or as empty files for input streams. These operations are usually done by redirection, and any data you want to discard can be written to it.

Discard standard output

When writing a shell script, you only want to execute the following logic through the result of the command, and you don't want to have a lot of intermediate output during the command execution. At this time, you can write all the input in the command execution to / dev/null.

The existing a.sh script, whose function is to determine whether the incoming system command exists, is as follows

#! / bin/bash command-v $1 if [[$?-eq 0]]; then echo "command $1 exist..." Else echo "command $1 not exist..." Fi

Execute the. / a.sh top command, and the output is as follows

[tt@ecs-centos-7 dev_test] $. / a.sh top/bin/topcommand top exist...

Description: command-v command name is to find whether the command of the specified command name exists, if so, output the path of the specified command name, otherwise, do not make any output

$? Indicates the execution result of the previous command. 0 indicates success, and others indicates failure.

The execution result of the script first outputs the path of the top command, followed by the log of the existence of the top command.

Redirect the result of command-v $1 to / dev/null to mask the output of the top command path. The adjusted a.sh content is as follows

#! / bin/bash command-v $1 > / dev/null if [[$?-eq 0]]; then echo "command $1 exist..." else echo "command $1 not exist..." fi

Execute. / a.sh top again, and the result is as follows

[tt@ecs-centos-7 dev_test] $. / a.sh topcommand top exist... [tt@ecs-centos-7 dev_test] $

As can be seen from the execution result, after redirecting the intermediate result of command-v $1 to / dev/null, the path of the top command will not be output.

Moreover, the execution result of the modified script is the same as the original result.

Discard standard error output

In the shell script, when deleting a file, you need to determine whether the file exists before performing the deletion operation, otherwise an error will be output when deleting the file. The general delete file script content is as follows:

#! / bin/bash if [- f $1]; then rm $1 fi

You can avoid output error messages by redirecting the output of the delete command to / dev/null, and you don't have to determine whether the file exists. The adjusted delete script is as follows:

#! / bin/bashrm $1 > / dev/null 2 > $1

Execute the commands. / d.sh t1.txt and. / d.sh t2.txt, respectively, and the results are as follows:

[tt@ecs-centos-7 dev_test] $ls t*.txtt1.txt [tt@ecs-centos-7 dev_test] $. / d.sh t1.txt [tt@ecs-centos-7 dev_test] $. / d.sh t2.txt [tt@ecs-centos-7 dev_test] $ls t*.txtls: unable to access t*.txt: there is no such file or directory

Ls: cannot access t*.txt: there is no such file or directory

The t1.txt file is located in the current directory, and t2.txt does not exist. From the execution result, we can see that there will be no error output information whether deleting existing files or non-existing files.

Empty the contents of the file

There are many ways to empty the content of a file. Here is a method to clear the content of a file using / dev/null. The specific examples are as follows:

[tt@ecs-centos-7 dev_test] $cat t.txt 123456 [tt@ecs-centos-7 dev_test] $cat / dev/null > t.txt [tt@ecs-centos-7 dev_test] $cat t.txt Log processing

In the script, in order to facilitate debugging, some log printing logic is often added, and sometimes there are many debugging logs. After the script test is passed, these debugging logs may be deleted or commented out.

Here's a tip: you don't have to delete or comment out the logs, and you don't output these debug logs when you execute the script.

For example, there is a log file log.txt in the current directory, and 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 log.txt.

You can do this: the original script remains intact, delete log.txt locally, and then execute the ln-s / dev/null. / log.txt command, which establishes a soft connection from log.txt to / dev/nulll. Later, everything written to log.txt is actually written to / dev/null, while everything written to / dev/null is discarded.

Thank you for your reading, the above is the content of "what is the use of / dev/null in shell". After the study of this article, I believe you have a deeper understanding of the use of / dev/null in shell, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report