Shell Array Common operations

Source: Internet
Author: User

1. See the script for details

#!/bin/bash# declaring an array declare -a test#j=0#cat > test.file << eof#1#2#3#4#5# eof# writes the value of the file to the array #for line in $ (cat test.file) #do #        test[${j}]=${line}#       let j+=1#done# defines the contents of an array test[0]=1test[1]=2test[2 ]=3test[3]=4test[4]=5# Print array contents echo ${test[@]} #打印数组所有元素: ${array_name[@]} or ${array_name[*]}# Print array elements   way one echo  "One way to print all of the array elements" for i in ${test[@]}do        echo ${i}done# Printing array elements   Mode two echo  "Another way to print all of the array elements" for  i in ${test[*]}do        echo ${i}done# Get Array Length # Law one echo  "Get array length" echo  "solution 1" echo ${#TEST [*]}echo  "solution  2 "#法二echo  ${#Test[@]} #打印数组的第四个元素echo   "Print the fourth array element" echo ${test[3]}# Delete the fourth element of the array echo  "Delete fourth array element" unset test[3]for i in ${ test[@]}do        echo ${i}done# delete the entire array and no more print group elements after deletion unset  Testfor i in ${test[@]}do        echo ${i}done


2. Implementation results

[[Email protected] ~]# sh a.sh 1 2 3 4 5one A-print all of the array elements12345another A-to-print all of the ARR Ay elements12345get array lengthsolution 15solution 25print the fourth array element4delete Fourth array element #此处可以看到没 There is a print out of the deleted array element 41235


Shell Array Common operations

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.