当前位置: 首页 > news >正文

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 
$

http://www.lryc.cn/news/210237.html

相关文章:

  • CN考研真题知识点二轮归纳(1)
  • hadoop使用简介
  • WebSocketClient objects are not reuseable
  • 分享54个ASP.NET源码总有一个是你想要的
  • 闭包通俗解释,Demo(Go Java Python)
  • Linux部署Redis Cluster高可用集群(附带集群节点添加删除以及槽位分配操作详解)
  • 【PWN · heap | Off-By-One】Asis CTF 2016 b00ks
  • C++STL---Vector、List所要掌握的基本知识
  • 使用FastAPI部署Ultralytics YOLOv5模型
  • A. Doremy‘s Paint 3
  • 深度学习_1 介绍;安装环境
  • Python基础入门例程19-NP19 列表的长度(列表)
  • LeetCode 2558. 从数量最多的堆取走礼物
  • 【JVM】字节码文件的组成部分
  • STM32 TIM(四)编码器接口
  • 力扣第56题 合并区间 c++ 贪心
  • php 日期
  • 食物链解读
  • Day10配置文件日志多线程
  • leetcode:1154. 一年中的第几天(python3解法)
  • 竞赛 深度学习图像修复算法 - opencv python 机器视觉
  • flutter升级+生成drift文件
  • [AUTOSAR][诊断管理][ECU][$34] 下载请求
  • C 标准库 - <errno.h>和<float.h>详解
  • 对于如何学习的一点思考
  • Ensemble Methods集成学习大比拼:性能、应用场景和可视化对比总结
  • 【2024秋招】2023-9-16 贝壳后端开发二面
  • SpringCloud 微服务全栈体系(七)
  • SAP ABAP 报表输出成 excel 统计图形 (RFC : GFW_PRES_SHOW_MULT)
  • 微信小程序如何获取地理位置