Chinaunix
标题:
bash -x 如何打印出时间戳
[打印本页]
作者:
dolinux
时间:
2012-02-29 19:28
标题:
bash -x 如何打印出时间戳
HI,各位
RT,在执行脚本的时候,用 bash -x 可以打印出脚本详细的执行过程,现在有个问题,如何在打印详细过程的时候同时打印出脚本中
每条命令执行的时间?
比如:
$ cat test.sh
#!/bin/bash
echo "hello"
echo "world"
bash -x test.sh
{2012-02-29 18:00:00} + echo hello
{2012-02-29 18:00:01} hello
{2012-02-29 18:00:02} + echo world
{2012-02-29 18:00:03} world
作者:
hbmhalley
时间:
2012-02-29 19:46
bash -x temp.sh 2>&1 | sed -u 'h;s/.*/date/e;G;s/\n/ : /;'
复制代码
硬搞的 ..
作者:
blackold
时间:
2012-02-29 20:27
回复
1#
dolinux
可以这样搞硬PS4。
$ PS4='\d \t+ '
$ set -x
$ for i in t*.txt ;do echo $i;done
Wed Feb 29 20:27:02+ for i in 't*.txt'
Wed Feb 29 20:27:02+ echo test.txt
test.txt
Wed Feb 29 20:27:02+ for i in 't*.txt'
Wed Feb 29 20:27:02+ echo tt67.com.txt
tt67.com.txt
复制代码
作者:
dolinux
时间:
2012-02-29 20:46
回复
2#
hbmhalley
找到解决方法了,如下:
mengalong@ubuntu:/tmp$ cat a.sh
#!/bin/bash
export PS4='+ [$USER@\H \w ${BASH_SOURCE[0]}
LINENO ${FUNCNAME[0]} \D{%F %T}]\$'
source func.sh
function tt
{
for i in `seq 0 3`
do
echo $i
done
}
echo hello
tt
aa
mengalong@ubuntu:/tmp$ bash -x a.sh
+ export 'PS4=+ [$USER@\H \w ${BASH_SOURCE[0]}
LINENO ${FUNCNAME[0]} \D{%F %T}]\$'
+ PS4='+ [$USER@\H \w ${BASH_SOURCE[0]}
LINENO ${FUNCNAME[0]} \D{%F %T}]\$'
+ [mengalong@ubuntu /tmp a.sh:3 2012-02-29 04:45:58]$source func.sh
+ [mengalong@ubuntu /tmp a.sh:11 2012-02-29 04:45:58]$echo hello
hello
+ [mengalong@ubuntu /tmp a.sh:12 2012-02-29 04:45:58]$tt
++ [mengalong@ubuntu /tmp a.sh:5 tt 2012-02-29 04:45:58]$seq 0 3
+ [mengalong@ubuntu /tmp a.sh:6 tt 2012-02-29 04:45:58]$for i in '`seq 0 3`'
+ [mengalong@ubuntu /tmp a.sh:8 tt 2012-02-29 04:45:58]$echo 0
0
+ [mengalong@ubuntu /tmp a.sh:6 tt 2012-02-29 04:45:58]$for i in '`seq 0 3`'
+ [mengalong@ubuntu /tmp a.sh:8 tt 2012-02-29 04:45:58]$echo 1
1
+ [mengalong@ubuntu /tmp a.sh:6 tt 2012-02-29 04:45:58]$for i in '`seq 0 3`'
+ [mengalong@ubuntu /tmp a.sh:8 tt 2012-02-29 04:45:58]$echo 2
2
+ [mengalong@ubuntu /tmp a.sh:6 tt 2012-02-29 04:45:58]$for i in '`seq 0 3`'
+ [mengalong@ubuntu /tmp a.sh:8 tt 2012-02-29 04:45:58]$echo 3
3
+ [mengalong@ubuntu /tmp a.sh:13 2012-02-29 04:45:58]$aa
+ [mengalong@ubuntu /tmp func.sh:3 aa 2012-02-29 04:45:58]$echo 'ni mei'
ni mei
其余的自由组合~
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2