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 purpose of / dev/null in Shell scripts

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today I'll show you what the purpose of / dev/null is in the Shell script. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.

/ dev/null is a special device file that discards all data written into it and treats it as a black hole. It is equivalent to a write-only file, and everything written to it disappears. Trying to read or output from it will not yield any results. Similarly, / dev/null is very useful on 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/top command 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 top command 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/bash rm $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*.txt t1.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*.txt ls: 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

Processing of logs

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.

This is the whole content of what is the use of / dev/null in the Shell script. For more content related to the use of / dev/null in the Shell script, you can search the previous article or browse the following article to learn! I believe the editor will add more knowledge to you. I hope you can support it!

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: 297

*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