虽然未找到命令并且在意外标记附近出现语法错误

虽然未找到命令并且在意外标记附近出现语法错误

我正在尝试编写一个 shell 脚本,它接受用户输入的字符串,询问文件名并报告该字符串是否存在于文件中。以下是我当前的脚本。

#!/bin/bash  
while :  
       do  
       echo "Please enter a string"  
       read input_string  
       echo "Please enter the file name to see if that string is present in it -  (Enter .abw after)"  
     read input_string1  
     grep -q "${input_string}" "${input_string1}"       
     if grep -q $input_string $input_string1 ; then  
         echo  "Your string has been found"    
     else   
         echo "Your string has not been found"  
      fi  
 done

当我运行脚本时它说

Line 2: while:: command not found
Line 3: syntax error near unexpected token 'do'
Line 3: 'do'

如果有人能指出我正确的方向,我将不胜感激。

答案1

您实际上并不需要两条 grep 行,相反您不妨输入:

if grep -q "$input_string" "$input_string1" ; then

echo "Your string has been found"

else

echo "Your string has not been found"

fi

相关内容