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 script

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly shows you the "Shell script / Dev/Null what is the use", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what is the use of Shell script / Dev/Null" this article.

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.

The above is all the content of the article "what is the use of / Dev/Null in Shell script". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Servers

Wechat

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

12
Report