linux shell 关联数组 hash
http://blog.csdn.net/ysdaniel/article/details/7909824
[root@web]# cat exe.sh
#!/bin/bash
#echo $1
if [ ! -n "$1" ] ;then
echo "请输入路径,如[ 20170711/huayangnianhua ]"
exit
fi
if [ ! -n "$2" ] ;then
echo "
选择以下数据库
+------------------------------+
| Database |
+------------------------------+
| 1 test |
| 2 huayangnianhua |
| 3 qq_log |
| 4 u_log |
| 5 bibei_log |
| 6 pro_log |
| 7 uefi_log |
| 8 player_log |
+------------------------------+
18 rows in set (0.02 sec)
"
exit
fi
declare -A phone
database=([1]="log1" [2]="log2" [3]="qq_log" [4]="udashi_log" [5]="bibei_log" [6]="pro_log" [7]="uefi_log" [8]="player_log" )
db=${database[$2]}
echo "数据库为: "$db
sqllist=`find $1 -name "*.sql"`
if [ $? -ne 0 ]
then
echo "路径不对呀"
fi
for sql in $sqllist
do
echo $sql
echo --------------------------------------------------------------
name=${sql%.*}
#echo $name
#echo --------------------------------------------------------------
name=${sql%.*}
echo $name.txt
mysql -h10.10.10.20 -uroot -p -D $db < $sql
if [ $? -eq 0 ]
then
echo "ok"
mv $sql $name.txt
fi
done
普通数组
1 #!/bin/bash
2 filename=(`ls *.txt`)
3 for((i=0;i<${#filename[*]};i++))
4 do
5 echo ${filename[i]}
6 done
#!/bin/bash
filename=(`ls *.txt`)
for var in ${filename[@]};do
echo $var
echo ${filename[1]}
done
root@test:/home/test/shell# echo ${a[0]}
1
root@test:/home/test/shell# echo ${a[1]}
2
root@test:/home/test/shell# echo ${a[2]}
3
root@test:/home/test/shell# echo ${a[2]}
3
root@test:/home/test/shell# echo ${a[@]}
1 2 3 4 5 6
root@test:/home/test/shell# echo ${a[*]}
1 2 3 4 5 6
root@test:/home/test/shell# echo ${#a[*]}
6
root@test:/home/test/shell# for((i=0;i<${#a[*]};i++))
> do
> echo ${a[i]}
> done
1
2
3
4
5
6
关联数组
root@test:/home/test/shell# declare -A arraytest
root@test:/home/test/shell# arraytest[username]=test
root@test:/home/test/shell# arraytest[age]=30
root@test:/home/test/shell# echo ${arraytest[username]}
test
root@test:/home/test/shell# echo ${arraytest[age]}
30
root@test:/home/test/shell# echo ${!arraytest[@]}
username age
root@test:/home/test/shell# echo ${!arraytest[*]}
username age
root@test:/home/test/shell# echo ${#arraytest[*]}
2
root@test:/home/test/shell# echo ${#arraytest[@]}
2
root@test:/home/test/shell#