Shell Debugging Method Summary

Source: Internet
Author: User

For tired print and Echo, try a little bit of fresh effort.

1. Using the shell's execution options

The common execution directives for SH and bash are:-N,-X,-C,-V.

which

-N is a shorthand for noexec, which is to do only the syntax checking of the script without executing the script.

-X is shorthand for xtrace, which is to print the code itself to standard output before each line of code executes (the code adds a "+" to differentiate). This is especially useful when debugging.

-C is the abbreviation for command string, which is used to read the script from a string instead of a file. For example, Sh-c ' a=1;b=2;let c= $a + $b; echo "c= $c".

-V is the abbreviation for verbose, which displays all the standard inputs that are read in.

These options, in addition to being specified when starting the script, can also be set using set in the script. For example, in a script, we can use set to open and close xstrace:

Set-x #启动"-x"+x #关闭"-x" option

2. Set debug traps with trap

The trap is a script that can receive the specified SIGNAL and then execute the setting . In addition to the SIGNAL we use with kill-l, there are three virtual signal:exit,err,debug. Top two needless to say, the third is that every instruction executed will be issued ... So, we can use DEBUG to track variables. As follows, we can add this line of code to the script:

' echo "line: $LINENO, a= $a" ' DEBUG

Then, each line of the script executes, the corresponding line number is printed, and the value of a is ... Of course, in addition to variable A, you can arbitrarily specify the variables you want to chase. For example, the following example:

1#!/bin/Bash2 3Trap'echo "line: $LINENO, str= $str, str1= $str 1"'DEBUG4 5Str="ABCDEFG"6 7str1=$ (ExprSubstr"$str" 1 3)8 9 Echo$str 1Ten  One if[$str 1 = ="ABC"] ; Then A     Echo " Nice" - fi

The results of the implementation are as follows:

Line : 5, str=, str1= line:7, STR=ABCDEFG, str1= line:9, STR=ABCDEFG, str1= Abcabcline: One, STR=ABCDEFG, str1=abcline:STR=ABCDEFG, str1=abc Nice

Idea, because here are only 5, 7, 9, 11, 12 These lines have code, so ~

3. Use Tee to debug piping

Sometimes we need to debug the pipeline ... For example, the following, set a few layers, do not know who is wrong ... A trial of the strips? It seems to be more troublesome.

the function of tee is that when the standard input has data, the data is transferred to the standard output, and the data is written to a file . Like what:

Ipaddr= '/sbin/ifconfiggrep'inet addr:'grep'  127.0.0.1'teecutawk'{print $}' ` Echo $ipaddr

Tee This section, is exactly to see what the middle output ... After executing this sentence, there is a temp.txt file in the current directory, the content is:

Cat temp.txtinet Addr: 192.168. 0.1  Bcast:192.168. 0.255  Mask:255.255. 255.0

So ...

4. Using the Debug hooks

This is entirely a small trick to do with ordinary code, I believe in other languages when programming, such code is also commonly used. Implemented in the shell is probably the case:

if ["$DEBUG" = "truethenecho  " debugging "  #此处可以输出调试信息 fi

With this code, we can conveniently use a variable called Debug to control the output of debugging information; set ' Export Debug=true ' to open debug output . Alternatively, we can set a DEBUG function :

DEBUG () {if["$DEBUG"="true"]; Then[email protected]fi}a=1DEBUGEcho "a= $a"if["$a"-eq1] ; Thenb=2Elseb=1fiDEBUGEcho "b= $b"C=3DEBUGEcho "c= $c"

When the release, remember to switch off the debugging, keep the code, there is no significant impact.

Shell Debugging Method Summary

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.