Linux Shell Programming (3): arrays

Source: Internet
Author: User

Http://snailwarrior.blog.51cto.com/680306/154704BASH only supports one-dimensional arrays, but there is no limit to the number of parameters. declares an array:
declare-a Array
(In fact, you don't have to declare it, you assign it directly to the variable as an array, and bash knows it's an array) Array Assignment:
(1) array= (var1 var2 var3 ... varn)
(2) array= ([0]=VAR1 [1]=var2] [2]=var3 ... [N]=varn]
(3) array[0]=var1
Arrya[1]=var2
...
Array[n]=varn

To calculate the number of array elements:
${#array [@]}Or${#array [*]}Bash's special parameters @ and * Both denote "extended position parameters, starting from 1", but the form is slightly different, but it seems to be common in arrays. Reference array:
echo ${array[n]} To iterate over an array:
filename= (' ls ')
For Var in ${filename[@]};d o
Echo $var
Done practical examples of arrays: (personal collection and collation) 1. Read n string from "standard input", and each input string is saved in arrayi=0
N=5
While ["$i"-lt $n]; Do
echo "Please input strings ... ' expr $i + 1 '"
Read array[$i]
b=${array[$i]}
echo "$b"
i= ' expr $i + 1 '
Done 2. Put the letters in the string into the array, and output to the "standard output"
chars= ' ABCDEFGHIJKLMNOPQRSTUVWXYZ '
for ((i=0; i<26; i++)); Do
array[$i]=${chars: $i: 1}
echo ${array[$i]}
DoneThe interesting place here is ${chars: $i: 1}, which means to get 1 characters starting from the $i position of the chars string. If you change 1 to 3, you get 3 characters ~ The result is: ABC
Bcd
...
Vxy
Xyz
YZ//There is not enough string to get the
z//Not enough string to get 3, apply array to Shell environment variable 3. Apply the array to the shell environment variable (1)Array Assignment:[[email protected] ~]# season= ("srping" "Summer" "Autumn" "Winter")When you find that the assignment is wrong, you can immediately correct it from the new assignment, as the above Spring is written as srping.
Re-assignment: (The original value is overridden)
[[email protected] ~]# season= ("Spring" "Summer" "Autumn" "Winter")Look at the environment variables:
[[email protected] ~]# Set | grep SEASON
season= ([0]= "Spring" [1]= "Summer" [2]= "Autumn" [3]= "Winter") displays the entire array:
[[email protected] ~]# echo ${season[*]}or Echo ${season[@]}
Spring Summer Autumn Winter displays an array element:
[[email protected] ~]# echo ${season[3]}
Winter assigns a value to a single array element:
[email protected] ~]# season[0]= "new_spring"Look at the array again:
[[email protected] ~]# echo ${season[*]}
New_spring Summer Autumn Winter clears the specified single array element:
[email protected] ~]# unset season[2]Clear the entire array:
[email protected] ~]# unset SEASON 4. Apply the array to the shell environment variable (2)"This is a good use!" To the original author praise one! "Use the TR command to convert a carriage return from a file into a space:
[email protected] ~]# cat/etc/shells | tr "\ n" "" ">/tmp/tmp.fileAssigns the contents of a file to an array: (the content before the first carriage return)
[email protected] ~]# read-a Shells </tmp/tmp.file To View array assignment conditions:
[[email protected] ~]# Set | grep "Shells"
shells= ([0]= "/bin/sh" [1]= "/bin/bash" [2]= "/sbin/nologin" [3]= "/bin/tcsh" [4]= "/bin/csh" [5]= "/bin/ksh")

Linux Shell Programming (3): 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.