统计文件种类数+获取子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返回值的其它方法:

  1. 使用管道
#这段放在while之前
if [ ! -p npipe ]
then mkfifo -m 777 npipe
fi
#替换掉获得ftype值那里
( file -b "$fileinfo" | cut -d , -f 1 > npipe & )
read ftype<npipe
  1. 使用临时文件
#替换掉获得ftype值那里
( file -b "$fileinfo" | cut -d , -f 1 )>temp.txt
read ftype<temp.txt
  1. 使用HERE文档
#替换掉获得ftype值那里
read type << HERE
`file -b "$fileinfo" | cut -d , -f 1`
HERE
posted @   AnnsShadoW  阅读(905)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示