如何看到具体的执行过程?并将执行的过程完整的保存到一个文件并且显示行号
1】使用sh –x,使用-x参数能看到脚本的具体执行过程
2】使用输出重定向可以将执行过程保存到另一文件
3】显示行号:
在/etc/vimrc文件中添加行 set nu
然后在要执行的脚本中添加
trap 'echo "before execute line:$LINENO" >/dev/null' DEBUG 【添加位置在#! /bin/sh 之后,真正执行的脚本之前 】
eg:
[root@gbackup2 zhaoyj]# head -4 test.sh
#!/bin/sh
trap 'echo "before execute line:$LINENO" >/dev/null' DEBUG
Adir=backup
for i in fmx1 mx1 mx2 mx14
[root@gbackup2 zhaoyj]# tail -3 /etc/vimrc
set t_Sf=m
set nu
endif
4】执行命令
命令模式: sh -x ScriptName > 输出结果 2> 执行过程保存文件
或:sh -x ScriptName > 输出结果 2>&1
[root@gbackup2 zhaoyj]# sh -x test.sh > 245.backup.txt 2> log.txt
5】展示执行过程
所有以++开头的都是提示行,所有已+开头的都是脚本行
[root@gbackup2 zhaoyj]# more log.txt
+ trap 'echo "before execute line:$LINENO" >/dev/null' DEBUG
++ echo 'before execute line:3'
+ Adir=backup
+ for i in fmx1 mx1 mx2 mx14
++ echo 'before execute line:4'
+ for j in dir mysql post qmail
++ echo 'before execute line:6'
++ echo 'before execute line:8'
+ '[' dir '!=' post ']'
++ echo 'before execute line:10'
+ cd /backup/fmx1/dir/
++ echo 'before execute line:11'
+ echo -n 'backup.fmx1.dir '
++ echo 'before execute line:12'
++ echo 'before execute line:12'
+ ls -lt