Shell Script---Array

Source: Internet
Author: User
Tags array definition

Array definition: The so-called array, is the same data type of elements in a certain order of the set, that is, the finite type of the same variable with a name command, that is, these variables are defined as a group, they do not have their own name, then how do we find the variables or elements? is by index or subscript (sometimes called subscript variable)


Array Features:

1. Arrays are collections of elements of the same data type

2, the elements in the array are sequential, they put together in memory in this order, that is to say: The array is a contiguous memory of a space.

3. The array element is represented by the entire array name and its own sequential position in the array, a[0] means: The first element in array A (or the first component) and so on, the array Subscript (index) in the bash shell starts at 0.


Example: Now there are AA, BB, CC, DD, EE, ff Six elements to create these files under/tmp in their name, using the For Loop way. How do you write it?

Let's start by talking about the array assignment method:

The first method:

array= (aa bb cc dd EE ff separate each element with a space.

Second method: Using key-value pairs (key=value)

array= ([0]=AA [1]=BB] [2]=CC [3]=DD] [4]=ee [5]=FF]

The third method: You can use DECLARE-A to declare a variable type as an array type and assign it later.

Declare-a ARRAY

Array[0]=aa

Array[1]=bb

Array[2]=cc

Array[3]=dd

Array[4]=ee

Array[5]=ff

This method assigns a value to the array, but there is a flaw, which is a bit silly when the array component (element) is relatively large.


The fourth method: Use the result of the system execution command as an array element.

That is: array= ($ (LS)) this way


Declare-a array #先声明一个变量, array type

values= "AA bb cc dd EE ff" #将数组中的各个元素保存在一个变量中用空格作为分隔符

Array= ($ (Echo ${values}))


To write this script: The following text-mode ....

###########################################################

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/86/7C/wKioL1fAUS-guN7LAABlPoKffA4785.png "title=" capture. PNG "alt=" Wkiol1faus-gun7laablpokffa4785.png "/>############################################################# ###########################

#!/bin/bash

#Author: Wangergui email:[email protected] date:2016-08-26

#Release 1.0

#Function: Auto Create files (practice array)

Path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:~/bin

Export PATH

files= "AA bb cc dd EE ff"

Declare-a ARRAY

Array= ($ (Echo ${files}))

index=$ ((${#ARRAY [@]}-1))

function Check () {

if [!-F "$"];then

return 0

Else

Return 1

Fi

}

For I in $ (seq 0 ${index});d O


Check/tmp/${array[${i}]}


If [$?-eq 0];then


Touch/tmp/${array[${i}]}


Fi






















This article from the "Linux-related technology" blog, declined to reprint!

Shell Script---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.