Linux Bash 中使用重定向运算符的 5 种方法

注:机翻,未校。


Five ways to use redirect operators in Bash

Posted: January 22, 2021 | by Damon Garn

Redirect operators are a basic but essential part of working at the Bash command line. See how to safely redirect input and output to make your Linux sysadmin life easier.

重定向操作符是在 Bash 命令行中工作的一个基本但重要的部分。了解如何安全地重定向输入和输出,从而简化 Linux 系统管理员的工作。

Detour sign

Photo by Kind and Curious on Unsplash

Data is entered into the computer via stdin (usually the keyboard), and the resulting output goes to stdout (usually the shell). These pathways are called streams. However, it’s possible to alter these input and output locations, causing the computer to get information from somewhere other than stdin or send the results somewhere other than stdout. This functionality is referred to as redirection.
数据通过 stdin(通常是键盘)输入到计算机中,生成的输出进入 stdout(通常是 shell)。这些路径称为流。但是,可能会更改这些输入和输出位置,从而导致计算机从 stdin 以外的位置获取信息,或者将结果发送到 stdout 以外的位置。此功能称为重定向。

In this article, you’ll learn five redirect operators, including one for stderr. I’ve provided examples of each and presented the material in a way that you can duplicate on your own Linux system.
在本文中,您将学习 5 个重定向运算符,包括一个用于 stderr 的运算符。我提供了每个示例,并以您可以在自己的 Linux 系统上复制的方式呈现这些材料。

Regular output > operator 常规输出>运算符

The output redirector is probably the most recognized of the operators. The standard output (stdout) is usually to the terminal window. For example, when you type the date command, the resulting time and date output is displayed on the screen.
输出重定向器可能是运算符中最受认可的。标准输出 (stdout) 通常发送到终端窗口。例如,当您键入 date 命令时,生成的时间和日期输出将显示在屏幕上。

[damon@localhost ~]$ date
Tue Dec 29 04:07:37 PM MST 2020
[damon@localhost ~]$

It is possible, however, to redirect this output from stdout to somewhere else. In this case, I’m going to redirect the results to a file named specifications.txt. I’ll confirm it worked by using the cat command to view the file contents.
但是,可以将此输出从 stdout 重定向到其他位置。在本例中,我要将结果重定向到名为 specifications.txt 的文件。我将通过使用 cat 命令查看文件内容来确认它是否正常工作。

[damon@localhost ~]$ date > specifications.txt
[damon@localhost ~]$ cat specifications.txt 
Tue Dec 29 04:08:44 PM MST 2020
[damon@localhost ~]$ 

The problem with the > redirector is that it overwrites any existing data in the file. At this stage, you now have date information in the specifications.txt file, right? If you type hostname > specifications.txt, the output will be sent to the text file, but it will overwrite the existing time and date information from the earlier steps.
> 重定向器的问题在于它会覆盖文件中的所有现有数据。在此阶段,您现在在 specifications.txt 文件中有 date 信息,对吧?如果键入 hostname > specifications.txt,输出将发送到文本文件,但会覆盖先前步骤中的现有时间和日期信息。

[damon@localhost ~]$ hostname > specifications.txt 
[damon@localhost ~]$ cat specifications.txt 
localhost.localdomain
[damon@localhost ~]$ 

It is easy to get in trouble with the > redirector by accidentally overwriting existing information.
由于意外覆盖现有信息,很容易在使用 > 重定向器时遇到麻烦。

Regular output append >> operator 常规输出追加>>运算符

The append >> operator adds the output to the existing content instead of overwriting it. This allows you to redirect the output from multiple commands to a single file. For example, I could redirect the output of date by using the > operator and then redirect hostname and uname -r to the specifications.txt file by using >> operator.
追加 >> 运算符将输出添加到现有内容中,而不是覆盖它。这使您可以将多个命令的输出重定向到单个文件。例如,我可以使用 > 运算符重定向 date 的输出,然后使用 >> 运算符将 hostnameuname -r 重定向到 specifications.txt 文件。

[damon@localhost ~]$ date > specifications.txt
[damon@localhost ~]$ hostname >> specifications.txt 
[damon@localhost ~]$ uname -r >> specifications.txt 
[damon@localhost ~]$ cat specifications.txt 
Tue Dec 29 04:11:51 PM MST 2020
localhost.localdomain
5.9.16-200.fc33.x86_64
[damon@localhost ~]$ 

Note: The >> redirector even works on an empty file. That means that you could conceivably
注意:>> 重定向器甚至可以处理空文件。

