Summary of array operations in shell and bubble sort implementation script sharing _linux Shell

Source: Internet
Author: User

Shell Array Description:

Copy Code code as follows:

Array variable definition:
$ arr= (1 3 6 8 2 5 9 0 7 4)
Get first element by default
$ echo $arr
By subscript access, the second element of the array below, the shell array subscript is starting from 0
$ echo ${arr[1]}
Accessing the entire array
$ echo ${arr[@]} or echo ${arr[*]}
Gets the length of the array (maximum subscript)
$ echo ${#arr [@]}
Get a single element string length
$ echo ${#arr [3]}
Slice to get part of the array contents
$ echo ${arr[@]:1:2}
Start with the second element
$ echo ${arr[@]:2}
to the second element
$ echo ${arr[@]::2}

Shell implementation Bubble Sort:
Copy Code code as follows:

#!/bin/sh
#sorting following array
echo "Please input a number list:"
Read-a arr
for ((i=0; i<${#arr [@]}; i++))
Todo
For ((j=${#arr [@]}-1; j>i; j--))
Todo
#echo $j
if [[${arr[j]}-lt ${arr[j-1]}]
Then
T=${ARR[J]}
ARR[J]=${ARR[J-1]}
arr[j-1]= $t
Fi
Done
Done
echo "After sorting:"
Echo ${arr[@]}
[Tech@ebs sqlee]$./sorting.sh
Please inout a number list:
0 2 9 6 8 5 7 4 3 1
After sorting:
0 1 2 3 4 5 6 7 8 9

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.