用于检查文件名是否存在的while循环 [英] while loop for checking a file name exists or not
本文介绍了用于检查文件名是否存在的while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
filename=""
while [[ $filename = "" ]];do
echo "Enter the file name"
read filename
if [[ -f $filename ]];then
echo "$filename exists"
else
echo "File name doesn't exists, so re-enter"
fi
done
使用这种循环样式,当文件名不存在时循环存在.但我希望循环继续,直到用户输入文件名.请建议我应该在上述脚本中包含哪些附加命令.
with this style of loop, the loop exists when the file name is not present. But i want the loop to continue until the user input the file name. Please suggest what additional command i should include in the above script.
推荐答案
使用break
跳出循环
while true ; do
echo "Enter the file name"
read filename
if [ -f "${filename}" ]; then
echo "${filename} exists"
break
else
echo "File name doesn't exists, so re-enter"
fi
done
echo "Thanks for ${filename}"
或更短
while true ; do
read -p "Enter the file name: " filename
test -f "${filename}" && break
echo "File name doesn't exists, so re-enter"
done
echo "Thanks for ${filename}"
这篇关于用于检查文件名是否存在的while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文