bash 要求用户继续使用 ay/n 的 Shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2387554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shell script that asks user to continue with a y/n
提问by 32423hjh32423
I have a shell script that I want to ask the user if they want to continue. If they type 'n' and press enter the script will exit.
我有一个 shell 脚本,我想询问用户是否要继续。如果他们输入“n”并按回车键,脚本将退出。
If they press 'y' and enter it will continue to run. I have this at the top of my script but it continues regardless of what I type.
如果他们按“y”并输入,它将继续运行。我在脚本的顶部有这个,但无论我输入什么,它都会继续。
What am I doing wrong ?
我究竟做错了什么 ?
goon=
while [ -z $goon ]
do
echo -n 'Do you want to continue? '
read goon
if [[ $goon = 'n' ]]
then
break
fi
goon=
done
回答by ghostdog74
Use an infinity loop and case/esac like this:
像这样使用无限循环和 case/esac:
while true
do
read -r -p 'Do you want to continue? ' choice
case "$choice" in
n|N) break;;
y|Y) echo 'Do your stuff here';;
*) echo 'Response not valid';;
esac
done
回答by Jumipo
The 'break' statement will exit you out of your while loop.
'break' 语句将使您退出 while 循环。
If you want to exit the script you want to use 'exit'.
如果要退出脚本,请使用“退出”。
回答by Pointy
That works perfectly well for me if I get rid of the doubled square brackets:
如果我去掉双方括号,这对我来说非常有效:
if [ $goon = 'n' ]
回答by sorpigal
Rather than echo + read, just use read -p
而不是 echo + read,只需使用 read -p
read -p "Do you want to continue? " goon
回答by Ranjithkumar T
Try below script
试试下面的脚本
#!/bin/bash
pause ()
{
REPLY=X
while [ "$REPLY" == "X" ] || [ "$REPLY" != "n" ]
do
echo -e "\t\tPress 'n' to continue\t\t\tPress 'x' to quit"
read -n1 -s
case "$REPLY" in
"x") exit ;;
"X") echo "case sensitive!!" ;;
"n") clear ;;
"N") echo "case sensitive!!" ;;
* ) echo "Invalid Option" ;;
esac
done
}
pause
echo "Hi"
am sure this script will give you only two options to move around...
我相信这个脚本只会给你两个选项来移动......
回答by cazlab
I'm less than positive, but it looks as though your if statement will always evaluate to false. Here's a resource on BASH coding that explains how to use conditionals in the way you are attempting to. http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html#ss6.4
我不太积极,但看起来好像你的 if 语句总是评估为假。这是有关 BASH 编码的资源,它解释了如何以您尝试的方式使用条件。 http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html#ss6.4
回答by David Gelhar
I think you mean "exit" instead of "break", if you want the whole script to exit.
如果您希望整个脚本退出,我认为您的意思是“退出”而不是“中断”。
Also, you aren't actually checking for "y", so it loops forever even if they do say "y".
此外,您实际上并没有检查“y”,因此即使他们确实说“y”,它也会永远循环。
if [[ $goon = 'n' ]]
then
exit
fi
if [[ $goon = 'y' ]]
then
break
fi
回答by clyfe
Here's a working example (==instead of =for equality testing)
这是一个工作示例(==而不是=用于相等性测试)
goon=
while [ -z $goon ]
do
echo -n 'Do you want to continue? '
read goon
if [[ $goon == 'n' ]]
then
break
fi
goon=
done
Strange thing, the original works ok for me too ...
奇怪的是,原来的作品对我来说也不错......

