shell数组

发布时间:2021-08-03 22:12:30 阅读:1673次

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# a=(1 2 3 4 5 6)
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#

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:shell数组 出自老鄢博客 | 欢迎分享