免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1420 | 回复: 8
打印 上一主题 下一主题

FIND的naive实现(呵呵) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-11-16 15:00 |只看该作者 |倒序浏览
①支持双参数格式,即./naivefind filename./naivefind  /etc/xxxx filename
②统计搜索到的文件个数并显示出显示结果的绝对路径和名称
③判断/etc/xxxx 是否存在,不存在的话就提示错误,而不是简单的从当前目录下搜索,因为这样做也许是画蛇添足,比如我当前目录下文件非常多,搜索要半天,而用户只是由于输入错误,而你非要给用户在当前目录搜索,显然是不必要的.
④很多人find shell程序在遇到存在无权限访问的文件夹时会死循环,不断的试图进入,但却无法进入,在这里我做除了了异常处理,如果存在这个文件夹但却进不去,那我自动去选择下一个文件夹做递归,而给出无法进入的文件夹路径,当然shell 本身可以提示你没有访问权限.但是如果不处理的话,死循环仍然无法避免,除非改设计.
④但是利用递归效率不高,因为存在多级子目录,而且文件比较多时,效率很低.需要等待很长时间

#!/bin/sh
#Naive Find for Linux 2.4.18
#written by magicgiant CS NJU http://magicgiant.blog@bbs.nju.edu.cn/
#wriitten for OS Experiment 1
#/^    ^\
#    (..)  ZZZzzzzz.......
#FF is a sub function that maches the dir or the file
FF(){
#get name of file or dir from the list
for I in $(ls|tr '\n' ' ')
do
   if [ "$I" == "$FN" ]
   then echo "$PWD"/"$FN"
#one found ,count increases
   count=$((count+1))
   fi
done
}

REC(){
#SB is the sub dir of the dir that the programm in
   for SB in $(ls -F|grep '/$'|sed 's/\$//'|tr '\n' ' ')
   do
#i think pwd1 and pwd2 is the best in the shell,here they used for the case that one dir you have no right to visit.and then we can jump the dir to the #next one.if not ,the shell will go into a dead circul,it'll try again and again to go into the dir,but fail foever
   pwd1=$PWD
   cd $SB
   pwd2=$PWD
   if [ "$pwd1"!="$pwd2" ] ;
   then
   FF
   REC
   cd ..
   else
   echo "$PWD"/"$SB"/"you may not have the right or somthing else to visit!"
   fi
   done
}
#for the case that ./naivefind   filename style
WU(){
FF
REC
}
#for the case that ./naivefind /etc/xxxx  filename style ,in this naive shell ,it is the same as the function WU,but in advanced shell it'll be different
YOU(){
FF
REC
}

#in the beginning ,count = 0
count=0
#we can judge $2,here is a trick.for I don't know how to write the case that $2 is NULL,so I use $3 as the judgement
case $2 in
`echo "$3" `)
#FN =$1
FN=$1
WU;;
#$2 is not NULL
*)
FN=$2
#here pwd1 and pwd2 used for the cases that ./naivefind /etc/xxxx fliename ,whichi /etc/xxxx is not exist.though it can find filename from the dir #wherethe shell stand in,but it is not clever.it sometimes may be " hua she tian zu "------a snake but you give it feet.
pwd1=$PWD
cd $1
pwd2=$PWD
if [ "$pwd1" = "$pwd2" ];
then
#judge if the dir you give $1 exists
echo "lease make sure that you have input a right dir!"
exit
fi
YOU;;
esac
#judge count and output different message!
case $count in
0)
echo "There is no such file!"
;;
*)
echo "There is "$count" files found!"
;;
esac

论坛徽章:
0
2 [报告]
发表于 2004-11-16 15:05 |只看该作者

FIND的naive实现(呵呵)

bash下调试通过,算是所有当中最好,最全面的一个了~呵呵,还要谢谢大家的那天的批评和帮助了
[root@localhost root]# chmod +x naivefind
[root@localhost root]# ./naivefind src
/root/src
/root/linux/arch/m68k/ifpsp060/src
There is 2 files found!
[root@localhost root]# ./naivefind /home magicgiant
/home/magicgiant
/home/magicgiant/magicgiant
/home/magicgiant/xuhui/magicgiant
/home/magicgiant/xuhui/magicgiant/magicgiant
There is 4 files found!
[root@localhost root]# ./naivefind /home magicgiant1
There is no such file!
[root@localhost root]# ./naivefind sdsss
There is no such file!
[root@localhost root]#

论坛徽章:
0
3 [报告]
发表于 2004-11-16 16:48 |只看该作者

FIND的naive实现(呵呵)

.........好~~好~~好~~~~~~~~~~~~~长啊

论坛徽章:
0
4 [报告]
发表于 2004-11-17 11:30 |只看该作者

FIND的naive实现(呵呵)

#/^ ^\
# (..) ZZZzzzzz.......

此图像是程序运行的真实写照。

论坛徽章:
0
5 [报告]
发表于 2004-11-17 11:49 |只看该作者

FIND的naive实现(呵呵)

牛逼!~~~确实是猪头点`不过我才看了1晚上哎```能把权限问题考虑到了不错了`难道说我们一个系都是猪头吗``?我做的是最好的了`

论坛徽章:
0
6 [报告]
发表于 2004-11-17 11:57 |只看该作者

FIND的naive实现(呵呵)

你们一个系都是布置这作业吗?叫系主任出来检讨一下,哈哈
看一晚上SHELL就能写出这样的东西是挺不错

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
7 [报告]
发表于 2004-11-17 16:12 |只看该作者

FIND的naive实现(呵呵)

不错~~~,不过,用如此复杂的shell完成find这样的功能,有些....,还不如看find的源代码 , 或者直接用C这样的语言实现呢~,其实这样,又悖于UNIX精神的(各人感觉)

论坛徽章:
0
8 [报告]
发表于 2004-11-17 16:18 |只看该作者

FIND的naive实现(呵呵)

这个题目是助教选的````本来准备做定时器的``但是没多少时间了``

论坛徽章:
0
9 [报告]
发表于 2004-11-18 13:40 |只看该作者

FIND的naive实现(呵呵)

强!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP