zoukankan      html  css  js  c++  java
  • 统计文件种类数+获取子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
    
  • 相关阅读:
    OC与AS3的观察者模式比较以及外部回调
    判断矩形重叠
    2010谷歌校园招聘笔试题
    2011谷歌校园招聘笔试题
    2012Google校园招聘笔试题
    2013谷歌校园招聘笔试题2
    2013谷歌校园招聘笔试题
    2012搜狗校园招聘笔试题
    2013搜狗校园招聘笔试题
    搜狐面试题
  • 原文地址:https://www.cnblogs.com/annsshadow/p/5153872.html
Copyright © 2011-2022 走看看