首页 > 代码库 > shell script追踪和调试

shell script追踪和调试

1. shell script调试


使用sh命令,检查shell的语法,追踪执行步骤。
sh [-nvx] script.sh
-n : 不执行script,仅检查是否有语法问题
-v : 执行前将script打印出来
-x : 将使用到的script打印出来

2. 检查语法错误


使用sh -n, 如果有语法错误,会提示出来。
[work@www sh]$ sh -n for.sh 
for.sh: line 7: syntax error near unexpected token `o'
for.sh: line 7: `o'
[work@www sh]$ 

3. shell调试打印

[work@www sh]$ sh -v for.sh 
#!/bin/bash

# desc : for loop


for animal in dog cat pig 
do
    echo "HI, ${animal}"

done
HI, dog
HI, cat
HI, pig
exit;
[work@www sh]$
[work@www sh]$ sh -x for.sh 
+ for animal in dog cat pig
+ echo 'HI, dog'
HI, dog
+ for animal in dog cat pig
+ echo 'HI, cat'
HI, cat
+ for animal in dog cat pig
+ echo 'HI, pig'
HI, pig
+ exit
[work@www sh]$

地址 :http://blog.csdn.net/yonggang7/article/details/40680369

shell script追踪和调试