Shell Programming
A. for Loop
- Build List {start number: End number}
- Command generation list ' seq [number of start] [step length] End number '
For L in {1..5};d o
For L in ' seq 5 ';d o
1 to 100 of the cumulative sum and
1 #!/bin/bash2 declare-i sum=034 for in { 1.. ; Do 5 sum=$[$SUM +$i]6done78"$SUM"
Two. Arrays
Array variable definition
$ arr={1,2,3,4,5,6}
Gets the first element by default
$echo $arr
Access by subscript, following the second element of the array, subscript starting from 0
$echo ${arr[1]}
Accessing the entire array
$echo ${arr[@]} or echo ${arr[*]}
Gets the length of the array
$echo {#arr [@]}
Slice method gets a subset of the array
$echo ${arr[@]:1:2}
Implementation of bubble sort
1#!/bin/Bash2 3Echo"Please input a number list:"4Read-a arr5 6 for((i=0; i<${#arr [@]};i++));7 Do8 for(j=${#arr [@]}-1; j>i;j--));9 DoTen if[[${arr[j]}-lt ${arr[j-1]} ]] One Then At=${arr[j]} -arr[j]=${arr[j-1]} -arr[j-1]=$t the fi - Done - Done - +Echo"After sorting:" -Echo ${arr[@]}
Shell programming-sum and bubble sort from 1 to 100