统计文件种类数+获取子shell返回值的其它方法
前言
只是作为一个shell的小小练习和日常统计用,瞎折腾的过程中也是摸到了获取子shell返回值的几种方法;
肯定还有别的方法,跟进程间的通信相关,希望你能提出建议和补充,谢谢~
完整程序:
#! /bin/bash
#检查参数数量是否符合要求
if [ $# -ne 1 ]
then
echo How to use: $0 ---> basepath!
exit -1
fi
#获取传入的路径参数
path=$1
#声明一个关联数组
declare -A typearray
while read fileinfo
do
ftype=`file -b "$fileinfo" | cut -d , -f 1`
let typearray["$ftype"]++
done< <( find $path -type f -print )
echo '==File==Type==Stastics=='
for ftype in "${!typearray[@]}"
do
echo $ftype : ${typearray["$ftype"]}
done
获取子shell返回值的其它方法:
- 使用管道
#这段放在while之前
if [ ! -p npipe ]
then mkfifo -m 777 npipe
fi
#替换掉获得ftype值那里
( file -b "$fileinfo" | cut -d , -f 1 > npipe & )
read ftype<npipe
- 使用临时文件
#替换掉获得ftype值那里
( file -b "$fileinfo" | cut -d , -f 1 )>temp.txt
read ftype<temp.txt
- 使用HERE文档
#替换掉获得ftype值那里
read type << HERE
`file -b "$fileinfo" | cut -d , -f 1`
HERE
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)