这是我的一道笔试题,现场做了,感觉不好,回来又仔细修改、测试了一下。现在发出来,希望大家多提宝贵意见:
要求:写一个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
本文介绍了一个Shell脚本,该脚本能够递归地搜索指定目录(或当前目录)中包含特定关键字“conclusion”的文本文件,并输出这些文件的完整路径。
1511

被折叠的 条评论
为什么被折叠?



