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

Example Analysis of the Array of shell script programming

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail about the shell script programming array of example analysis, Xiaobian feel quite practical, so share to everyone as a reference, I hope you can have some harvest after reading this article.

An array is a collection of elements of the same data type arranged in a certain order, that is, a finite number of variables of the same type are named with a name, and then a collection of variables that distinguish them with a number. This name is called an array name, and the number is called a subscript. The variables that make up an array are called elements of the array. An array is a form in which several variables of the same type are organized in an ordered form for convenience in programming.

1. Representation of arrays

1. Subscript array

Subscript must be integer, format: array name [subscript], subscript default starts from 0.

Shell supports sparse arrays, subscript integers do not need to be consecutive.

Declare -a Array name

2, associative array

Associative arrays can access array elements using arbitrary strings as subscripts (not necessarily integers). Bash 5.0 supports associative arrays.

Format: array name [arbitrary string]

Declare -A associative array

In addition, shells only support one-dimensional arrays, not multidimensional arrays.

2. Assignment of arrays

3. Access to arrays

Access to array elements is achieved through operation syntax.

There are several syntax formats:

Length of array:

${#ARRAY[*]}: Display the number of elements;

${#ARRAY[@]}: Display the number of elements;

${#ARRAY[#]}: Number of characters of the #element;

${#ARRAY}: Number of characters of the zeroth element;

Elements of the array:

${ARRAY[*]}: Display all element contents;

${ARRAY[@]}: Show all element content;

${ARRAY[#]}: Display #element;

${ARRAY}: Display element 0;

Key value of array:

$[! ARRAY[*]}: Display all key values;

$[! ARRAY[@]}: Displays all key values.

Example: Write a script that generates 10 random numbers, saves them to an array, and displays elements with even subscripts:

4. Select elements from an array

If you want to extract certain elements from an array, it is troublesome to use subscripts to find them one by one, you can use the offset element function of the array to simplify the operation.

Syntax format:

${ARRAY[@]:offset:number}

offset: number of offset elements;

number: number of elements taken out;

Example: There are 6 elements in the array off_array. Suppose we want to take the three values from March to May:

V. Array replication

$@: Each parameter is a separate string, recommended;

$*: All elements as a string.

Example: Copy elements with even subscripts from an array into a new array:

Delete elements from an array

Format: unset array name [subscript]

VII. Comprehensive examples

10 random numbers are generated and sorted.

About "shell script programming array of sample analysis" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

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

Development

Wechat

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

12
Report