if-else文でgrepを使用する[閉じる]

if-else文でgrepを使用する[閉じる]

入力文字列がファイルにない場合、コードが出力されないのはなぜですか?文字列を入力しましたが、ファイルに存在しない場合は応答がなく、最初に再度繰り返されます。誰かが私のコードにどのような問題があるのか​​教えてもらえますか?

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

おすすめ記事