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

How to configure Unredirection in shell script

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

Share

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

This article mainly explains "how to configure the unredirect in the shell script". The content 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 "how to configure the redirect in the shell script".

The code is as follows:

#! / bin/bash

Function setlogfile

{

If! [- z "$1"]; then

Echo "logfilename is not empty!" > > kthh

Exec 3 > & 1

Exec 4 > & 2

Exec 2 > > $1

Exec 1 > > $1

Fi

}

Num1=$1

Logfile=$2

Execlogfile=$3

Setlogfile ${execlogfile}

Set-x

If [$num1-eq 0]; then

Echo "num1 is 0" > > ${logfile}

Elif [$num1-ge 0]; then

Echo "num1 is grate 0" > > ${logfile}

Else

Echo "num1 is less 0" > > ${logfile}

Fi

Exec 2 > & 4

Exec 1 > & 3

If [$num1-eq 0]; then

Echo "num1 is 0 again" > > ${logfile}

Fi

Added exec 3 > & 1 to the function setlogfile Exec 4 > & 2, where 3, 1, 4 and 2 all represent file descriptors, and 1 is standard output, that is, STDOUT,2 is standard error output, that is, STDERR,1 and 2 are originally output on the monitor, while 3 and 4 represent two file descriptors, which can be understood as two temporary variables that hold 1 and 2. 3 > & 1 means to assign the value of file descriptor 1 to file descriptor 3, which can also be understood as saving the rules of file descriptor 1 (current standard output) to file descriptor 3, and then redirecting the output of 1 to the file, in the following shell program We also use 1 > & 3, which means that the original standard output rules saved in file descriptor 3 are re-assigned to file descriptor 1, the original standard output rules are output to the display, not to a file. This is the whole process of redirecting and unredirecting the restore. The same is true of 2 and 4.

To cut down the gossip, let's first look at the running results:

The code is as follows:

[root@UFO shellprogram] #. / testexecutelog.sh 0 msglog execlog

+ exec

+'['0-eq 0']'

+ echo 'num1 is 0 again'

[root@UFO shellprogram] # cat execlog

+'['0-eq 0']'

+ echo 'num1 is 0'

+ exec

Thank you for reading, the above is "how to configure shell script to cancel redirection" content, after the study of this article, I believe you on how to configure shell script to cancel redirection in this problem have a deeper understanding, the specific use of the situation also 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