[Linux] Capture the abnormal output of Linux command line and shell script and save it to a file

 

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

File /tmp/log.

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

Guess you like

Origin blog.csdn.net/qq_44065303/article/details/109094600