您的位置:首页 > 运维架构 > Shell

Linux Bash:在用户输入目录中查找含有某字符串的文件,打印行号

2008-12-24 12:07 330 查看
#!/bin/bash
######## My homework for Network Operating System#########
######## To find files that contains the entered string###########
#######################################################
if test $# -ne 2####判断参数是否合法
then
echo "Please enter the Directory that you want to search:/c"
read dir#####读入用户输入目录
echo "Please enter the word you want to find:/c"
read word###读入用户输入字符串
dir=${dir}*
if grep -n $word $dir
then
echo "The result is listed above!"
exit 0
else
echo "Sorry,The word was not found."
exit -1
fi
else
dir=$1
word=$2
dir=${dir}*
if grep -n $word $dir#这里我用grep带n参数来过滤出来文件。
then
echo "The result is listed above!"
else
echo "Sorry,The word was not found."
fi
fi
exit 0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