#!/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
04-17
1600
09-27
339