只重定向错误
#!/bin/bash
echo "************ 只重定向错误 ************"
ls -al badfile 2> test4.txt
echo "cat test4.txt"
cat test4.txt
运行结果
************ 只重定向错误 ************
cat test4.txt
ls: cannot access 'badfile': No such file or directory
混合使用STDOUT和STDERR错误消息
echo "************ 混合使用STDOUT和STDERR错误消息 ************"
ls -al test badtest test4.txt 2> test5.txt
echo "cat test5.txt"
cat test5.txt
运行结果
************ 混合使用STDOUT和STDERR错误消息 ************
-rw-r--r-- 1 root root 55 Jul 24 19:36 test4.txt
cat test5.txt
ls: cannot access 'test': No such file or directory
ls: cannot access 'badtest': No such file or directory
重定向错误消息和正常输出
echo "************ 重定向错误消息和正常输出 ************"
ls -al test badtest test4.txt test5.txt 2> test6.txt 1> test7.txt
echo "cat test6.txt"
cat test6.txt
echo "cat test7.txt"
cat test7.txt
运行结果
************ 重定向错误消息和正常输出 ************
cat test6.txt
ls: cannot access 'test': No such file or directory
ls: cannot access 'badtest': No such file or directory
cat test7.txt
-rw-r--r-- 1 root root 55 Jul 24 19:36 test4.txt
-rw-r--r-- 1 root root 107 Jul 24 19:36 test5.txt
所有输出送同一位置 &>
echo "************ 所有输出送同一位置 &> ************"
ls -al test test4.txt test5.txt badtest &> test8.txt
echo "cat test8.txt"
cat test8.txt
运行结果
************ 所有输出送同一位置 &> ************
cat test8.txt
ls: cannot access 'test': No such file or directory
ls: cannot access 'badtest': No such file or directory
-rw-r--r-- 1 root root 55 Jul 24 19:36 test4.txt
-rw-r--r-- 1 root root 107 Jul 24 19:36 test5.txt