Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze {curly braces} in Bash

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to analyze Bash {curly braces}, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Here, let's first look at the curly braces {}.

Structural sequence

Curly braces have already appeared in the previous article on the meaning of dots, when we only checked the dots. The usage of this article is introduced. But in the process of building a sequence, there can be no lack of curly braces.

We use

Echo {0..10}

To output the 11 numbers from 0 to 10 sequentially. Use

Echo {10..0}

You can output these 11 numbers in reverse order. Further, you can use the

Echo {10..0..2}

To skip the odd numbers.

And

Echo {z..a..2}

Outputs the alphabet from reverse order and skips the odd number of letters.

and so on.

You can also combine two sequences:

Echo {a..z} {a..z}

This command outputs all the two-letter combinations from aa to zz in turn.

This is very useful. In Bash, the way to define an array is to place elements in parentheses () and separate them with spaces, like this:

Month= ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov"Dec")

If you need to get the elements in the array, use square brackets [] and fill in the index of the element:

The $echo ${month [3]} # array index starts at 0, so [3] corresponds to the fourth element Apr

Don't pay too much attention to the three parentheses used here, which we'll talk about later.

Note that, like above, we can define an array like this:

Letter_combos= ({a.. z} {a.. z})

The array pointed to by the letter_combos variable contains all the two-letter combinations from aa to zz in turn.

Therefore, you can also define an array as follows:

Dec2bin= ({0.. 1} {0.. 1} {0.. 1} {0.. 1} {0.. 1} {0.. 1} {0.. 1})

Here, the array pointed to by the dec2bin variable contains all 8 binary numbers in ascending order, that is, 00000000, 00000001, 00000010,... , 11111111 . This array can be used as a converter from decimal to 8-bit binary numbers. For example, to convert the decimal number 25 to a binary number, you can do this:

$echo ${dec2bin [25]} 00011001

There is a better way for binary conversion, but this is an interesting one.

Parameter expansion

Look back at the one in front.

Echo ${month [3]}

In this case, the role of curly braces is not to construct a sequence, but to expand the parameter parameter expansion. As the name implies, parameter expansion is to expand the variable in curly braces into the actual content of the variable.

Let's continue to use the above month array as an example:

Month= ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov"Dec")

Notice that the array index in Bash starts at 0, so 3 represents the fourth element "Apr". So echo ${month [3]} is equivalent to echo "Apr" after parameter expansion.

Expanding an array into all its elements as above is just one of the uses of parameter expansion. In addition, a string variable can be read and processed by parameter expansion.

For example, for the following variable:

A = "Too longgg"

If executed:

Echo ${a%gg}

You can output "too long", that is, the two g of * * are removed.

Here,

${.} tell shell to expand the contents in curly braces

An is the variable that needs to be operated

% tells shell that something needs to be removed from the end of the string after the string is expanded

Gg is the deleted content.

This feature is useful when converting file formats. Let me give you an example:

ImageMagick is a set of command-line tools that can be used to manipulate image files with a convert command. The purpose of this convert command is to make a copy of an image file in one format in another format.

The following command is to use convert to make a copy of the image in PNG format image.png for the JPEG format image image.jpg:

Convert image.jpg image.png

ImageMagick is pre-installed in many Linux distributions, and if not, it can be found in the corresponding software manager for the distribution.

Going on, after expanding the variables, you can perform similar operations in batches:

I=image.jpgconvert $I ${i%jpg} png

This actually removes the "jpg" at the end of the variable I, then adds "png", and finally splices the entire command into convert image.jpg image.png.

If you don't think much of it, you can imagine hundreds of image files that need to do this and just run:

For i in * .jpg; do convert $I ${i%jpg} png; done

The task was completed in an instant.

If you need to remove the beginning of the string, change the% above to #:

$a = "Hello World!" $echo Goodbye$ {a#Hello} Goodbye World!

Parameter expansion has many uses, but it is usually needed when writing scripts. It will be mentioned in later articles in this series.

Merge output

Finally, I introduce the use of curly braces, which is very simple, that is, you can merge the output of multiple commands. First of all, take a look at the following command:

Echo "I found all these PNGs:"; find. -iname "* .png"; echo "Within this bunch of files:"; ls > PNGs.txt

Several commands separated by semicolons are executed, but only the resulting output of the * * ls command is redirected to the PNGs.txt file. If you wrap these commands in curly braces, like this:

{echo "I found all these PNGs:"; find. -iname "* .png"; echo "Within this bunch of files:"; ls;} > PNGs.txt

After the execution, you can see that the PNGs.txt file contains the contents of echo twice, the PNG file found by the find command, and the result of the ls command.

It is important to note that there needs to be a space between the curly braces and the command. Because the curly braces {and} here are reserved words in shell, shell combines the output between the two symbols.

In addition, each command should be separated by a semicolon, otherwise the command will not work properly.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, 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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report