More Ways to Use Angle Brackets in Bash

More Ways to Use Angle Brackets in Bash

Preface

In this article, we will continue to explore more other uses of angle brackets.

In the previous article, we introduced angle brackets (<>) and some of their uses. In this article, we will continue to explore more other uses of angle brackets.

By using <, you can achieve a "cheating" effect and make other commands think that the output of a command is a file.

For example, when backing up files, if you are not sure whether the backup is complete, you need to confirm whether 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 tool for comparing the differences between two files line by line. In the above example, < is used to make diff think that the output results of the two ls commands are files, so that the differences between them can be compared.

Note that there is no space between < and (...).

I tried to execute the above command in my image directory and its backup directory, and the output was the following:

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

The < in the output indicates that the file Dv7bIIeUUAAD1Fc.jpg:large.jpg exists in the directory on the left (/My/Pictures) but not in the directory on the right (/My/backup/Pictures). In other words, a problem may have occurred during the backup process, resulting in the file not being successfully backed up. If diff displays no output, the files in the two directories are identical.

Seeing this, you may think that since you can use < to provide the output content of some command lines as a file to a command that needs to accept a file format, then in the "sorting favorite actors" example in the previous article, you can skip some intermediate steps and directly perform the sort operation on the output content.

Indeed, this example can be simplified to this:

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 the redirection function of angle brackets.

I believe everyone is familiar with the usage of using echo and pipe (|) to pass variables. 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 can convert a string to a certain format. In the above example, tr is used to convert all lowercase letters in a string to uppercase letters.

It is important to understand that the focus of this transfer process is not the variable, but the value of the variable, which is the string Hello World. Such a string is called a HERE string, which means "this is the string we are going to process." But for the above example, you can also use a more intuitive way to handle it, as follows:

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

This shortcut does not require the use of echo or pipes, but instead uses the angle brackets we have been talking about.

Summarize

Using these two simple symbols, < and >, it turns out that so much can be accomplished. Bash once again provides many options for flexibility in work.

Of course, our introduction is far from over, because there are many other symbols that can bring more convenience to Bash commands. But if you don't fully understand them, Bash commands full of symbols will just look like a bunch of gibberish. I'll be explaining more Bash symbols like this in the future, so see you next time!

Well, the above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • A Deep Understanding of Angle Brackets in Bash (For Beginners)
  • Using brackets and backticks in Bash scripts

<<:  js to upload pictures to the server

>>:  JavaScript implements checkbox selection function

Recommend

Mysql database recovery actual record by time point

Introduction: MySQL database recovery by time poi...

Three common methods for HTML pages to automatically jump after 3 seconds

In practice, we often encounter a problem: how to...

Detailed tutorial on installing Nginx 1.16.0 under Linux

Because I have been tinkering with Linux recently...

CSS3 uses transform to create a moving 2D clock

Now that we have finished the transform course, l...

Solution to data duplication when using limit+order by in MySql paging

Table of contents summary Problem Description Ana...

PNG Alpha Transparency in IE6 (Complete Collection)

Many people say that IE6 does not support PNG tra...

Using CSS3 to create header animation effects

Netease Kanyouxi official website (http://kanyoux...

Example code for implementing ellipse trajectory rotation using CSS3

Recently, the following effects need to be achiev...

JavaScript implements asynchronous acquisition of form data

This article example shares the specific code for...

How to configure MySQL master-slave synchronization in Ubuntu 16.04

Preparation 1. The master and slave database vers...

How to build a multi-node Elastic stack cluster on RHEL8 /CentOS8

Elastic stack, commonly known as ELK stack, is a ...

Vue gets token to implement token login sample code

The idea of ​​using token for login verification ...

VMware configuration hadoop to achieve pseudo-distributed graphic tutorial

1. Experimental Environment serial number project...

JS Asynchronous Stack Tracing: Why await is better than Promise

Overview The fundamental difference between async...