2014年9月1日 星期一

Linux - Shell scripting, Show command executed along with output in log file



1. 用echo or printf

2. 用set -x


[root@testdb4 script]# tar cf /tmp/test.tar -T /root/script/tar_from.txt
tar: Removing leading `/' from member names
[root@testdb4 script]#
[root@testdb4 script]# /root/script/test_tar2.sh
[root@testdb4 script]#
[root@testdb4 script]# cat test_tar2.sh.out
+ tar cf /tmp/test.tar -T /root/script/tar_from.txt
tar: Removing leading `/' from member names
[root@testdb4 script]#
[root@testdb4 script]# cat /root/script/test_tar2.sh
#!/bin/sh
out=$0.out
set -x
{
tar cf /tmp/test.tar -T /root/script/tar_from.txt
} >$out 2>&1



[root@testdb4 script]# cat /root/script/test_tar2.sh
#!/bin/sh
out=$0.out
#set -x
{
tar cf /tmp/test.tar -T /root/script/tar_from.txt
} >$out 2>&1
[root@testdb4 script]# /root/script/test_tar2.sh
[root@testdb4 script]# cat test_tar2.sh.out
tar: Removing leading `/' from member names

  • 2>&1 indicates that the standard error (2>) is redirected to the same file descriptor that is pointed by standard output (&1).



沒有留言:

張貼留言