统计文件种类数+获取子shell返回值的其它方法
生活随笔
收集整理的這篇文章主要介紹了
统计文件种类数+获取子shell返回值的其它方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
只是作為一個shell的小小練習和日常統計用,瞎折騰的過程中也是摸到了獲取子shell返回值的幾種方法;
肯定還有別的方法,跟進程間的通信相關,希望你能提出建議和補充,謝謝~
完整程序:
#! /bin/bash
#檢查參數數量是否符合要求
if [ $# -ne 1 ]
thenecho How to use: $0 ---> basepath!exit -1
fi
#獲取傳入的路徑參數
path=$1
#聲明一個關聯數組
declare -A typearraywhile read fileinfo
doftype=`file -b "$fileinfo" | cut -d , -f 1`let typearray["$ftype"]++
done< <( find $path -type f -print )echo '==File==Type==Stastics=='
for ftype in "${!typearray[@]}"
doecho $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
轉載于:https://www.cnblogs.com/annsshadow/p/5153872.html
總結
以上是生活随笔為你收集整理的统计文件种类数+获取子shell返回值的其它方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: “怀哉聊一陈”下一句是什么
- 下一篇: Python如何进行cross vali