【Linux】捕获Linux命令行、shell脚本输出异常保存到文件

#!/bin/bash
cat aa.txt > /tmp/error 2>&1 #使用 " 2>&1" 把标准错误 stderr 重定向到标准输出 stdout ;
echo $? #捕获上一条命令的输出 (if 0 正常 else 错误)
ls -l > /tmp/log
echo $?

文件/tmp/log中。

# cat /tmp/error 
cat: aa.txt: No such file or directory

猜你喜欢

转载自blog.csdn.net/qq_44065303/article/details/109094600
今日推荐