Bash 脚本有助于 if then else 和区分大小写
好的,我正在尝试检查文件是否存在,如果存在,则我为用户提供再次下载该文件的选项 - 我希望默认值(输入)为 Y,我希望 Y 或 y 继续脚本,我希望 N 或 n 退出脚本,并且我希望所有其他响应返回并重新提示问题......但我坚持这一点。
我所做的实际上只是继续(输入),并且除了小写 y 之外的所有其他响应都失败。
这里是:
if [ -f $target/$remote_backup ];then
read -p "This file already exists, do you still want to download? [Y/n]" decide
if [ -z $decide ];then
# if you press return it'll default to Y and continue
decide="Y"
else
if [ $decide != y ]; then
echo "Ok you said no or pressed a random button, exiting"
exit -1
fi
fi
fi
Ok I'm trying to have a situation where I check if a file exists, if it does then I give the user the option to download it again - I want the default (enter) to be Y, I want Y or y to continue the script, I want N or n to exit the script, and I want all other responses to go back and re-prompt the question... but I'm stuck on that.
What I've done really just continues on (enter), and fails on all other responses other than lowercase y.
Here it is:
if [ -f $target/$remote_backup ];then
read -p "This file already exists, do you still want to download? [Y/n]" decide
if [ -z $decide ];then
# if you press return it'll default to Y and continue
decide="Y"
else
if [ $decide != y ]; then
echo "Ok you said no or pressed a random button, exiting"
exit -1
fi
fi
fi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常使用的结构是
case
。The usual structure to use for this is
case
.尝试使用
while
循环:Try a
while
loop: