An array of Bash programming series

Source: Internet
Author: User

Array Arrays:


How to declare an array

Declare-a AA


Assignment Method 1:

Aa[0]=jerry

Aa[1]=tom

Aa[2]=wendy

Aa[6]natasha

Default is null between


Assignment Method 2:

Aa= (Jerry Tom Wendy)


Aa= ([0=]jerry [1]=tom] [2]=wendy [6]=natasha]


Aa[3]=selina

Aa[4]=nikita


Experiment:

[Email protected] scripts]# vim testarray.sh

#!/bin/bash

#

Aa= ([0]=jerry [1]=tom [6]=nikita]

Echo ${aa[1]}

Echo ${aa[2]}

Echo ${aa[6]}


[Email protected] scripts]# bash testarray.sh

Tom


Nikita

[Email protected] scripts]#


Randomly select a value between 0-6

[Email protected] scripts]# vim testarray.sh

#!/bin/bash

#

Aa= ([0]=jerry [1]=tom [6]=nikita]

index=$[$RANDOM%7] randomly select a

echo ${aa[$INDEX]}


----------------------------------

Randomly 20 times:

#!/bin/bash

#

Aa= ([0]=jerry [1]=tom [6]=nikita]

For I in {1..20};d o

index=$[$RANDOM%7]

echo ${aa[$INDEX]}

I+=1

Done

-----------------------------------

#!/bin/bash

#

Aa= ([0]=jerry [1]=tom [6]=nikita]


echo ${#AA [0]} indicates the front element length is displayed

[Email protected] scripts]# bash testarray.sh

5

------------------------------------------

#!/bin/bash

#

Aa= ([0]=jerry [1]=tom [6]=nikita]


echo ${#AA [@]} @ or * indicates the number of reference elements (number is not empty)

[Email protected] scripts]# bash testarray.sh

3

----------------------------------

1. Write a script to find the maximum number in the data. This set of data is saved with data

#!/bin/bash

#

Aa= (1 2 3 4 5 6 7111 8 9 101)

Declare-i Max=${aa[0]}

ss=$[${#AA [@]}-1]

For I in ' seq 1 $SS ';d o

If [$MAX-lt ${aa[$I]}];then

max=${aa[$I]}

Fi

Done

Echo $MAX


---------------------------------------

Randomly generate a group of 10 elements, and then find the maximum number

#!/bin/bash

#

For I in {0..9};d o

aa[$I]= $RANDOM

Echo-n "${aa[$I]}"

Done

Echo

Declare-i Max=${aa[0]}

ss=$[${#AA [@]}-1]

For I in ' seq 1 $SS ';d o

If [$MAX-lt ${aa[$I]}];then

max=${aa[$I]}

Fi

Done

Echo $MAX


--------------------------------------

To generate an array:

1. The number of elements in an array is 1-39

2. The elements of an array cannot be the same

3. Display values for each element of this array


#!/bin/bash


Read-p "The element numbers[1-39]:" Elenum

Declare-a ARRAY #将ARRAY定义为数组

function Comele {

For J in ' seq 0 $[${#ARRAY [@]}-1] ';d o

If [$1-eq ${array[$J]}];then

Return 1

Fi

Done

return 0

}

For I in ' seq 0 $[$ELENUM-1] '; Do

While True;d o #while true dead loop

Element= $RANDOM

Comele $RANDOM

If [$?-eq 0];then

Break

Fi

Done

array[$I]= $ELEMENT

echo "${array[$I]}"

Done

---------------------------------------------

Trap: captures a specific signal in a script and can be processed specifically

1:sighup

2:sigint

9:sigkill

15:sigterm

18:sigcont

19:sigstop

Trap ' echo ' NO quit ... ' Int

Functions can be defined to implement. When the utility executes CTRL + C revocation. Delete the previously created file.

#!/bin/bash

#

function DEL {

Rm-rf/var/tmp/test

Exit

}

Trap ' DEL ' INT

Mkdir-p/var/tmp/test

While True;d o

touch/var/tmp/test/file-' Date +%f-%h-%m-%s '

Sleep 2

Done

-------------------------------


This article from "Operation and maintenance Growth Road" blog, declined reprint!

An array of Bash programming series

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.