【shell实战之】重定向错误

只重定向错误

#!/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
# bash shell自动赋予错误消息更高优先级,会优先展示错误消息

运行结果

************ 所有输出送同一位置 &> ************
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值