A method instance for canceling redirection in a shell script _linux shell

Source: Internet
Author: User

In the previous example of Shell programming, it was mentioned that the shell script was extended and redirected to a file for viewing and debugging http://www.jb51.net/article/62435.htm. However, if there is another situation: redirect only in some places, and no redirects elsewhere. So let's revise the example in the previous article to meet this need:

Copy Code code as follows:

#!/bin/bash
function SetLogFile
{
if! [-Z "$"]; Then
echo "LogFileName is not empty!" >> kthh
EXEC 3>&1
EXEC 4>&2
EXEC 2>> $
EXEC 1>> $
Fi
}

Num1=$1
Logfile=$2
Execlogfile=$3

SetLogFile ${execlogfile}
Set-x
If [$num 1-eq 0]; Then
echo "NUM1 is 0" >> ${logfile}
elif [$num 1-ge 0]; Then
echo "NUM1 is grate 0" >> ${logfile}
Else
echo "NUM1 is less 0" >> ${logfile}
Fi
EXEC 2>&4
EXEC 1>&3
If [$num 1-eq 0]; Then
echo "NUM1 is 0 again" >> ${logfile}
Fi


The exec 3>&1;exec 4>&2 is added to the function SetLogFile, where 3, 1, 4, 2 represent the file descriptor, 1 is the standard output, or the stdout,2 is the standard error output, that is, the stderr, 1 and 2 were originally output on the display, while 3 and 4 are two file descriptors, which can be understood as two and 1 of temporary variables, 3>&1 represents the assignment of the file descriptor 2 to the file descriptor 1, It is also understood that the rule of the file Descriptor 1 (current standard output) is saved to the file descriptor 3, then the output of 1 is redirected to the file, and in the following shell program we use the 1>& 3 This means that the rules of the original standard output saved in the file Descriptor 3 are assigned to the file descriptor 1, and the original standard output rule is to output to the display instead of to a file. This is the process of redirecting and canceling the redirection restore. 2 and 4 are the same.

Gossip less, first look at the results of the operation:

Copy Code code 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


See the results??? Don't you see??? Then look carefully, ^_^

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.