Bash是Linux系统中默认的脚本编程语言,既然是编程语言,那么脚本的调试是必不可少的。下面介绍下bash简单的调试方法:
1 利用bash的参数-x,如
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‘ #+表示打印出每行脚本
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‘ #++表示调用
+ echo ‘2017-03-27 14:03:34‘
2017-03-27 14:03:34
2 如果脚本太长的话,使用set -x打开脚本的跟踪调试
如:vim a.sh
#!/bin/bash
for a in bafa{1..4};do
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
增加执行权限: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
本文出自 “一万年太久,只争朝夕” 博客,请务必保留此出处http://zengwj1949.blog.51cto.com/10747365/1910716
原文地址:http://zengwj1949.blog.51cto.com/10747365/1910716