Use of shell arrays

Source: Internet
Author: User

Definition: array= (1 2 3)

Echo ${array[0]}

Echo ${array[1]}

Echo ${array[2]}

echo ${array[*]} all elements

Echo ${array[@]} The length of the array

Array Assignment

Array= (1 2 3) There are 3 elements in this array

Array[3]=4 4 elements in this array

Delete

unset array[0] removed the No. 0 element

Intercept

Array= (1 2 3 4 5)

${array[@]:1:3} intercept number 1th to 3rd

Results 2 3 4

Echo ${array[@]:3:2}

Results 4 5

Array substitution

Echo ${ARRAY[@]/5/6} Replaces 5 in the array with 6, temporarily in effect, and the element family has not been modified

Results 1 2 3 4 6

Turn the result into an array

array= ($ (LS))

Print

All elements ${array[@]} or ${array[*]}

Array length ${#array [@]} or ${#array [*]}

Single element ${array[i]}

Using an example of an array,

gamelist=${pwd}/gamelist forPidinch`awk '{print $7}'$gamelist ' DoPingtai=`awk '/\<" "$pid" "\>/{print $4}'$gamelist ' Daqu=`awk '/\<" "$pid" "\>/{print $6}'$gamelist ' Destdir=${pingtai}cq_lycqly_s$daqu dbname=${pingtai}cq_s$daqu Dbip=`awk '/\<" "$pid" "\>/{print}'$gamelist ' array=($(awk '/\<" "$pid" "\>/{print}'$gamelist))  forIinch{0..2}                                     DoLanip=`Echo${array[i]}|Cut-D:-F1 ' Port=`Echo${array[i]} |Cut-D:-F2 'Echo${array[*]}Echo---------------------------------------------------------------# array=($(Echo${array[@]:1}))                                        Echo$lanipEcho--------------------------------------------------------------Echo$port Done                                 Done
View Code

Use of shell arrays

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.