Shell script implements monitoring shell script execution flow and variable value _linux shell

Source: Internet
Author: User

Many times, we will write a shell program to complete some of the time without duplicating the wheel, but because the shell statement will also have functions, there will be variables, in the end after the implementation of what related operations, it is necessary for the specific implementation of variables such as variable factors monitoring, So we're going to write a little shell example to do the monitoring of the changes in the variables in the execution process and the execution process of the entire program.

Shell Program code:

Copy Code code as follows:

#!/bin/bash
function SetLogFile
{
if! [-Z "$"]; Then
echo "LogFileName is not empty!" >> kthh
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
/sbin/shutdown-k 10
If [$num 1-eq 0]; Then
echo "NUM1 is 0 again" >> ${logfile}
Fi


Executing on the command line:
Copy Code code as follows:

[Root@ufo shellprogram]#./testexecutelog.sh 0 Msglog Execlog

In the above shell script, num1 corresponds to a value of 0 logfile in the command line with a value of Msglog execlogfile execlog.

The use of function setlogfile is setlogfile log file name, in the example using SetLogFile ${execlogfile}, in the function of exec 2>> $1;exec $ 1 redirects the standard output and the standard error output information to the Execlog file.

Let's look at the contents of the Execlogfile and see the changes in the execution process and the value of the variables in the execution of the shell program.

Copy Code code as follows:

[Root@ufo shellprogram]# Cat Execlog
+ ' [' 0-eq 0 '] '
+ echo ' num1 is 0 '
+/sbin/shutdown-k 10
+ ' [' 0-eq 0 '] '
+ echo ' NUM1 is 0 again '

As you can see, the process in the program is determined by the first parameter after the./testexecutelog.sh. You can also test by changing the first argument to a value greater than 0 or less than 0.

The most important sentence is set-x, which means that after each simple command is extended, the PS4 extension value is displayed, followed by the command to execute. If this is not done, the actual value of the variable in execution will not be printed into the execlog. The use of set is available for man set to learn more.

Note: When exec operates on a file descriptor, it is only then that EXEC does not overwrite your current shell environment. That is, it does not start a new process.

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.