Bash is the default scripting language in a Linux system, and since it is a programming language, debugging of scripts is essential. Here's a simple debugging approach to bash:
1 using Bash's parameter-X, such as
Bash-x a.sh
+ for A in ' bafa{1..4} '
+ ID BAFA1
+ Echo ' bafa1 is exits '
BAFA1 is exits
+ for A in ' bafa{1..4} '
+ ID BAFA2
+ Echo ' bafa2 is exits ' #+ means print out each line of script
BAFA2 is exits #打印出脚本执行的结果
+ for A in ' bafa{1..4} '
+ ID Bafa3
+ Echo ' bafa3 is exits '
Bafa3 is exits
+ for A in ' bafa{1..4} '
+ ID BAFA4
+ Echo ' bafa4 is exits '
BAFA4 is exits
+ + Date ' +%f%T ' #++ represents the call
+ echo ' 2017-03-27 14:03:34 '
2017-03-27 14:03:34
2 If the script is too long, use set-x to open the script for trace debugging
such as: Vim a.sh
#!/bin/bash
For a in bafa{1..4};d o
If ID $a &>/dev/null;then
echo "$a is exits"
Else
Useradd $a && echo "$a" | passwd--stdin $a &>/dev/null
echo "$a is create OK"
Fi
Done
Set-x
echo "$ (date +"%F%T ")"
Set +x
Increased execution permissions: chmod +x a.sh
./a.sh
BAFA1 is exits
BAFA2 is exits
Bafa3 is exits
BAFA4 is exits
+ + Date ' +%f%T '
+ echo ' 2017-03-27 14:17:27 '
2017-03-27 14:17:27
+ Set +x
This article from "10,000 years too long, seize" blog, please be sure to keep this source http://zengwj1949.blog.51cto.com/10747365/1910716
Shell Script debugging