我正在编写一个脚本(用于教育目的),但我有点卡在这里。程序要求输入一个现有文件名并检查该文件名是否确实存在。如果不存在,则循环重复,直到您填写一个确实存在的文件。到目前为止一切顺利!但是当您输入一个确实存在的文件名时,我希望脚本进入下一个 while 循环,输入路径文件。但它没有继续。我怎样才能进入下一个 while 语句???
clear
echo "Your filesystem is threatned, files should be moved in order to guarantee safety!!!!"
while read FILE
do
if [ -f $FILE ];
then
echo "File is safe to secure"
else
echo "Too late, we lost the file, safe another!"
fi
done
echo "Time is running out, we must secure this file inmidiately, quick give me a safe location!"
while read PATH
do
if [ -d $PATH ] && [ -f $FILE ];
then
echo "the location is secure! Move the file!"
else
echo "Either the file or the safehouse is corrupt, quick try again!"
fi
done
答案1
这有效:
clear
echo "Your filesystem is threatned, files should be moved in order to guarantee safety!!!!"
while read file; do
if [[ -f "$file" ]]; then
echo "File is safe to secure"
break
else
echo "Too late, we lost the file, safe another!"
fi
done
echo "Time is running out, we must secure this file inmidiately, quick give me a safe location!"
while read path; do
if [ -d "$path" ]; then
echo "the location is secure! Move the file!"
break
else
echo "Either the file or the safehouse is corrupt, quick try again!"
fi
done
您需要添加break
语句以在找到有效文件/路径时退出循环。另外,不要使用大写字母作为变量名。