此脚本应删除垃圾箱目录的内容。 如果使用-a选项,脚本应从垃圾箱中删除所有文件。 否则,脚本应逐个显示垃圾箱中的文件名,并要求用户确认是否应将其删除
#!/bin/sh
echo " The files in the dustbin are : "
ls ~/TAM/dustbin
read -p " Please enter -a to delete all files else you will be prompted to delete one by one : " filename
read ans
if ["filename" == "-a"]
cat ~/TAM/dustbin
rm -rf*
else
ls > ~/TAM/dustbin
for line in `cat ~/TAM/dustbin`
do
echo "Do you want to delete this file" $line
echo "Y/N"
read ans
case "ans" in
Y) rm $line ;;
N) "" ;;
esac
EDITED VERSION
if test ! -f ~/TAM/dustbin/*
then
echo "this directory is empty"
else
for resfile in ~/TAM/dustbin/*
do
if test -f $resfile ; then
echo "Do you want to delete $resfile"
echo "Y/N"
read ans
if test $ans = Y ; then
rm $resfile
echo "File $resfile was deleted"
fi
fi
done
fi
这可行但现在我得到2个错误之一
第4行需要二元运算符或第4行:许多参数
答案 0 :(得分:1)
我看到一个明显的错误:
rm -rf*
什么时候应该
rm -rf *
询问每个文件删除 - 添加 -i 键
rm -rfi *
答案 1 :(得分:0)
这里有很多问题:
在*
rm
之前缺少一个空格。需要空间以便shell可以识别通配符并将其展开。
您真的要删除当前目录中的所有文件吗?如果没有,请在目录中指定路径rm -rf /path/to/files/*
或cd
,最好使用cd /path/to/files || exit 1
。
我不明白脚本的逻辑。您显示垃圾箱,但如果用户提供-a
,则会使用所有非隐藏文件(ls > dustbin
)覆盖垃圾箱。这就是你想要的吗?
答案 2 :(得分:0)
首先,case "ans" of
只是将字符串“ans”与其他字符串匹配,这显然是错误的,您需要case $ans of
来获取变量{{的值1}}。 ans
也是两个字符串之间的比较,它总是错误的。脚本的第一个参数可以作为if ["filename" == "-a"]
访问(第二个参数为$1
,依此类推)。
请阅读$2
以获取shell编程的基础知识(可在此处找到所有上述注释)。