创建一个名叫DELSTAR的shell脚本,它接受输入并
查看输入的内容是否是一个目录,并可读写。
如果是,那么目录里所有的普通文件将被一个一个被
列出,对每个可写的普通文件,用户会被询问是否要
删除文件,如果是,那么文件将被删除,不然的话查
看下一个文件。
执行到最后,脚本将显示下面的信息:
——ordinary files deletes from the directory
____ordinary files remaining in the directory
代码如下: 注意第7行,是$path/$file.
echo "input path"
read path
if [ -d $path ]
then
for file in `ls $path`
do
if [ -f $path/$file ]
then
echo "delete file $file?"
read choice
if [ "$choice" = "yes" ]
then
rm $path/$file
fi
fi
done
echo "——ordinary files deletes from the directory"
echo "____ordinary files remaining in the directory"
else
echo "invalid input"
fi
标准答案
1 Echo “enter a directory name:”
2 Read inp
3 If test –d ${inp} –a –r ${inp} –a –w ${inp}
4 Then
5 files_deleted=0
6 files_remaining=0
7 for c in `ls ${inp}`
8 do
9 if test –f $inp/$c –a –w $inp/$c
10 then
11 echo “wish to delete file $c?”
12 read ans
13 if test $ans = ‘y’
14 then
15 rm $inp/$c
16 files_deleted=`expr $files_deleted + 1`
17 else
18 files_remaining=`expr $files_remaining + 1`
19 fi
20 fi
21 Done
22 Echo “$files_deleted ordinary files deleted from the directory”
23 Echo “$files_remaining ordinary files remaining in the directory”
24 Else
25 echo “directory name entered is not a readable or writable directory”
26 fi