A detailed array of bash programming in Linux

Source: Internet
Author: User

A detailed array of bash programming in Linux

1.Array arrays and variable concepts and differences

The variable is a memory space, and the variable and the variable are mostly discontinuous memory spaces

Array arrays are made up of multiple variables, variables in arrays are called elements, elements in arrays are contiguous memory spaces

2. Declaring an array

Declare-a array Name

Example: Declaring an array stu and initializing the values

Notation 1:

Declare-a STU

Stu[0]=willow

Stu[1}=tom

Stu[2]=redhat

Notation 2:

stu= (Willow Tom Redhat) #元素之间用空格隔开, the elements are assigned from 0 onwards and cannot be jumped

Notation 3:

stu= ([0]=willow [1]=tom [2]=redhat]

stu= ([0]=willow [1]=tom [5]=redhat] #元素可以跳跃赋值, the 2,3,4 element is a null value

3. Array properties

3.1. Calculate the character length of the specified element in the array:

${#STU [0]} indicates the length of the 1th element in the array STU

${#STU [1]} indicates the length of the 2nd element in the array STU

3.2. Calculating the number of elements in an array

${#STU [*]}

${#STU [@]}

4. Experiment. Remove the maximum value from the specified array, as in the following script:

#!/bin/bash

#

Array= (23 15 66 88 54 110 686 336 2 256)

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

index=$[${#ARRAY [*]}-1]

For I in ' seq 1 $INDEX '; Do

If [$MAX-lt ${array[$I]}]; Then

max=${array[$I]}

Fi

Done

echo "Array_max is $MAX"

5. Experiment, randomly generate an array using random and take the maximum value out of the array, as in the following script:

#!/bin/bash

#

Echo-n "ARRAY is"

For I in {0..9}; Do

array[$I]=$RANDOM

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

Done

Echo


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

index=$[${#ARRAY [*]}-1]


For I in ' seq 1 $INDEX '; Do

If [$MAX-lt ${array[$I]}]; Then

max=${array[$I]}

Fi

Done

echo "Array_max is $MAX"

6. Experiment, randomly remove an element from the specified array, such as the following script:

#!/bin/bash

#

stu= (Tom Willow Jerry cruo xbei roony Yibu)

#STU = ([0]=tom [1]=willow [2]=jerry [3]=cruo] [4]=xbei [5]=roony] [6]=yibu]

index=$[$RANDOM%7]

echo ${stu[$INDEX]}

7. Randomly generate an array and sort the elements in the array from small to large, as in the following script:

#

Echo-n "ARRAY is"

For I in {0..9}; Do

array[$I]= $RANDOM

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

Done

Echo


index=${#ARRAY [*]}

for ((i=0; i < $INDEX; i++)); Do

For ((j= $i; j< $INDEX; j + +)); Do

If [${array[$i]}-gt ${array[$j]}]; Then

temp=${array[$i]}

array[$i]=${array[$j]}

array[$j]= $TEMP

Fi

Done

Done


Echo-n "ARRAY Sorted is:"

For ((I=0;i < $INDEX; i++)); Do

echo-n "${array[$i]}"

Done

Echo

8. Custom generates the specified number of array elements, and the elements within the array cannot be the same and display them, as in the following script:

#!/bin/bash

#

Read-p "The element number[1-50]:" Elenum

Declare-a ARRAY

function Comele {

Local I

For I in ' seq 0 $[${#ARRAY [*]}-1] '; Do

If [$1-eq ${array[$I]}]; Then

Return 1

Fi

Done

return 0

}


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

While true; Do

element=$[$RANDOM%30];

Comele $ELEMENT

If [$?-eq 0]; Then

Break

Fi

Done

array[$I]= $ELEMENT

echo "${array[$I]}"

Done

9.getopts command: Is a bash built-in command

: Indicates that there must be content after the parameter

Optarg: Gets the contents after the short option

Optind: Gets the last parameter of the command

New Opttest simple script, accept short option-B or-D to understand the use of the getopts command

Vim opttest.sh

While getopts ": B:d:" SWITCH; Do

Case $SWITCH in

b) echo "The option is B."

Echo $OPTARG

Echo $OPTIND;;

D) echo "the option is D."

Echo $OPTARG

Echo $OPTIND;;

\?) echo "Usage:opttest.sh [-D args] [-B args]";;

Esac

Done

./optest.sh-b "OK"-D "good" #执行显示结果如下:

[Email protected] sh]#/opttest.sh-b "OK"-D "good"

The option is B.

Ok

3

The option is D.

Good

5

[Email protected] sh]#

10. Create a script file that creates a new script template that automatically generates what you need to write at the beginning of the script:

For example, #!/bin/bash, description information, creation time, if the created script exists, then just edit; If the script created does not exist,

Create a template script, and other information

#!/bin/bash

# Name:mkscript

# description:create Script

# Author:willow

# version:0.0.1

# datetime:06/14/2016 08:53:00

# Usage:mkscript FILENAME

#以下这段脚本, use the getopts command to get the-D option information

While getopts ":d:" SWITCH; Do

Case $SWITCH in

d) desc= $OPTARG;;

\?) echo "Usage:mkscript [-D DESCRIPTION] FILENAME";;

Esac

Done

Shift $[$OPTIND-1]

#以下这段脚本, creating a new script file must be content

if! grep "[^[:space:]]" $ &>/dev/null; Then

Cat > $ << EOF

#!/bin/bash

# Name: ' basename $ '

# Description: $DESC

# Author:willow

# version:0.0.1

# Datetime: ' date +%f-%t '

# Usage: ' basename $ '


Eof

Fi

Vim + $

#以下这段脚本, if the script syntax has errors, follow the prompts to return to edit changes

Until Bash-n $ &>/dev/null; Do

Read-p "Syntax error,q| Q for Quiting,else for editing: "OPTIONS

Case $OPTIONS in

q| Q)

echo "Quiting ..."

Exit 8;;

*)

Vim + $;

Esac

Done

chmod +x $




This article is from the "Xavier Willow" blog, please be sure to keep this source http://willow.blog.51cto.com/6574604/1789102

A detailed array of bash programming in Linux

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.