Array of shell scripts

Source: Internet
Author: User

Array:
A collection of a bunch of variables.

Statement:
Declare-a Arraynname
Each of these variables is Arrayname[index]
Index starts from 0.

Assignment value:
Arrayname[index] = VALUE
Arrayname = (VALUE1 VALUE2 ...)
Arrayname = ([index1]=value1 [index2]=value2 ...)

Special Reference methods:
${#ARRAYNAME} Displays the character length of the first element.
${#ARRAYNAME [index]} displays the character length of the INDEX element

${#ARRAYNAME [*]} shows the number of elements in the array whose values are not empty
${#ARRAYNAME [@]} displays the number of elements in the array whose values are not empty


Signal Capture:
Common signals:
1.SIGHUP
2.SIGINT
9.SIGKILL
15.SIGTERM
18.SIGCONT
19.SIGSTOP

Among them, Sigkil and sigterm are generally not allowed to capture.

#trap ' ACTION ' Sigtype




Instance:
1. Create an array and display the elements in the array
#vim 1.sh
#!/bin/bash
Aa= ([0=jerry [1]=tom [5]=mike])
Echo ${aa[0]}
Echo ${aa[4]}
#bash 1.sh
Jerry

2. Randomly display the variables:
#vim 1.sh
#!/bin/bash
index=$[$RANDOM%7]
Aa= ([0=jerry [1]=tom [5]=mike])
echo ${aa[$INDEX]}

3. Randomly displayed 30 times
#vim 1.sh
#!/bin/bash

Aa= ([0=jerry [1]=tom [5]=mike])
For I in {1..20}; Do
index=$[$RANDOM%7]
echo ${aa[$INDEX]}
Done

4. Displays the number of elements in the array that are not empty, and the length of an element's characters
#vim 1.sh
#!/bin/bash
Aa= ([0=jerry [1]=tom [5]=mike])
echo${#AA}
Echo ${aa[3]}
Echo ${aa[@]}
#bash 1.sh
5
0
3

5. Select 5 different elements from the array

6. Generate a set of random numbers and find out the maximum number of them
#vim findmax.sh
#!/bin/bash

For I in {0:9};d O
arr[$I]= $RANDOM
Echo-n "${arr[$I]}"
Done
Echo

Declear-i Max=${arr[0]}
index=$[${#ARR [*]}-1]
For I in ' seq 1 $INDEX ';d o
If [$MAX-it ${aar[$I]] Then
max=${aar[$I]
Fi
Done

Echo $MAX

#bash findmax.sh
92

7. Generate an array with the number of user input. The elements in the array cannot be the same. Displays the values of the elements in the array after the build
#vim 2.sh
#!/bin/bash
function COMP {
For K in ' seq 0 $[${#ARR [*]}-1] ';d o
if [$1-eq $ARR [$K]] Then
Return 1
Fi
Done

return 0
}

Read-p "Enter The sum of array" sum
Declare-a ARR

For I in ' seq 0 $[$SUM-1] ';d o
While Ture;do
Ele= $RANDOM
COMP $ELE
If [$?-eq 0];then
Break
Fi
Done
$ARR [$I]= $ELE
echo ${arr[$I]}
Done

8. To achieve the signal capture, press CTRL + C not cancel.
#vim findmax.sh
#!/bin/bash
Trap ' echo ' no quit! " ' INT
For I in {0:9};d O
arr[$I]= $RANDOM
Echo-n "${arr[$I]}"
Done
Echo

Declear-i Max=${arr[0]}
index=$[${#ARR [*]}-1]
For I in ' seq 1 $INDEX ';d o
If [$MAX-it ${aar[$I]] Then
max=${aar[$I]
Fi
Done
Echo $MAX

#bash findmax.sh
CTRL + C
No quit!

9. Achieve signal capture
#vim test.sh
#!/bin/bash
Trap ' rm-rf/tmp/test;echo ' clean OK '; Exit 5 ' INT
Mkdir-p/tmp/test
While Ture;do
touch/tmp/test/file-' Date =%f-%h-%m-%s '
Sleep 2
Done

10. Use the function to overwrite the above script
#vim test.sh
#!/bin/bash
function clean ()) {
Rm-rf/tmp/test
echo "Clean OK"
}

Trap ' $CLEAN; Exit 5 ' INT
Mkdir-p/tmp/test
While Ture;do
touch/tmp/test/file-' Date =%f-%h-%m-%s '
Sleep 2
Done

This article is from "Small Private blog" blog, please be sure to keep this source http://ggvylf.blog.51cto.com/784661/1668727

Array of shell scripts

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.