LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 778|回复: 3

新手。。。新手。。求助。。

[复制链接]
发表于 2007-3-20 22:39:21 | 显示全部楼层 |阅读模式
在没有接触过linux的时候。。老师留了这么一道题。。。
让我们写一个bash script删除空文件夹。。。
折腾了好久好久。。终于高出一个差不多的来。。
但是最后一行还是有错误。。实在没法了。。求救。。

#!bin/bash
# Filename:clearDir

for direc in $1/*
do
        echo $direc

if test -d $direc
        then
                bash $0 $direc
fi

n=0

for direc in $1/*
do
        n=`expr $n + 1`
done

echo $n

if $n -eq 0
        then rmdir $direc
fi
done

他说eq那里有错。。。其他的逻辑错误我显然还不知道。。。
发表于 2007-3-20 22:58:16 | 显示全部楼层
执行后:
  1. test/test
  2. test/test/ok
  3. 2
  4. ./cleanDir.sh: line 23: 2: command not found
  5. test/test/test
  6. test/test/test/*
  7. 1
  8. ./cleanDir.sh: line 23: 1: command not found
  9. 2
  10. ./cleanDir.sh: line 23: 2: command not found
  11. 1
  12. ./cleanDir.sh: line 23: 1: command not found
复制代码
没怎么搞明白
回复 支持 反对

使用道具 举报

发表于 2007-3-20 23:54:36 | 显示全部楼层
第一,如果可以用rm -rf的话,就无需做递归了。
第二,如果不允许,那么如下:
  1. #!/bin/bash
  2. for direc in `ls $1/`
  3. do
  4. echo $direc
  5. if test -d $direc
  6. then
  7. cd $direc
  8. `basename $0` .
  9. cd ..
  10. rmdir $direc
  11. else
  12. rm $1/$direc
  13. fi
  14. done;
复制代码

结果:
  1. peter@Thinkpad:~/temp$ tree .
  2. .
  3. |-- 1.txt
  4. |-- 2.txt
  5. |-- 3.txt
  6. |-- test1
  7. |   |-- 4.txt
  8. |   `-- test3
  9. |       `-- 5.txt
  10. `-- test2
  11.     `-- 6.txt
  12. 3 directories, 6 files
  13. peter@Thinkpad:~/temp$ mysh .
  14. 1.txt
  15. 2.txt
  16. 3.txt
  17. test1
  18. 4.txt
  19. test3
  20. 5.txt
  21. test2
  22. 6.txt
  23. peter@Thinkpad:~/temp$ ls
  24. peter@Thinkpad:~/temp$
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-3-21 21:20:33 | 显示全部楼层
呵呵,谢谢大家了~~~收获不小~
现在知道我那里的错误了。。。
最后的eq 没有用test。。土鳖。。
继续学习~~~~
回复 支持 反对

使用道具 举报

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

本版积分规则

快速回复 返回顶部 返回列表