bash output command not found

可紊 提交于 2019-12-31 01:45:24

问题


Im facing following problem I have created mentioned condition, but when I choose y for yes everything is ok, but when I choose n for not I get annoying error output: output : Do you agree yes (y) or not (n) n ./myscript: [n: command not found

myscript is the name of my script Code 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 can I get rid of the output and fix the problem ? thanks


回答1:


That's not bash. "done" does not terminate an "if" condition in bash. You should remove "done" and add "fi" at the end of the else body.

Also, the semicolon after "exit 1" is not needed.




回答2:


You missed the space in:

["$answer" = 'Y' ]

Change to:

[ "$answer" = 'Y' ]

There are also other mistakes in the script. Here you have working code:

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



回答3:


You are missing a space after the [ in your second condition. [ is actually a command and since it's together it tries to literally run [n. You don't see the output with y because the evaluation is short circuited (i.e. the first condition is true so there's no need to evaluate the second).




回答4:


done should be after the else clause, i.e., before exit. and it is "fi" not "done".



来源:https://stackoverflow.com/questions/9831768/bash-output-command-not-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!