More on the use of Angle brackets in Bash

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

preface

In this article, we go on to explore many other uses of Angle brackets.

In the previous article, we introduced the Angle brackets ( < > ) and some of their uses. In this article, we go on to explore many other uses of Angle brackets.

Through the use of < Can achieve the effect of "spoofing" other commands into thinking that the output of a command is a file.

For example, if you are not sure if the backup is complete, you need to confirm that a directory already contains all the files copied from the original directory. You can try this:


diff <(ls /original/dir/) <(ls /backup/dir/)

The diff command is a line-by-line tool for comparing the differences between two files. In the above example, it is used < Let diff think that the output of both ls commands is a file, so you can compare the differences between them.

Be aware of that < And (...). There is no space between.

I tried to execute the above command in my image directory and its backup directory, and the output is as follows:

[

diff < (ls /My/Pictures/) < (ls /My/backup/Pictures/)
5d4 < Dv7bIIeUUAAD1Fc.jpg:large.jpg

]

In the output < Said Dv7bIIeUUAAD1Fc. jpg: large. jpg this file exists in the left of the directory (/ My/Pictures) but not in the right directory (/ My backup/Pictures). That is, there may have been a problem during the backup process, causing the file not to be backed up successfully. If diff does not show any output, it indicates that the files in both directories are 1.

If you look at this, you might think, well, since we can pass < By providing the output of 1 command line as 1 file to a command that accepts the file format, you can dispense with the intermediate steps in the "favorite actor sort" example in the previous article and simply do the sort operation on the output.

Indeed, this example can be simplified as follows:


sort -r <(while read -r name surname films;do echo $films $name $surname ; done < CBactors)

Here string

In addition, there is another way to use Angle bracket redirection.

Use echo and pipe (|) to pass the variable usage, I believe everyone is familiar with. If you want to convert a string variable to all uppercase, you can do this:


myvar="Hello World" echo $myvar | tr '[:lower:]' '[:upper:]' HELLO WORLD

The tr command converts a string to a certain format. In the above example, tr is used to convert all lowercase letters in the string to uppercase.

It's important to understand that the point of this passing process is not the variable, but the value of the variable, which is the string Hello World. Such strings are called HERE strings, which means "this is the string we are dealing with." But for the above example, you can do it in a more intuitive way, like this:


tr '[:lower:]' '[:upper:]' <<< $myvar

Instead of using echo or pipes, this handy method uses the Angle brackets we refer to as 1.

conclusion

use < and > With these two simple symbols, Bash once again offers many options for flexibility in work.

Of course, our introduction is far from over, as there are many other symbols that can make the Bash command more convenient. But without a full understanding of them, the sign-laden Bash command will look like just a jumble of code. I'll be reading more of these Bash symbols, see you next time!


Related articles: