【发布时间】:2010-11-11 12:54:49
【问题描述】:
我编写了一个 shell 脚本(称为 Trash.sh),运行时会显示我的“垃圾箱”目录中的所有文件,并询问用户是否希望逐个删除每个文件。脚本如下:
#if directory is empty, display message
if test ! -f ~/dustbin/*
then
echo "Directory is empty"
#if directory is not empty, then display each item and ask to delete
else
for resfile in ~/dustbin/* #for each file in directory, store it in resfile variable
do
if test -f $resfile ; then #if a file exists
echo "Do you want to delete $resfile?"
echo "Enter y or n"
read ans #store user input in ans variable
if test $ans = y ; then
rm $resfile
echo "File $resfile was deleted"
fi
fi
done
fi
这对我来说很好。当用户键入sh Trash.sh 时,脚本会正常运行,每个文件都会显示出来,并询问用户是否删除它。但是我想添加的是用户输入sh Trash.sh -a的选项,并且目录中的所有文件都可以在不确认的情况下自动删除。
我有点不知道如何实现这一点。有什么想法吗?
ps - 我正在使用 Mac OS X 10.6.4 并通过终端完成所有操作
【问题讨论】:
-
请不要用基于答案的新版本替换您的原始版本。它使答案没有任何意义。如果有必要,最好在原版下方添加明确标记的修订版。