How to debug bash scripts

Source: Internet
Author: User

Bash
Is the default shell script of the Linux operating system. Shell is a program used to process operating system and user interaction. Shell scripts can help users automate interaction with the operating system. You can also understand it as a script-based programming. Now that programming is available, the compiler, interpreter, and debugger of the program are essential, and bash is the same. However, there may be something different from the programming language and technology in debugging, therefore, the following article mainly describes various techniques for debugging bash scripts.

Tracking Script Execution

You can let Bash print all the statements in the script execution process. This is simple. You only need to use the-x option of Bash. Let's take a look.

The following script first outputs a greeting statement and then the current time:

#! /Bin/bash <br/> echo "Hello $ user," <br/> echo "Today is $ (date + '% Y-% m-% D ')"

Let's use the-x option to run this script:

 

$ Bash-x example_script.sh <br/> + echo 'Hello Chenhao, '<br/> Hello Chenhao, <br/> ++ date + % Y-% m-% d <br/> + echo 'Today is '<br/> today is

At this time, we can see that Bash prints every line of command before running. The plus sign in front of each row indicates nesting. This output allows you to see the order of command execution and the behavior of the entire script.
Output row number in the trail

In a large script, you will see a lot of output of execution traces, which is very difficult to read. Therefore, you can add the row number of the file before each row, this can be very useful. To do this, you only need to set the following environment variables:

 

export
PS4=
'+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: '

Let's take a look at the output after the PS4 environment variable is set.

 

$
bash
-x example_script.sh +example_script.sh:2::
echo
'Hello chenhao,' Hello chenhao, ++example_script.sh:3::
date
+%Y-%m-%d +example_script.sh:3::
echo
'Today is 2009-08-31' Today is 2009-08-31

 

Debug some scripts

Sometimes, you don't want to debug the entire script. You only need to debug one of them, so you can call "Set-X" before the script you want to debug ", call "set + X" at the end. The following script is used:

 

#! /Bin/bash <br/> echo "Hello $ user, "<br/> set-x <br/> echo" Today is $ (date % Y-% m-% d) "<br/> set + x

Let's see how it works?

 

$ .
/example_script
.sh

Hello chenhao,

++example_script.sh:4::
date
+%Y-%m-%d

+example_script.sh:4::
echo
'Today is 2009-08-31'

Today is 2009-08-31

+example_script.sh:5::
set
+x

Note: we do not need to use Bash-X when running the script.

Log output

Sometimes there are too many logs to be tracked, and the output content is hard to read. Generally, we only focus on conditional expressions, variable values, function calls, loops, and so on .. In this case, it may be better to log some specific information of interest.

Before using log, write a function:

 

_ Log () {<br/> If ["$ _ debug" = "true"]; then <br/> Echo 1> & 2 "$ @" <br/> fi <br/>}

Therefore, you can use the following in your script:

 

_log
"Copying files..."

cp
src/* dst/

We can see that the above _ log function needs to check a _ debug variable. Only when the variable is true can the log be actually developed and output. In this way, you only need to control this switch and do not need to delete your debug information.

 

$ _DEBUG=
true
.
/example_script
.sh

Use the bash special Debugger

If you are writing a complicated script and you need a complete debugger like debugging other languages, you can try to use the open source software bashdb.
, A bash-specific debugger. This debugger is very powerful and has all the functions you want, such as setting breakpoints, single-step tracking, jumping out of functions, and so on. Its user interface misses GDB very much. This is his document.
.

 

Source of the original article (Click here)

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.