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 use shell script to monitor the execution flow of shell script and the value of variables

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

Share

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

This article mainly explains "how to use shell script to monitor the execution process of shell script and the value of variables", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use shell scripts to monitor the execution process of shell scripts and the values of variables.

Shell program code:

The code is as follows:

#! / bin/bash

Function setlogfile

{

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

Echo "logfilename is not empty!" > > kthh

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

/ sbin/shutdown-k 10

If [$num1-eq 0]; then

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

Fi

Execute from the command line:

The code is as follows:

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

In the above shell script, the value of num1 is 0 logfile on the command line, the value of msglog execlogfile is execlog.

The use of the function setlogfile is the setlogfile log file name. In the example, setlogfile ${execlogfile} is used. Exec 2 > > $1bot exec 1 > > $1 in the function means to redirect the standard output and standard error output information to the execlog file.

Let's take a look at the contents of execlogfile, and we can see the changes in the execution flow of the shell program and the values of variables during execution.

The code is 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'

It can be seen that the flow in the program is determined by the first parameter after. / testexecutelog.sh. You can also change the first parameter to a value greater than or less than 0 for testing.

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 be executed. If you do not execute this sentence, the actual values of the variables in execution will not be printed to execlog. You can learn more about the use of set by man set.

Note: when exec operates on file descriptors, and only then, exec will not overwrite your current shell environment. That is, a new process will not be started.

At this point, I believe you have a deeper understanding of "how to use shell scripts to monitor the execution process of shell scripts and the values of variables". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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