ignore the regular > redirector to alleviate the potential to overwrite data, and always rely on the >> redirector instead. It’s not a bad habit to get into.
忽略常规 > 重定向器以减轻覆盖数据的可能性,并始终依赖 >> 重定向器。养成这不是一个坏习惯。

Regular input < operator 常规输入<运算符

The input redirector pulls data in a stream from a given source. Usually, programs receive their input from the keyboard. However, data can be pulled in from another source, such as a file.
输入重定向器从给定源拉取流中的数据。通常,程序从键盘接收输入。但是,可以从其他源(例如文件)拉取数据。

It’s time to build an example by using the sort command. First, create a text file named mylist.txt that contains the following lines:
现在是使用 sort 命令构建示例的时候了。首先,创建一个名为 mylist.txt 的文本文件,其中包含以下行:

cat
dog
horse
cow

Observe that the animals are not listed in alphabetical order.
请注意,这些动物不是按字母顺序列出的。

What if you need to list them in order? You can pull the contents of the file into the sort command by using the < operator.
如果您需要按顺序列出它们怎么办?您可以使用 < 运算符将文件的内容拉入 sort 命令。

[damon@localhost ~]$ sort < mylist.txt 
cat
cow
dog
horse
[damon@localhost ~]$ 

You could even get a little fancier and redirect the sorted results to a new file:
您甚至可以更花哨一点,并将排序结果重定向到新文件:

[damon@localhost ~]$ sort < mylist.txt > alphabetical-file.txt
[damon@localhost ~]$ cat alphabetical-file.txt 
cat
cow
dog
horse
[damon@localhost ~]$ 

Regular error 2> operator 常规错误 2> 运算符

The stdout displays expected results. If errors appear, they are managed differently. Errors are labeled as file descriptor 2 (standard output is file descriptor 1). When a program or script does not generate the expected results, it throws an error. The error is usually sent to the stdout, but it can be redirected elsewhere. The stderr operator is 2> (for file descriptor 2).
stdout 显示预期结果。如果出现错误,将以不同的方式进行管理。错误被标记为文件描述符 2(标准输出为文件描述符 1)。当程序或脚本未生成预期结果时,它会引发错误。错误通常会发送到 stdout,但可以将其重定向到其他地方。stderr 运算符为 2>(用于文件描述符 2)。

Here is a simple example, using the misspelled ping command:
下面是一个简单的示例,使用拼写错误的 ping 命令:

[damon@localhost ~]$ png
bash: png: command not found...
[damon@localhost ~]$

Here is the same misspelled command with the error output redirected to /dev/null:
这是相同的拼写错误命令,错误输出重定向到 /dev/null

[damon@localhost ~]$ png 2> /dev/null
[damon@localhost ~]$ 

Skip to the bottom of list
Automation advice 自动化建议
Ansible Automation Platform beginner’s guide Ansible 自动化平台初学者指南
A system administrator’s guide to IT automation IT 自动化系统管理员指南
Ansible Automation Platform trial subscription Ansible 自动化平台试用订阅
Automate Red Hat Enterprise Linux with Ansible and Satellite 使用 Ansible 和 Satellite 实现红帽企业 Linux 的自动化

The resulting error message is redirected to /dev/null instead of the stdout, so no result or error message is displayed on the screen.
生成的错误消息将重定向到 /dev/null 而不是 stdout,因此屏幕上不会显示任何结果或错误消息。

Note: /dev/null, or the bit bucket, is used as a garbage can for the command line. Unwanted output can be redirected to this location to simply make it disappear. For example, perhaps you’re writing a script, and you want to test some of its functionality, but you know it will throw errors that you don’t care about at this stage of development. You can run the script and tell it to redirect errors to /dev/null for convenience.
注意:/dev/null,即位桶,用作命令行的垃圾桶。不需要的输出可以重定向到此位置,以使其消失。例如,也许您正在编写一个脚本,并且想要测试其中的一些功能,但您知道它会抛出您在此开发阶段不关心的错误。为方便起见,您可以运行脚本并告诉它将错误重定向到 /dev/null

Pipe | operator 管道 | 运算符

Ken Hess already has a solid article on using the pipe | operator, so I’m only going to show a very quick demonstration here.
Ken Hess 已经有一篇关于使用 pipe | 运算符的扎实文章,所以我在这里只打算展示一个非常快速的演示。

