shell递归实文件内容过滤

         这是我的一道笔试题,现场做了,感觉不好,回来又仔细修改、测试了一下。现在发出来,希望大家多提宝贵意见:

         要求:写一个shell脚本,递归实现文件查找。输入参数1为查找目录,如果没有则为当前目录,查找目录中内容包含“conclusion”单词的文本文件,输出文件全路径

#! /bin/bash
# Directory judge:if $1 is empty,then use current directory
if [[ -z "$1" ]] || [[ ! -d "$1" ]] ; then #目录判断,如果$1不是目录或者是空,则使用当前目录
    nowdir=$(pwd)
else
    nowdir=$(cd $1; pwd)
fi
echo "$nowdir"


# recursion solution for text file content filter
searchfile() {
        old_IFS="$IFS"
IFS=$'\n'            #IFS修改
        for chkfile in $1/*
        do
                if [[ -f "$chkfile" ]] && [[ -n $(file $chkfile | grep text) ]] ; then # file type judgement   
                    result=$(grep conclusion $chkfile) # "conclusion" is filter string
    if [ ! -z "$result" ] ; then
printf "file: "
                        echo $chkfile 
                    fi
                fi


                if [[ -d $chkfile ]] ; then
                        searchfile $chkfile
                fi
        done
        IFS="$old_IFS"
}


searchfile $nowdir


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值