創(chuàng)建一個(gè)名叫DELSTAR的shell腳本,它接受輸入并
查看輸入的內(nèi)容是否是一個(gè)目錄,并可讀寫。
如果是,那么目錄里所有的普通文件將被一個(gè)一個(gè)被
列出,對每個(gè)可寫的普通文件,用戶會被詢問是否要
刪除文件,如果是,那么文件將被刪除,不然的話查
看下一個(gè)文件。
執(zhí)行到最后,腳本將顯示下面的信息:
——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
標(biāo)準(zhǔn)答案
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