shell 文件重定向

#!/bin/bash

rd_out_2f()
{
echo "Hello kate, this is error." >&2 #定向到文件描述符的时候,描述符前加 &
echo "This is a goog dog!" >testout_file #重定向到文件
}


rd_out_fd()
{
exec 3>&1 #新建输出描述符
exec 1>testout

echo "This should store in the output file"
echo "along with this line." >&3

exec 1>&3 #将描述符1输入重定向到标准输入
exec 3>&- #关闭描述符3 关闭其实就是定向到一个特殊的描述符 -
echo "Now things should be back normal."
}

rd_in_fd()
{
echo -e "\t### redirect input ###"
exec 6<&0      #新建描述符,暂存标准输入描述符
exec 0<inputfile #重定向标准输入

count=1
while read line
do
echo "Line #$count:$line"
count=$[ $count + 1 ]
done

exec 0<&6     #将0重新定向到标准输入
exec 6<&-     #关闭描述符

read -p "Are you done now?"
case $REPLY in
Y|y) echo "Goodbye!"
;;
N|n) echo "Sorry this is end!"
;;
esac

}



#rd_out_2f
#rd_out_fd
rd_in_fd


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值