shell for循環(huán)與數(shù)組應用介紹
讀取一個文件,把每一行賦值給一個數(shù)組的變量,然后用for循環(huán)打印出來
#!/bin/sh
i=1
SUM=`sed -n '$=' tmp.txt` #計算文件的總行數(shù)
echo "$SUM"
while read line
do
arr[$i]="$line"
i=`expr $i + 1`
done < tmp.txt
echo "$i"
i=1
for i in `seq $SUM` ;do
echo "${arr[$i]}"
done
附:shell數(shù)組補充知識
一、聲明數(shù)組
1)array[key]=value # array[0]=one,array[1]=two
2)declare -a array # array被當作數(shù)組名
3)array=( value1 value2 value3 ... )
4)array=( [1]=one [2]=two [3]=three ... )
5)array="one two three" # echo ${array[0|@|*]},把array變量當作數(shù)組來處理,但數(shù)組元素只有字符串本身
二、訪問數(shù)組
1)${array[key]} # ${array[1]}
三、刪除數(shù)組
1)unset array[1] # 刪除數(shù)組中第一個元素
2)unset array # 刪除整個數(shù)組
四、計算數(shù)組的長度
1)${#array}
2)${#array[0]} #同上。 ${#array[*]} 、${#array[@]}。注意同#{array:0}的區(qū)別
- Shell中的循環(huán)語句for、while、until實例講解
- shell中的for循環(huán)用法詳解
- linux shell循環(huán):for、while、until用法詳解
- Shell中的for和while循環(huán)詳細總結
- Shell中的for循環(huán)總結
- 基于Shell中for循環(huán)的幾個常用寫法分享
- linux shell 中數(shù)組的定義和for循環(huán)遍歷的方法
- Linux shell 實現(xiàn)用for循環(huán)100次的方法
- Shell腳本用for循環(huán)遍歷參數(shù)的方法技巧
- shell編程中for循環(huán)語句的實現(xiàn)過程及案例
相關文章
一鍵配置CentOS iptables防火墻的Shell腳本分享
這篇文章主要介紹了一鍵配置CentOS iptables防火墻Shell腳本分享,可保存到一個腳本文件中,在新安裝的CentOS系統(tǒng)時一條命令搞定iptables配置,需要的朋友可以參考下2014-07-07Linux oracle數(shù)據(jù)庫自動備份自動壓縮腳本代碼
這篇文章主要介紹了Linux oracle數(shù)據(jù)庫備份完成后可以自動壓縮腳本代碼,大家參考使用2013-12-12