The pipe takes the output of the first command and makes it the input of the second command. You might want to see a list of all directories and files in the /etc directory. You know that’s going to be a long list and that most of the output will scroll off the top of the screen. The less command will break the output into pages, and you can then scroll upward or downward through the pages to display the results. The syntax is to issue the ls command to list the contents of /etc, and then use pipe to send that list into less so that it can be broken into pages.
管道接收第一个命令的输出,并使其成为第二个命令的输入。您可能希望查看 /etc 目录中所有目录和文件的列表。你知道这将是一个很长的列表,大部分输出将从屏幕顶部滚动。less 命令会将输出分解为多个页面,然后您可以向上或向下滚动页面以显示结果。语法是发出 ls 命令列出 /etc 的内容,然后使用管道将该列表发送到 less 中,以便可以将其分解为多个页面。

[damon@localhost ~]$ ls /etc | less

Ken’s article has many more great examples. Personally, I find myself using *command* | less and *command* | grep *string* the most often.
Ken 的文章还有很多很好的例子。就我个人而言,我发现自己最常使用 *command* | less*command* | grep *string*

Download now: A sysadmin’s guide to Bash scripting.

Wrap up 总结

Redirect operators are very handy, and I hope this brief summary has provided you with some tricks for manipulating input and output. The key is to remember that the > operator will overwrite existing data.
重定向运算符非常方便,我希望这个简短的摘要为您提供了一些操作输入和输出的技巧。关键是要记住 > 运算符将覆盖现有数据。


Bash Redirections Cheat Sheet

RedirectionDescription
cmd > fileRedirect the standard output (stdout) of cmd to a file.
cmd 1> fileSame as cmd > file. 1 is the default file descriptor (fd) for stdout.
cmd 2> fileRedirect the standard error (stderr) of cmd to a file. 2 is the default fd for stderr.
cmd >> fileAppend stdout of cmd to a file.
cmd 2>> fileAppend stderr of cmd to a file.
cmd &> fileRedirect stdout and stderr of cmd to a file.
cmd > file 2>&1Another way to redirect both stdout and stderr of cmd to a file. This is not the same as cmd 2>&1 > file. Redirection order matters!
cmd > /dev/nullDiscard stdout of cmd.
cmd 2> /dev/nullDiscard stderr of cmd.
cmd &> /dev/nullDiscard stdout and stderr of cmd.
cmd < fileRedirect the contents of the file to the standard input (stdin) of cmd.
cmd << EOL
line1
line2
EOL
Redirect a bunch of lines to the stdin. If ‘EOL’ is quoted, text is treated literally. This is called a here-document.
cmd <<-EOL
<tab>foo
<tab><tab>bar
EOL
Redirect a bunch of lines to the stdin and strip the leading tabs.
cmd <<< “string”Redirect a single line of text to the stdin of cmd. This is called a here-string.
exec 2> fileRedirect stderr of all commands to a file forever.
exec 3< fileOpen a file for reading using a custom file descriptor.
exec 3> fileOpen a file for writing using a custom file descriptor.
exec 3< > fileOpen a file for reading and writing using a custom file descriptor.
exec 3>&-Close a file descriptor.
exec 4>&3Make file descriptor 4 to be a copy of file descriptor 3. (Copy fd 3 to 4.)
exec 4>&3-Copy file descriptor 3 to 4 and close file descriptor 3.
echo “foo” >&3Write to a custom file descriptor.
cat <&3Read from a custom file descriptor.
(cmd1; cmd2) > fileRedirect stdout from multiple commands to a file (using a sub-shell).
{ cmd1; cmd2; } > fileRedirect stdout from multiple commands to a file (faster; not using a sub-shell).
exec 3< > /dev/tcp/host/portOpen a TCP connection to host:port. (This is a bash feature, not Linux feature).
exec 3< > /dev/udp/host/portOpen a UDP connection to host:port. (This is a bash feature, not Linux feature).
cmd <(cmd1)Redirect stdout of cmd1 to an anonymous fifo, then pass the fifo to cmd as an argument. Useful when cmd doesn’t read from stdin directly.
cmd < <(cmd1)Redirect stdout of cmd1 to an anonymous fifo, then redirect the fifo to stdin of cmd. Best example: diff <(find /path1 | sort) <(find /path2 | sort).
cmd <(cmd1) <(cmd2)Redirect stdout of cmd1 and cmd2 to two anonymous fifos, then pass both fifos as arguments to cmd.
cmd1 >(cmd2)Run cmd2 with its stdin connected to an anonymous fifo, and pass the filename of the pipe as an argument to cmd1.
cmd1 > >(cmd2)Run cmd2 with its stdin connected to an anonymous fifo, then redirect stdout of cmd to this anonymous pipe.
cmd1 | cmd2Redirect stdout of cmd1 to stdin of cmd2. Pro-tip: This is the same as cmd1 > >(cmd2), same as cmd2 < <(cmd1), same as > >(cmd2) cmd1, same as < <(cmd1) cmd2.
cmd1 |& cmd2Redirect stdout and stderr of cmd1 to stdin of cmd2 (bash 4.0+ only). Use cmd1 2>&1 | cmd2 for older bashes.
cmd | tee fileRedirect stdout of cmd to a file and print it to screen.
exec {filew}> fileOpen a file for writing using a named file descriptor called {filew} (bash 4.1+).
cmd 3>&1 1>&2 2>&3Swap stdout and stderr of cmd.
cmd > >(cmd1) 2> >(cmd2)Send stdout of cmd to cmd1 and stderr of cmd to cmd2.
cmd1 | cmd2 | cmd3 | cmd4 echo ${PIPESTATUS[@]}Find out the exit codes of all piped commands.

