linux循环打印检索结果,shell 脚本完成在目录或者文件中查找输入的内容,并且输出含有输入字符串的文件名称,但是得不到正确的结果。...

各位好,写了下面这个脚本,完成在目录或者文件中查找输入的内容,并且输出含有输入字符串的文件名称,但是得不到正确的结果。

估计在这一句话:

grep -l '$findstring' $targetpath$append

在下面echo出来了之后单独运行,又能正确出结果、小弟很纳闷。望各位高手解答,谢谢~!

#!/bin/sh

echo "请输入你要查找的内容:"

read findstring

echo "请输入你要超找的路径或者文件"

read targetpath

while test -z $findstring

do

echo "输入为空请重新输入:"

read findstring

done

while test -z $targetpath

do

echo "路径或者文件为空请重新输入:"

read targetpath

done

append='/*'

if test -d $targetpath || test -f $targetpath

then

grep -l '$findstring' $targetpath$append

echo $targetpath$append

echo "grep -l '$findstring' $targetpath$append"

else

echo "不是一个文件或者路径"

fi

|

#!/bin/sh

echo "请输入你要查找的内容:"

read findstring

echo "请输入你要超找的路径或者文件"

read targetpath

while test -z $findstring

do

echo "输入为空请重新输入:"

read findstring

done

while test -z $targetpath

do

echo "路径或者文件为空请重新输入:"

read targetpath

done

append="/*"

if test -d $targetpath || test -f $targetpath

then

grep -l "$findstring" $targetpath$append

echo $targetpath$append

echo "grep -l '$findstring' $targetpath$append"

else

echo "不是一个文件或者路径"

fi单引号不会变量扩展,要双引号。

|

#!/bin/sh

echo "请输入你要查找的内容:"

read findstring

echo "请输入你要超找的路径或者文件"

read targetpath

while test -z $findstring

do

echo "输入为空请重新输入:"

read findstring

done

while test -z $targetpath

do

echo "路径或者文件为空请重新输入:"

read targetpath

done

append="/*" #赋值错误,要双引号

if test -d $targetpath || test -f $targetpath

then

grep -l "$findstring" $targetpath$append #单引号不能变量扩展

echo $targetpath$append

echo "grep -l '$findstring' $targetpath$append"

else

echo "不是一个文件或者路径"

fi

|

问题在这

if test -d $targetpath || test -f $targetpath

应该分开目录和文件

按你的脚本文件后面会加上/* 这肯定会错的

if test -d $targetpath || test -f $targetpath

then

if test -d $targetpath

then

echo "目录"

grep -l '$findstring' $targetpath$append

fi

if test -f $targetpath

then

echo "文件"

grep -l '$findstring' $targetpath

fi

else

echo "不是一个文件或者路径"

fi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值