shell练习--关于关联数组自增统计判断的学习
今天在书上看到了一个关联数组 let statarray["$ftype"]++ 这样一个操作,用来做索引的自增统计,所以记下来
#!/bin/bash
#统计文件类型
#关于关联数组 用let ++ 值自增的验证 if [ $# -ne 1 ];
then
echo "Usage is $0 basepath";
exit
fi path=$1 declare -A statarray; #定义一个关联数组 for line in $(find $path -type f -print) #通过 find的-print 将输出给for循环
do
ftype=`file -b "$line" | cut -d , -f1` #通过file命令获取文件类型,并赋值给ftype
let statarray["$ftype"]++; #自增操作,此处有疑问,关联数组的赋值也是直接在本条语句中赋值的吗?
echo "$ftype:${statarray["$ftype"]}"
done
echo ================= file types and counts =====================
for ftype in "${!statarray[@]}";
do
echo $ftype : ${statarray["$ftype"]}
done
输出结果:
wyf349@ubuntu:~/user/study_shell$ ./filestat.sh /home/wyf349/user/study_shell
ASCII text:1
UTF-8 Unicode text:1
UTF-8 Unicode text:2
ASCII text:2 #由输出结果可知,对应索引值为ASCII的值,在做自增操作。
empty :1
ASCII text:3
empty :2
ASCII text:4
Bourne-Again shell script:1
ASCII text:5
ASCII text:6
ASCII text:7
Bourne-Again shell script:2
ASCII text:8
Bourne-Again shell script:3
ASCII text:9
UTF-8 Unicode text:3
Bourne-Again shell script:4
Bourne-Again shell script:5
ASCII text:10
ASCII text:11
================= file types and counts =====================
empty : 2
Bourne-Again shell script : 5
ASCII text : 11
UTF-8 Unicode text : 3
shell练习--关于关联数组自增统计判断的学习的更多相关文章
- shell 之awk 关联数组高级应用
最近由于数据迁移过,有些用户信息需要再次确认下,也许数据量比较大,但是需要最终确认的比如说是用户ID和其对应的用户积分数,这样就会导致出现文本a(老的数据),文本b(新的数据).比如 这是文本a.tx ...
- 【Linux】shell字符串分割、数组访问、条件判断
参考资料: shell字符串分割再循环:http://www.shangxueba.com/jingyan/1633455.html linux shell中 if else以及大于.小于.等于逻辑表 ...
- Shell数组的增删改查
Shell数组的增删改查 shell数组的定义及取值: a=(1 2 3) [root@bogon tmp]# echo ${a[*]} 1 2 3 [root@bogon tmp]# echo $ ...
- [ SHELL编程 ] 数组、关联数组和awk数组
本文主要对shell编程中常用的数组.关联数组和awk数组定义.操作以及注意事项做个总结,并提供具体案例. 数组 数组定义:一对圆括号表示数组,数组元素之间用空格符号分割. Array=(val1 v ...
- SHELL脚本编程-普通数组(列表)和关联数组(字典)
SHELL脚本编程-普通数组(列表)和关联数组(字典) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数组相关概述 变量: 存储单个元素的内存空间 数组: 存储多个元素的连续的 ...
- 使用bash关联数组统计单词
使用bash关联数组统计单词 从bash 4开始支持关联数组,使用前需要声明,即 declare -A map map[key1]=value1 map[key2]=value2 map=([key1 ...
- shell编程之数组和关联数组
一.数组类似c语言的数组 1.两种赋值方式 可以整体定义数组:ARRAY_NAME=(value0 value1 value2 value3 ...) 此时数组的下标默认是从0开始的 还可以单独定义数 ...
- shell之数组和关联数组
数组和关联数组 #!/bin/bash #定义数组1 array_var1=(1 2 3 4 5 6)# #定义数组2 array_var[0]="test1" array_var ...
- Linux shell —— 数组与关联数组
使用 declare -A(declare 的用法请使用 help 进行查看,help declare) 进行声明关联数组变量: $ declare -A fruits_price $ fruits_ ...
随机推荐
- How do I add a Foreign Key Field to a ModelForm in Django?
What I would like to do is to display a single form that lets the user: Enter a document title (from ...
- 【HANA系列】SAP HANA SLT在表中隐藏字段并传入HANA的方法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SLT在表中隐 ...
- MySQL 服务正在启动 MySQL 服务无法启动解决途径
解决方案: 1.删除自己手动创建的data文件夹: 2.管理员权限CMD的bin目录下,移除已错误安装的mysqld服务: mysqld -remove MySQL出现删除成功! 3.在CMD的bin ...
- 【神经网络与深度学习】卷积神经网络-进化史:从LeNet到AlexNet
[卷积神经网络-进化史]从LeNet到AlexNet 本博客是[卷积神经网络-进化史]的第一部分<从LeNet到AlexNet> 如需转载,请附上本文链接:http://blog.csdn ...
- 【VS开发】【图像处理】RGB Bayer Color分析
RGB Bayer Color分析 Bayer色彩滤波阵列 拜耳色彩滤波阵列(Bayer Color Filter Array,CFA)是非常有名的彩色图片的数字采集格式.色彩滤波器的模式如上图所示, ...
- Django项目与mysql交互进行数据迁移时报错:AttributeError: 'str' object has no attribute 'decode'
问题描述 Django项目启动,当我们执行命令 python manage.py makemigrations 出现如下错误: File , in last_executed_query query ...
- 使用filebeat收集日志传输到redis的各种效果展示
0 环境 Linux主机,cengtos7系统 安装有openresty软件,用来访问生成日志信息 1.15.8版本 安装有filebeat软件,用来收集openresty的日志 7.3版本 安装有r ...
- 使用git配置ssh的文章推荐
https://blog.51cto.com/sgk2011/1925922 https://www.cnblogs.com/superGG1990/p/6844952.html https://bl ...
- 一文简单理解package-lock.json
根据官方文档,https://docs.npmjs.com/files/package-lock.json 这个package-lock.json 是在 `npm install`时候生成一份文件,用 ...
- 一、bif
缩进是python的灵魂,缩进可以使python的代码整洁,有层次. python是脚本语言,就是为了简单方便以辅助科学运算,因此python有许多bif,build in function 全部都是 ...