via:

  • Five ways to use redirect operators in Bash | Enable Sysadmin Posted: January 22, 2021 | by Damon Garn
    https://www.redhat.com/sysadmin/redirect-operators-bash

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/893270.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C语言内存之旅:从静态到动态的跨越

大家好&#xff0c;这里是小编的博客频道 小编的博客&#xff1a;就爱学编程 很高兴在CSDN这个大家庭与大家相识&#xff0c;希望能在这里与大家共同进步&#xff0c;共同收获更好的自己&#xff01;&#xff01;&#xff01; 本文目录 引言正文一 动态内存管理的必要性二 动态…

AI时代:弯道超车的新思维与实践路径

大家好&#xff0c;我是herosunly。985院校硕士毕业&#xff0c;现担任算法研究员一职&#xff0c;热衷于机器学习算法研究与应用。曾获得阿里云天池比赛第一名&#xff0c;CCF比赛第二名&#xff0c;科大讯飞比赛第三名。拥有多项发明专利。对机器学习和深度学习拥有自己独到的…

【Spring】定义的Bean缺少隐式依赖

问题描述 初学 Spring 时&#xff0c;我们往往不能快速转化思维。例如&#xff0c;在程序开发过程中&#xff0c;有时候&#xff0c;一方面我们把一个类定义成 Bean&#xff0c;同时又觉得这个 Bean 的定义除了加了一些 Spring 注解外&#xff0c;并没有什么不同。所以在后续使…

『 实战项目 』Cloud Backup System - 云备份

文章目录 云备份项目服务端功能服务端功能模块划分客户端功能客户端模块划分 项目条件Jsoncpp第三方库Bundle第三方库httplib第三方库Request类Response类Server类Client类搭建简单服务器搭建简单客户端 服务端工具类实现 - 文件实用工具类服务器配置信息模块实现- 系统配置信息…

网络编程 | UDP组播通信

1、什么是组播 在上一篇博客中&#xff0c;对UDP的广播通信进行了由浅入深的总结梳理&#xff0c;本文继续对UDP的知识体系进行探讨&#xff0c;旨在将UDP的组播通信由浅入深的讲解清楚。 组播是介于单播与广播之间&#xff0c;在一个局域网内&#xff0c;将某些主机添加到组中…

【无标题】微调是迁移学习吗?

是的&#xff0c;微调&#xff08;Fine-Tuning&#xff09;可以被视为一种迁移学习&#xff08;Transfer Learning&#xff09;的形式。迁移学习是一种机器学习方法&#xff0c;其核心思想是利用在一个任务上学到的知识来改进另一个相关任务的性能。微调正是通过在预训练模型的…

【HarmonyOS NAPI 深度探索12】创建你的第一个 HarmonyOS NAPI 模块

【HarmonyOS NAPI 深度探索12】创建你的第一个 HarmonyOS NAPI 模块 在本篇文章中&#xff0c;我们将一步步走过如何创建一个简单的 HarmonyOS NAPI 模块。通过这个模块&#xff0c;你将能够更好地理解 NAPI 的工作原理&#xff0c;并在你的应用中开始使用 C 与 JavaScript 的…

【电视盒子】HI3798MV300刷机教程笔记/备份遥控码修复遥控器/ADB/线刷卡刷/电视盒子安装第三方应用软件

心血来潮&#xff0c;看到电视机顶盒满天飞的广告&#xff0c;想改造一下家里的电视盒子&#xff0c;学一下网上的人刷机&#xff0c;但是一切都不知道怎么开始&#xff0c;虽然折腾了一天&#xff0c;以失败告终&#xff0c;还是做点刷机笔记。 0.我的机器 年少不会甄别&…

Python基于OpenCV和PyQt5的人脸识别上课签到系统【附源码】

博主介绍&#xff1a;✌Java老徐、7年大厂程序员经历。全网粉丝12w、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专栏推荐订阅&#x1f447;&…

