Shell debugging and shell debugging

Source: Internet
Author: User

Shell debugging and shell debugging

I. Introduction

This article comprehensively and systematically introduces the shell script debugging technology, including using echo, tee, trap and other commands to output key information, tracking variable values, and inserting debugging hooks into the script, use the "-n" option to check the syntax of shell scripts, and use the "-x" option to track shell scripts one by one, cleverly use the built-in shell variables to enhance the output information of the "-x" option.

 

Ii. shell debugging options

1) read only shell scripts, not actually executed, used to detect whether shell scripts have syntax errors

-n

 

2) Enable the shell interpreter to read and execute shell commands from a string for temporary testing of short scripts.

-c "string"

 

3) enter the tracking method, so that the shell will display every command line actually executed during script execution, and display a "+" number at the beginning of the line

-x

Example:

$ sh –x exp2.sh+ trap 'echo "before execute line:$LINENO, a=$a,b=$b,c=$c"' DEBUG++ echo 'before execute line:3, a=,b=,c='before execute line:3, a=,b=,c=+ a=1++ echo 'before execute line:4, a=1,b=,c='before execute line:4, a=1,b=,c=+ '[' 1 -eq 1 ']'++ echo 'before execute line:6, a=1,b=,c='before execute line:6, a=1,b=,c=+ b=2++ echo 'before execute line:10, a=1,b=2,c='before execute line:10, a=1,b=2,c=+ c=3++ echo 'before execute line:11, a=1,b=2,c=3'before execute line:11, a=1,b=2,c=3+ echo endend

 

Iii. shell debugging tool: bashdb

http://bashdb.sourceforge.net/

 

Reference: http://www.ibm.com/developerworks/cn/linux/l-cn-shell-debug/

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.