Shell Programming advanced 2.2 Shell Array

Source: Internet
Author: User

Specify an array for a character

How to display an array

A=1echo $a1
A= (1234) echo $a1
echo ${a[@]} 1 2 3 4
echo ${a[*]}1234

Specifies the number of digits in the display array

echo ${a[0]}1
echo ${a[2]}3
echo ${a[3]}4
Echo ${a[4]} spaces

Add array

a[4]=9echo ${a[4]}9
Echo ${a[*]}
1 2 3) 4 9

To change an element in an array

a[2]=7echo ${a[*]}12749

Get the number of elements in an array

echo ${#a [@]} 5

Sort the 10 arrays to a random 0-9 composition

 for inch 0 9  Do ' s//\n/g ' |sort-n

1759
4489
6142
9428
11344
15643
21679
25832
28684
30867

Remove one of the random 10 numbers

 for inch 0 9  do a[$i]= $RANDOM; Done; Echo ${a[@]}

21159 31025 9479 4700 29562 30523 31666 2424 15437 7014

unset a[4]
Echo ${a[@]}

21159 31025 9479 4700 30523 31666 2424 15437 7014

Select a few values by selecting the specified elements in order

 for inch 0 9  do a[$i]= $RANDOM; Done; Echo ${a[@]}

22278 2889 9019 10031 9383 31004 21595 20556 13672 26323

Echo ${a[@]:0:4}

22278 2889 9019 10031

Echo ${a[@]:1:3}

2889 9019 10031

Echo ${a[@]:4:4}

9383 31004 21595 20556

Shell Programming advanced 2.2 Shell Array

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.