如果输入的字符串不在文件中,为什么我的代码不输出。当我输入一个字符串但它不在文件中时,没有响应,它会重新循环回到开头。有人可以告诉我我的代码有什么问题吗?
while :
do
echo "Please enter a string"
read input_string
echo "Please enter the file name too see if that string is present in it - (Enter .abw after)"
read input_string1
if grep -q $input_string $input_string1 ; then
echo "Your string has been found"
fi
done
答案1
while :
do
echo "Please enter a string"
read input_string
echo "Please enter the file name too see if that string is present in it - (Enter .abw after)"
read input_string1
grep -q "${input_string}" "${input_string1}"
if [ $? -eq 0 ] ; then
echo "Your string has been found"
else
echo "Your string has not been found"
fi
done
答案2
您找到了丢失的 else 分支,但有一个建议:
而不是使用$input_string $input_string1
try${input_string} ${input_string1}
只是为了确保后面不会$input_string
跟着 1。