关于if-then-else和区分大小写的Bash脚本帮助
好的,我正在尝试一种情况,我检查一个文件是否存在,如果存在,那么我给用户再次下载的选项-我希望默认值(enter)为Y,我希望Y或Y继续脚本,我希望N或N退出脚本,我希望所有其他响应返回并重新提示问题。。。但我被困在这上面了 我所做的实际上只是继续(回车),并在除小写y以外的所有其他响应上失败 这是:关于if-then-else和区分大小写的Bash脚本帮助,bash,cron,if-statement,case-sensitive,case-insensitive,Bash,Cron,If Statement,Case Sensitive,Case Insensitive,好的,我正在尝试一种情况,我检查一个文件是否存在,如果存在,那么我给用户再次下载的选项-我希望默认值(enter)为Y,我希望Y或Y继续脚本,我希望N或N退出脚本,我希望所有其他响应返回并重新提示问题。。。但我被困在这上面了 我所做的实际上只是继续(回车),并在除小写y以外的所有其他响应上失败 这是: if [ -f $target/$remote_backup ];then read -p "This file already exists, do you still want to
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
尝试
而循环:
if [ -f $target/$remote_backup ]; then
decide="?"
while [ "$decide" != "y" -a "$decide" != "n" ]; do
read -p "This file already exists, do you still want to download? [Y/n] " decide
if [ -z $decide ]; then
decide="y"
fi
done
echo Decision: $decide
fi
尝试而循环:
if [ -f $target/$remote_backup ]; then
decide="?"
while [ "$decide" != "y" -a "$decide" != "n" ]; do
read -p "This file already exists, do you still want to download? [Y/n] " decide
if [ -z $decide ]; then
decide="y"
fi
done
echo Decision: $decide
fi
通常的结构是case
case "$decide" in
y|Y|'') echo "yes" ;;
n|N) echo "no" ;;
*) echo "boo" ;;
esac
通常的结构是case
case "$decide" in
y|Y|'') echo "yes" ;;
n|N) echo "no" ;;
*) echo "boo" ;;
esac
我想我不明白,这段时间过后会继续吗?这样地。。。如果[-f$target/$remote_backup];然后,[“$decise”!=“y”-a“$decise”!=“n”];是否读取-p“此文件已存在,是否仍要下载?[Y/n]”决定是否[-z$decise];然后决定“y”fidone@beatbreaker,而则在第一个if
之后。我用更多的上下文更新了我的答案。我想我不明白,while在后面吗?这样地。。。如果[-f$target/$remote_backup];然后,[“$decise”!=“y”-a“$decise”!=“n”];是否读取-p“此文件已存在,是否仍要下载?[Y/n]”决定是否[-z$decise];然后决定“y”fidone@beatbreaker,而则在第一个if
之后。我用更多的上下文更新了我的答案。那是在阅读之后吗?我不会回应我不认为,或者在这种情况下我需要回应吗?如果他们按n,我可以退出-1吗?如果他们回答*,我如何循环?将其放入循环中。在N上,退出
。在Y上,break
。告诉用户输入一个有效选项。如果不将用户从脚本中踢出,我无法要求用户输入有效选项,以便在读取之后执行?我不会回应我不认为,或者在这种情况下我需要回应吗?如果他们按n,我可以退出-1吗?如果他们回答*,我如何循环?将其放入循环中。在N上,退出
。在Y上,break
。告诉用户输入一个有效选项。如果不让用户退出脚本,我无法要求用户输入有效选项