shell操作數組
Shell代碼
./test.sh "100,200 300,400 500,600" hhhh
www.2cto.com
說明:參數分隔符默認是空格,所以數組參數用雙引號擴上,
Shell代碼
dd=`date +%Y%m%d_%H%M%S`
echo "start datetime ${dd}"
arrayListParam=$1
printName=$2
arrayList=($arrayListParam)
echo "length = ${#arrayList[@]}"
i=0
while [ $i -lt ${#arrayList[@]} ];do
firstsplit=`echo "${arrayList[$i]}" | cut -d "," -f1`
secondsplit=`echo "${arrayList[$i]}" | cut -d "," -f2`
echo "${printName}_${firstsplit}:${secondsplit}"
let i=i+1
done
dd=`date +%Y%m%d_%H%M%S`
echo "end datetime ${dd}"
顯示結果:
Shell代碼
start datetime 20130305_181428
length = 3
hhhh_100:200
hhhh_300:400
hhhh_500:600
end datetime 20130305_181428