手動輸入一行字符串,並對其排序。
腳本如下:
#!/bin/bash
#a test about sort
echo "please input a number list"
read -a arrs
for((i=0;i<${#arrs[@]};i++)){
for((j=0;j<${#arrs[@]}-1;j++)){
if [[ ${arrs[j]} -gt ${arrs[j+1]} ]];then
tmp=${arrs[j]}
arrs[j]=${arrs[j+1]}
arrs[j+1]=$tmp
fi
}
}
echo "after sort"
echo ${arrs[@]}
執行效果:
Linux Shell在while中用read從鍵盤輸入 http://www.linuxidc.com/Linux/2015-06/118831.htm
Linux Shell 程序調試 http://www.linuxidc.com/Linux/2015-07/119880.htm
Linux Shell腳本面試25問 http://www.linuxidc.com/Linux/2015-04/116474.htm
Linux/Unix Shell 參數傳遞到SQL腳本 http://www.linuxidc.com/Linux/2013-03/80568.htm
Shell腳本中參數傳遞方法介紹 http://www.linuxidc.com/Linux/2012-08/69155.htm
Shell腳本傳遞命令行參數 http://www.linuxidc.com/Linux/2012-01/52192.htm
Linux Shell 通配符、轉義字符、元字符、特殊字符 http://www.linuxidc.com/Linux/2014-10/108111.htm