In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Preface
Bash includes many important commands such as ls, cd, and mv, as well as many useful tools such as grep, awk, and sed. But in addition, there are many punctuation marks in Bash that can act as glue, such as dots (.), commas (,), parentheses (), quotation marks (") and so on. Let's take a look at the angle brackets () that can be used for data conversion and transfer.
Transfer data
If you know anything about other programming languages, you will know angle brackets
< 和 >It is generally used as a logical operator to compare the size relationship between two values. If you also write HTML, angle brackets, as part of various tags, will not be unfamiliar to you.
In the shell scripting language, angle brackets can move data from one place to another. For example, you can store data in a file like this:
Ls > dir_content.txt
In the above example, the > symbol tells shell to write the output of the ls command to dir_content.txt instead of displaying it directly on the command line. It is important to note that if the dir_content.txt file does not exist, Bash will be created for you, but if dir_content.txt is an existing non-empty file, its contents will be overwritten. So be careful before doing something like this.
You can also use > instead of >, so that the new data can be appended to the end of the file without overwriting the existing data in the file. For example:
Ls $HOME > dir_content.txt; wc-l dir_content.txt > > dir_content.txt
In this series of commands, you first write the contents of the home directory to the dir_content.txt file, then use wc-l to calculate the number of lines in the dir_content.txt file (that is, the number of files in the home directory) and append it to the end of the dir_content.txt.
After executing the above command on my machine, the content of the dir_content.txt will look like this:
Applications
Bin
Cloud
Desktop
Documents
Downloads
Games
ISOs
Lib
Logs
Music
OpenSCAD
Pictures
Public
Templates
Test_dir
Videos
17 dir_content.txt
You can understand > and > as arrows. Of course, the direction of this arrow can also be reversed. For example, some actors from Coen brothers (LCTT Brothers, an American film director group) and the number of times they starred in movies are saved in the CBActors file, like this:
John Goodman 5
John Turturro 3
George Clooney 2
Frances McDormand 6
Steve Buscemi 5
Jon Polito 4
Tony Shalhoub 3
James Gandolfini 1
You can execute the following command:
Sort
< CBActorsFrances McDormand 6 # 你会得到这样的输出George Clooney 2James Gandolfini 1John Goodman 5John Turturro 3Jon Polito 4Steve Buscemi 5Tony Shalhoub 3 就可以使用 sort 命令将这个列表按照字母顺序输出。但是,sort 命令本来就可以接受传入一个文件,因此在这里使用 < 会略显多余,直接执行 sort CBActors 就可以得到期望的结果。 如果你想知道 Coens 最喜欢的演员是谁,你可以这样操作。首先: while read name surname films; do echo $films $name $surname >Filmsfirst.txt; done
< CBActors 上面这串命令写在多行中可能会比较易读: while read name surname films;\doecho $films $name $surname >> filmsfirst;\ done
< CBActors 下面来分析一下这些命令做了什么: while …; do … done 是一个循环结构。当 while 后面的条件成立时,do 和 done 之间的部分会一直重复执行;read 语句会按行读入内容。read 会从标准输入中持续读入,直到没有内容可读入;CBActors 文件的内容会通过 < 从标准输入中读入,因此 while 循环会将 CBActors 文件逐行完整读入;read 命令可以按照空格将每一行内容划分为三个字段,然后分别将这三个字段赋值给 name、surname 和 films 三个变量,这样就可以很方便地通过 echo $films $name $surname >> filmsfirst;\ to rearrange the placement order of several fields and store them in the filmfirst file.
When you are done, look at the filmsfirst file and the content will look like this:
5 John Goodman
3 John Turturro
2 George Clooney
6 Frances McDormand
5 Steve Buscemi
4 Jon Polito
3 Tony Shalhoub
1 James Gandolfini
Use the sort command again at this point:
Sort-r filmsfirst
You can see that Coens's favorite actor is Frances McDormand. (the-r parameter indicates descending order, so McDormand comes first)
Summary
The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.