想知道是否有一种简单的方法来做我尝试的事情和最好的方式来做这样的事情。
answer=y
while [ "$answer" = y ]
do
echo "type name of the file you wish to delete"
IFS= read -r file
echo "would you like to delete another file [y/n]?"
IFS= read -r answer
done
echo "Exiting Program"
exit 0
我在这里的问题是当输入输入时它存储在文件中但我希望当答案为y时它可以存储到另一个变量如file1。 然后我留下了包含有人希望删除的所有文件名的变量,我可以回复。
如何在此处使用循环来继续添加输入,直到某人键入n
感谢您的帮助。
答案 0 :(得分:1)
我不会使用实时读取循环来删除文件。太多事情都可能出错。相反,可以采取输入文件并检查每个。
但是对于你的问题的简单回答 -
typeset -l answer=y # force to lowercase
lst=/tmp/file.list.$$
while [[ "$answer" = y ]]
do
echo "type name of the file you wish to delete"
IFS= read -r file
if [[ -e "$file" ]]
then echo "$file" >> $lst
else echo "That file does not exist/is not accessible."
fi
echo "would you like to delete another file [y/n]?"
IFS= read -r answer
done
echo -e "Files to be deleted:\n===\n$(<$lst)"
echo -e "\n===\n Delete all listed files [y/n]?"
IFS= read -r answer
if [[ y = "$answer" ]]
then rm $(<$lst)
fi
rm $lst
答案 1 :(得分:-1)
也许这会有所帮助:
while :; do
read -s -n 1 answer
if [ $answer = "y" || $answer = "Y" ]; then <your code>
elif [ $answer = "n" || $answer = "N" ]; then echo; echo; break
elif [ $answer = "" ]; then continue # pressed: return key
fi
done
取决于您使用的shell,您可以使用:[[$ {answer} =〜(Y | y)]]