shell如何在脚本中重定向

重定向输出

可以在脚本中用stdout和stderr文件描述符在多个位置生成输出,只要重定向相应的文件描述符就行了

临时重定向【不懂】

永久重定向

  • 重定向stdout
#!/bin/bash 
exec 1>testout   #exec会启动一个新shell并将stdout文件描述符重定向到文件。脚本中发给stdout的所有输出会重定向到testout。  1是stdout的文件描述符

echo "redirecting all output"
echo "to another file."
echo "without have to redirect ---->testout"

执行脚本

$ bash script.sh 
$ cat testout 
redirecting all output
to another file.
without have to redirect ---->testout
  • 重定向stderr
#!/bin/bash 
exec 2>error

echo "redirecting all error" >&2
echo "to another file."  >&2
echo "without have to redirect ---->error"  >&2

重定向输入

#!/bin/bash 
exec 0< error  #告诉shell它应该从文件error中获得输入,而不是stdin
count=1

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

创建自己的重定向

在脚本中重定向输入与输出时,并不局限于这3个[0、1、2]默认的文件输入符。还是用使用3~8之间的文件描述符作为输入或者输出重定向[在shell中最多可以有9个打开的文件描述符]

创建输出文件描述符

一旦将另一个文件描述符分配给一个文件,这个重定向就会一直有效,直到重新分配。

#!/bin/bash 
exec 3>output  #exec命令将文件描述符3重定向到另一个文件。
# exec 3>>output # 追加

echo "display on the monitor"
echo "this shold be stored in the file -- output" >&3  #这一行会重定向到output
echo "back on the monitor"

创建输入文件描述符

下面的例子时:testfile --> 0 —> 6 —>0【终端输入】

#!/bin/bash 
# redirecting input file descriptors 
exec 6<&0 
exec 0< testfile 
count=1 
while read line 
do 
	echo "Line #$count: $line" 
	count=$[ $count + 1 ] 
done 
exec 0<&6 
read -p "Are you done now? " answer 
case $answer in 
	Y|y) echo "Goodbye";; 
	N|n) echo "Sorry, this is the end.";; 
esac
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
shell脚本重定向是指将命令的输入、输出或错误流重定向到文件或其他位置。常见的重定向符号有以下几种: - `>`:标准输出覆盖重定向,将命令的输出重定向到其他文件。例如,`command > a.log`将命令的输出写入到a.log文件。 - `>>`:标准输出追加重定向,将命令的输出追加到其他文件。例如,`command >> a.log`将命令的输出追加到a.log文件。 - `2>&1`:标识输出重定向,将错误输出重定向到标准输出。例如,`command > /dev/null 2>&1`将命令的标准输出和错误输出都丢弃。 - `<`:标准输入重定向,命令将从指定文件读取输入而不是从键盘输入。例如,`cat < 1.txt`将1.txt文件的内容作为命令的输入。 - `|`:管道符,从一个命令读取输出并作为另一个命令的输入。例如,`command1 | command2`将command1的输出作为command2的输入。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [shell重定向](https://blog.csdn.net/qq_31725391/article/details/124669594)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [shell编程之重定向](https://blog.csdn.net/dingding_ting/article/details/116597376)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值