为生存而奔跑

   :: 首页 :: 联系 :: 聚合  :: 管理
  271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

留言簿(5)

我参与的团队

搜索

  •  

积分与排名

  • 积分 - 319337
  • 排名 - 75

最新评论

阅读排行榜

评论排行榜

创建一个名叫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


posted on 2010-05-30 11:16 baby-fly 阅读(145) 评论(0)  编辑 收藏 引用 所属分类: Ubuntu&Linux

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理