查询包含指定字符文件

#!/bin/bash
#基于zenity GUI 的脚本
#使用前请先安装 sudo apt install zenity
#查询特定字符的文件 并显示路径
FGF_B='====================================================='
FGF_D=`printf %-55.5s`
FGF_C=`printf %-50s`
if [ ! -f "/bin/zenity" ];then
zenity --error --width=380  --title="错误信息" --text="$FGF_B\n||$FGF_C你没有安装  zenity$FGF_C||\n$FGF_B" || exit
else
echo "zenity OK"
fi
#自定义函数 myfind
myfind()
{
echo -e "查询速度与你 电脑配置有关 请耐性等待\r开始查询 包含 $name_text 字符的文件 查询结束后 ===> 会显示完成\r"

if [ "$1" == "" ]; then

   echo -e "请输入后面的关键字 $0\.\n\n"
    exit 0
fi

keyword=$1

dir=.
if [ "$2" != "" ]; then
    dir=$2
fi

test ! -d $dir && echo -e "这 $dir 在您的系统中不存在\.\n\n" && exit 0

count=0
filelist=`ls -R $dir 2> /dev/null | grep -v '^$'`
for filename in $filelist
do
    temp=`echo $filename | sed 's/:.*$//g'`
    if [ "$filename" != "$temp" ]; then
        curdir=$temp
        #echo "current dir = $curdir"
    else
        filetype=`file $curdir/$filename | grep "text"`
        if [ "$filetype" != "" ]; then
            temp=`grep $keyword $curdir/$filename 2> /dev/null`
            #echo $curdir/$filename
            if [ "$temp" != "" ]; then
                echo $curdir/$filename
                count=$(($count+1))
            fi
        fi
    fi
done

echo -e "\n总数: $count"
echo -e "完成"
}
VideoTitle="查询包含指定字符的文件"
#每个路径用 | 分开
Box_dir="/home/$USER|/opt|/usr|/srv|/var|/sys|/tmp|/proc|/run|/etc|/cdrom|/dev|/bin|"
name_text_a=""

#选择 路径 对话框
while true ; do 
    name_dir=$(zenity --forms --width=520 --height="150" --title="$VideoTitle" --text="选择查询路径" --add-combo="请选择" --combo-values="$Box_dir") || exit 

   if [ -z "$name_dir" ];then
        zenity --error --width=380 --height=150 --title="选择错误" --text "$FGF_B\n||$FGF_D选择不能为空$FGF_D||\n$FGF_B"
    else
        break;         
    fi
done
 
#输入 字符 对话框     
while true ; do
    name_text=$(zenity --entry --width=520 --title="$VideoTitle" --text="请出入字符" --entry-text="$name_text_a" )
    if [ -z "$name_text" ];then
        zenity --error --width=380 --height=150 --title="输入错误" --text "$FGF_B\n||$FGF_D输入不能为空$FGF_D||\n$FGF_B"
    else
        break;         
    fi
done
                              #显示文字对话框
myfind $name_text $name_dir | zenity --text-info --title="$VideoTitle" --width=800 --height=600 || exit

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值