Shell Script debugging

Source: Internet
Author: User
Tags echo command

1. Use the Dos2unix command to process scripts developed under Windows

The following scenarios for placing scripts under Windows for editing under Linux are as follows:

[[email protected] ~]# cat-v nginx.sh #!/bin/bash^Ma=1 n^Msum=0    ^ M  while ((a <=50 )) ^mdo ^m    ((sum=sum+a)) ^m    ((a+ +)) ^  Mdone^m

[Email protected] ~]# sh nginx.sh #执行脚本
: Command not found
: Command not found
' Ginx.sh:line 4:syntax error near unexpected token '
' Ginx.sh:line 4: ' While ((I <=100))

In the above procedure, the script developed under Windows checks for no problems. However, there is an inexplicable syntax error when executing on a Linux system. At this point, it is best to perform dos2unix formatting.

#格式化命令安装~]# Yum-y install Dos2unix#使用方法 ~]# Dos2unix nginx.sh dos2unix:converting file nginx.sh to UNIX format ...
#再次查看

[Email protected] ~]# cat-v nginx.sh
#!/bin/bash
A=1 N
Sum=0
while ((a <=50))
Do
((Sum=sum+a))
((a++))
Done

Tip: The ^m disappears, stating that it is normal. The line break in Windows code is not the same as Linux, which causes the problem in this example. Scripts developed under Windows or not written by yourself require the use of Dos2unix formatting to prevent errors during execution.

2. Debugging with bash command parameters
[[Email protected] ~]# sh [-NVX] nginx.sh parameter description:-N: Do not execute the script, only query the script syntax for problems, and give the error-V: When executing the script, Output the contents of the script to the screen first, and then execute the script. If there is an error, you will also be given an error message. -X: Displays the executed script content and output to the screen, which is a useful parameter for debugging. 

Note: The parameter-X is a great way to track the execution of a script, and he can perform all the program segments that are executed at the forefront.

If the program paragraph, in the output, the first will be preceded by a + symbol, indicating the program code.

If there is a problem with the execution of the script (not a syntax problem), you can know which line the problem is in by using the-x parameter

In general, if you are debugging a script with logic errors, the parameter with-X works better.

Cons: Loading a library of system functions and so many of the scripts that we don't want to see the whole process, there's too much output, which makes it hard to see what you need.

3. Debug with echo command

The echo command is one of the most useful tools for debugging scripts. In general, you should include the echo command in the important part of the script that is likely to occur (add the Echo command before and after the variable read or modify operation, and exit the command immediately). This debugging method applies not only to the shell, but also to the PHP and Python languages.

Example:

[[Email protected] ~]# Cat debug.sh#!/bin/Bashread-P"Please input number:"a becho $a $b #输出变量, view the value of the obtained variable exit #退出脚本, do not proceed with the execution of the script. if(($a <$b)) Then Echo"$a < $b"elif (($a==$b)) Then Echo"$a = $b"ElseEcho"$a > $b"fi
4. Debug part of the script content using the SET command

The set command can also be used for secondary script debugging.

Common debugging options for the SET command:

    • Set-n: Read command order does not execute
    • SET-V: Show All Rows Read
    • Set-x: Show all commands and their parameters

Tip: Turn on the debug function with the Set-x command, and turn off the debug function via set +x.

The biggest advantage of the set command is that set-x can narrow the scope of debugging compared to Bash-x.

[email protected] ~]# cat debug.sh

#!/bin/Bashread-P"Please input number:"a BSet-x #开启调试if(($a <$b)) Then Echo"$a < $b"elif (($a==$b)) Then Echo"$a = $b"ElseEcho"$a > $b"fiSet+x#结束调试, only for set-x and set + x script between debug echo"Perfect"

Execute script to view Debug results:

[Email protected] ~]# sh debug.sh Please input number: 2 2  22  )) 2 2 ))2=22=2set +x Perfect

Tip: Add set +x to run the script without using Sh-x.

Shell Script debugging

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.