【FPGA】MIPS 12条整数指令【1】

目录 修改后的仿真结果 修改后的完整代码 实现bgtz、bltz、jalr 仿真结果&#xff08;有问题&#xff09; bltz------并未跳转&#xff0c;jCe&#xff1f; 原因是该条跳转语句判断的寄存器r7&#xff0c;在该时刻并未被赋值 代码&#xff08;InstMem修改前&#xff09; i…

Java面试专题——常见面试题1

引入 本文属于专题中的常见面试题模块&#xff0c;属于面试时经常遇到的&#xff0c;适合需要面试的小伙伴做面试前复习准备用&#xff0c;后续会持续补充 1.面向对象基本特征 面向对象的基本特征是什么&#xff1f;怎么理解&#xff1f; 面向对象的基本特征是封装、继承、…

VUE实现简单留言板(Timeline+infinite scroll+Springboot+Hibernate)

先贴出效果图&#xff1a; 留言按照倒序排列。在底部的文本框内输入留言后&#xff0c;点击“留言”按钮&#xff0c;留言将保存至数据库中&#xff0c;同时刷新网页&#xff0c;新留言出现在顶部。 当滚动条到底部时&#xff0c;自动调用加载函数&#xff0c;显示更多早期留…

Java基础(3)

Java 数据类型详解 九、运算符 1. 基本运算符 Java 提供了多种运算符来执行不同的操作&#xff1a; 算术运算符&#xff1a;&#xff08;加&#xff09;、-&#xff08;减&#xff09;、*&#xff08;乘&#xff09;、/&#xff08;除&#xff09;、%&#xff08;取模&…

电力场景红外测温图像绝缘套管分割数据集labelme格式2436张1类别

数据集格式&#xff1a;labelme格式(不包含mask文件&#xff0c;仅仅包含jpg图片和对应的json文件) 图片数量(jpg文件个数)&#xff1a;2436 标注数量(json文件个数)&#xff1a;2436 标注类别数&#xff1a;1 标注类别名称:["arrester"] 每个类别标注的框数&am…

降维算法:主成分分析

主成分分析 一种常用的数据分析技术&#xff0c;主要用于数据降维&#xff0c;在众多领域如统计学、机器学习、信号处理等都有广泛应用。 主成分分析是一种通过正交变换将一组可能存在相关性的变量转换为一组线性不相关的变量&#xff08;即主成分&#xff09;的方法。这些主…

深入解析 C++17 中的 u8 字符字面量:提升 Unicode 处理能力

在现代软件开发中&#xff0c;处理多语言文本是一个常见需求&#xff0c;特别是在全球化的应用场景下。C17 标准引入的 u8 字符字面量为开发者提供了一个强大的工具&#xff0c;以更有效地处理和表示 UTF-8 编码的字符串。本文将详细探讨 u8 字符字面量的技术细节、实际应用&am…

ElasticSearch索引别名的应用

个人博客&#xff1a;无奈何杨&#xff08;wnhyang&#xff09; 个人语雀&#xff1a;wnhyang 共享语雀&#xff1a;在线知识共享 Github&#xff1a;wnhyang - Overview Elasticsearch 索引别名是一种极为灵活且强大的功能&#xff0c;它允许用户为一个或多个索引创建逻辑上…

Java高频面试之SE-15

hello啊&#xff0c;各位观众姥爷们&#xff01;&#xff01;&#xff01;本牛马baby今天又来了&#xff01;哈哈哈哈哈嗝&#x1f436; String 怎么转成 Integer 的&#xff1f;它的原理是&#xff1f; 在 Java 中&#xff0c;要将 String 转换为 Integer 类型&#xff0c;可…

2024又是一年的CSDN之旅-总结过去展望未来

一、前言 一年就这样在忙忙碌碌的工作和生活中一晃而过&#xff0c;总结今年在CSDN上发表的博客&#xff0c;也有上百篇之多&#xff0c;首先感谢CSDN这个平台&#xff0c;能让我有一个地方记录工作中的点点滴滴&#xff0c;也在上面学到了不少知识&#xff0c;解决了工作中遇到…

c86机器安装nvaid显卡驱动报错:ERROR:Unable to load the kernel module ‘nvidia.ko‘.

背景&#xff1a; 最近小伙伴要去北京甲方现场搭建我们的AI编码服务&#xff0c;需要使用GPU机器跑大模型&#xff0c;根据现场提供的信息是2台C86的机器&#xff0c;显卡够够的&#xff0c;但是现场是内网环境&#xff0c;因此&#xff0c;需要先准备好需要的安装包&#xff…