#!/bin/bash date #打印当前时间 while true #死循环 do #每隔2秒打印一次 sleep 2 whatthis #不存在的命令 echo -e "std output" done
上续脚本运行的时候,会抛出异常,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Tue Aug 24 09:44:50 CST 2021 ./test.sh: line 7: whatthis: command not found std output ./test.sh: line 7: whatthis: command not found std output ./test.sh: line 7: whatthis: command not found std output ./test.sh: line 7: whatthis: command not found std output ./test.sh: line 7: whatthis: command not found std output ./test.sh: line 7: whatthis: command not found std output ./test.sh: line 7: whatthis: command not found std output
root@ezreal:~# ls -al /proc/31807/fd total 0 dr-x------ 2 root root 0 Aug 24 09:44 . dr-xr-xr-x 9 root root 0 Aug 24 09:44 .. lrwx------ 1 root root 64 Aug 24 09:44 0 -> /dev/pts/0 lrwx------ 1 root root 64 Aug 24 09:44 1 -> /dev/pts/0 lrwx------ 1 root root 64 Aug 24 09:46 2 -> /dev/pts/0 lr-x------ 1 root root 64 Aug 24 09:46 255 -> /root/test.sh
如果这个时候我们想将这个shell脚本输出都重定向到一个log文件中。我们可以这样处理:
./test.sh >> test.log
运行之后我们发现,标准输出是可以追加到日志中去,但是错误输出无法追加,
1 2 3 4 5
root@ezreal:~# ./test.sh >> test.log ./test.sh: line 7: whatthis: command not found ./test.sh: line 7: whatthis: command not found ./test.sh: line 7: whatthis: command not found
./test.sh: line 7: whatthis: command not found std output ./test.sh: line 7: whatthis: command not found std output ./test.sh: line 7: whatthis: command not found std output ./test.sh: line 7: whatthis: command not found std output ./test.sh: line 7: whatthis: command not found std output ./test.sh: line 7: whatthis: command not found std output ./test.sh: line 7: whatthis: command not found std output ./test.sh: line 7: whatthis: command not found std output ./test.sh: line 7: whatthis: command not found std output ./test.sh: line 7: whatthis: command not found std output