An in depth understanding of the Angle brackets in Bash (for beginners)

  • 2020-12-22 17:55:28
  • OfStack

preface

Bash includes many important commands like ls, cd, mv, and many useful tools like grep, awk, and sed. But there are plenty of other glue marks in Bash, such as the dot (.), comma (,), and bracket ( < > ), quotation marks ("), etc. Let's take a look at the Angle brackets ( < > ).

To transfer data

If you know anything about other programming languages, you'll know about Angle brackets < and > Usually 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, you won't be surprised.

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 example above, > 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. Note that if the file dir_content.txt does not exist, Bash will create it for you. But if ES39en_content.txt is an existing non-empty file, its contents will be overwritten. So be careful before doing something like this.

You may not use it > While the use of > > , so that new data can be appended to the end of the file without overwriting existing data in the file. Such as:


ls $HOME > dir_content.txt; wc -l dir_content.txt >> dir_content.txt

In this string of commands, first write the contents of the home directory to the dir_content.txt file, then use wc-ES54en 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 dir_content.txt.

After executing the above command on my machine, the contents of ES63en_content.txt would 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 take > and > > Think of it as an arrow. Of course, the arrow could go the other way around. For example, some of the Coen brothers actors and the number of times they appear in films 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 a command like this:


sort < CBActors
Frances McDormand 6 #  You're going to get an output like this 
George Clooney 2
James Gandolfini 1
John Goodman 5
John Turturro 3
Jon Polito 4
Steve Buscemi 5
Tony Shalhoub 3

You can output the list alphabetically using the sort command. However, the sort command would have accepted an incoming file anyway, so it's used here < This is a little redundant and you can get the desired result by executing sort CBActors directly.

If you want to know who Coens's favorite actors are, you can do this. First of all:


while read name surname films; do echo $films $name $surname > filmsfirst.txt; done < CBActors

The above command might be easier to read on multiple lines:


while read name surname films;\
do
echo $films $name $surname >> filmsfirst;\
done < CBActors

Here's what these commands do:

while... ; do... done is a cyclic structure. When the condition after while is true, the part between do and done will be executed 1 straight and repeatedly. The read statement reads in content on a line. read continues to read in from standard input until nothing can be read in; The contents of the CBActors file will pass < Read in from standard input, so the while loop reads the CBActors file line by line; The read command divides each line of content into three fields by whitesspace, then assigns the three fields to the three variables name, surname, and films, making it easy to pass echo $films $name $surname > > filmsfirst; \ to rearrange the placement order of several fields and put them in the filmfirst file.

Once executed, look at the filmsfirst file, which looks 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:


sort -r filmsfirst

You can see Coens's favorite actor is Frances McDormand. (The -ES210en parameter indicates descending order, so McDormand will come first)

conclusion


Related articles: