BASH's Debugging Tools

Source: Internet
Author: User

BASH's Debugging Tools

Echo/print (General Technology)

Print some variables, or prompt for information. This should be a common method. In BASH, we can simply use echo, or print, to output some logs, or add some loglevel to filter some logs. Here's my usual function:


_loglevel=2


Die () {

echo "Critical: $1>" >&2

Exit 1

}


INFO () {

[$_loglevel-ge 2] && echo "INFO: $1>" >&2

}


ERROR () {

[$_loglevel-ge 1] && echo "ERROR: $1>" >&2

}


The implementation here is simply adding a loglevel, you can actually output the log to a file, or to the log color. Like what:


# Add Color

[$_loglevel-ge 1] && echo-e "\033[31m error:\033[0m $1>" >&2

# Redirect to File

[$_loglevel-ge 1] && echo "ERROR: $1>" >/var/log/xxx_log. $BASHPID


Set-x (Rare technology)

The-X (xtrace) option causes BASH to print out the commands to be executed before executing the command. This option is useful for debugging some command errors.


Sometimes, because the passed in parameters have some special characters, the BASH parsing is not done as we expected. At this point, if you open the X, you can print out the extended command before the command executes. For example, a function based on the previous write:


Set-x

Info "This is a info log>"

Error "This is a error log>"

Set +x


If you want to open xtrace all the way, you can add the-x parameter when you execute the script.


Trap/bashdb (Epic)

For ease of debugging, BASH also provides a trap mechanism. This is a lot more advanced than the two methods described earlier. We can use the trap this built-in command to specify the commands that each sigspec should execute. The specific usage of trap is as follows:


Trap [-LP] [[ARG] sigspec ...]

Sigspec includes the various signal defined in, Exit,err,return, and DEBUG.


Each signal is not introduced here. Exit executes the specified command when the shell exits. If a command execution in the current shell returns a value other than 0, the command associated with ERR is executed. and return is for source and., each execution will trigger a return trap. If you bind a command to debug, the debug trap will be executed before each command executes. It is important to note that ERR and DEBUG are valid only in the current shell. If you want the functions and child shells to inherit these traps automatically, you can set-t (Debug/return) and-E (ERR).

For example, the following script will execute echo when exiting:


#!/bin/bash


Trap "Echo This is a exit echo>" exit


echo "This is a normal echo>"


Or, to make the command in the script go wrong, print the corresponding command:

#!/bin/bash


Trap ' echo $BASH _command return err ' err


echo This is a normal test

Unknowncmd


Or, let the script's commands step through:

#!/bin/bash


Trap ' (read-p "[$: $LINENO] $BASH _command?>") ' DEBUG


Echo This is a test


I=0

While [true]

Do

Echo $i

((i++))

Done


BASH's Debugging Tools

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.