How to cancel redirection in a shell program

Source: Internet
Author: User

/*************************************** ******************************
* Author: Samson
* Date: 08/18/2012
* Test Platform:
* GNU Linux version 2.6.29.4
* GNU bash, version 4.0.16 (1)-release (i386-redhat-linux-gnu)

**************************************** ****************************/

In the previous example about shell programming, we talked about extending the shell script and redirecting it to a file for viewing and debugging.

Http://blog.csdn.net/yygydjkthh/article/details/7879690. However, if there is another situation: only redirection is performed in some places, but not elsewhere. Let's modify the example in the previous article to meet this requirement:

#! /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

In the setlogfile function, exec 3> & 1; Exec 4> & 2 is added. 3, 1, 4, and 2 indicate file descriptors, 1 is the standard output, that is, stdout, 2 is the standard error output, that is, stderr, 1 and 2 are originally output on the display, and 3 and 4 are two file descriptors, it can be understood as two temporary variables that store 1 and 2. 3> & 1 indicates assigning the value of file descriptor 1 to file descriptor 3, it can also be understood that the rule of file descriptor 1 (current standard output) is saved to file descriptor 3, and then the output of 1 is redirected to the file, in the following shellProgramIn, we use 1> & 3, which means that the original standard output rules saved in file descriptor 3 are assigned to file descriptor 1, the original standard output rule is output to the display, rather than output to a file. This is the entire process of redirecting and canceling redirection restoration. The same is true for 2 and 4.

Let's talk about the running result first:
[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

Have you seen the result ??? Didn't you see it ??? Take a closer look, ^ _ ^

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.