shell_56.Linux永久重定向

永久重定向
1.脚本中有大量数据需要重定向,那么逐条重定向所有的 echo 语句会很烦琐。
这时可以用 exec 命令,它会告诉 shell 在脚本执行期间重定向某个特定文件描述符:

$ cat test10 
#!/bin/bash 
# redirecting all output to a file 
exec 1>testout 
echo "This is a test of redirecting all output" 
echo "from a script to another file." 
echo "without having to redirect every individual line" 
$ ./test10 
$ cat testout 
This is a test of redirecting all output 
from a script to another file. 
without having to redirect every individual line 
$

2.exec 命令会启动一个新 shell 并将 STDOUT 文件描述符重定向到指定文件。脚本中送往STDOUT 的所有输出都会被重定向。
也可以在脚本执行过程中重定向 STDOUT:

$ cat test11 
#!/bin/bash 
# redirecting output to different locations 
exec 2>testerror 
echo "This is the start of the script" 
echo "now redirecting all output to another location" 
exec 1>testout 
echo "This output should go to the testout file" 
echo "but this should go to the testerror file" >&2 
$
$ ./test11 
This is the start of the script 
now redirecting all output to another location 
$ cat testout 
This output should go to the testout file 
$ cat testerror 
but this should go to the testerror file 
$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微辣已是极限

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值