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