Bash output command not found

I'm facing the following problem, I created the specified condition, but when I select y for yes, everything is fine, but when I select n for I don't get annoying error output: Output: Do you agree yes (y) or no (n) N ... / myscript: [n: command not found

myscript is the name of my script The code is here:

echo  "Do you agree yes (y) or not (n)"
read  answer
if ( [ "$answer" =  'y' ]  ||  ["$answer" = 'Y' ]);
then

echo  -e  "  output for y"
done
else
echo -e "  output for n "
exit 1;

      

Any idea how I can get rid of the output and fix the problem? thank

+3


source to share


4 answers


This is not bash. "done" does not terminate the "if" condition in bash. You must remove "done" and add "fi" to the end of the else body.



Also, no semicolon after "exit 1" is required.

+1


source


You missed a space in:

["$answer" = 'Y' ]

      

Change to:



[ "$answer" = 'Y' ]

      

There are other errors in the script as well. You have working code here:

echo  "Do you agree yes (y) or not (n)"
read  answer
if ( [[ "$answer" =  'y' ]] || [[ "$answer" = 'Y' ]]);
then
 echo -e "  output for y"
else
 echo -e "  output for n"
exit 1
fi

      

+6


source


You are missing a space after [

in your second state. [

is actually a team, and because it is together, it literally tries to run [n

. You don't see the result with y

because the evaluation is short-circuited (i.e. the first condition is true, so there is no need to evaluate the second).

+5


source


done must be after the else clause, that is, before exiting. and this "fi" is not "done".

0


source







All Articles