免费注册 查看新帖 |

Chinaunix

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

shell练习题!!帮帮忙 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-27 14:09 |显示全部楼层 |倒序浏览
  for file in $(find /backup/ttt -type d -name "[0-9]")
         do
                echo $file
                num=`cd "$file" |ls|wc -l`
                pwd
                if [ $num -le 5 ];then
echo 1
                else
echo 2
                fi
        done


我是想判断这个目录下数字起名的文件夹个数是否大于5,但是现在出现的pwd时的路径不是我$file的路径而是我脚本所在的路径。怎么让num=`cd "$file" |ls|wc -l`这句话实现啊

论坛徽章:
0
2 [报告]
发表于 2012-03-28 15:12 |显示全部楼层
本帖最后由 a65751844 于 2012-03-28 15:23 编辑

回复 6# Shell_HAT


    谢了大哥。已经搞定了

论坛徽章:
0
3 [报告]
发表于 2012-03-29 13:36 |显示全部楼层
回复 2# Shell_HAT


    麻烦帮我看一下:
#! /bin/bash
        #! /bin/bash
        dir=/backup/ttt
        for file in $(find /backup/ttt   -name "[0-9]*" -type d -exec basename {} \
         do
                echo $file
                cd "$dir/$file"
                num=`ls|wc -l`
                if [ $num -ge 5 ];then
                cd "$dir"
                        if [ ! -f "$file.*.all.gz" ];then
                tar -zcf $file.`date '+%Y%m%d'`.all.tar.gz $file
                else
                    echo "you le"
                fi
                else
                cd "$dir"
                tar -zcf $file.`date '+%Y%m%d'`.tar.gz $file
                fi
        done


我第二个if -f判断会出现问题,我目录下确实有¥file date。all。gz 压缩包但是在执行他还会再压缩,如果把我不去反 if [  -f "$file.*.all.gz" ];他就会显示 you le,为什么明明存在但是这样判断确是不存在

论坛徽章:
0
4 [报告]
发表于 2012-03-29 16:05 |显示全部楼层
回复 10# Shell_HAT


    。。琢磨半天刚才删了OK了,大哥nb

论坛徽章:
0
5 [报告]
发表于 2012-03-30 09:37 |显示全部楼层
回复 2# Shell_HAT


    大哥我这个转义出不来啊,帮我看下问题在哪里啊,
[root@www ceshi]# ls
1.20120328.tar.gz
1.20120329.tar.gz
2.20120328.tar.gz
2.20120329.tar.gz
3.20120328.tar.gz
3.20120329.tar.gz
4.20120328.tar.gz
4.20120329.tar.gz
[root@www ceshi]# ls | grep "3.*.tar.gz"
1.20120328.tar.gz
1.20120329.tar.gz
2.20120328.tar.gz
2.20120329.tar.gz
3.20120328.tar.gz
3.20120329.tar.gz
4.20120328.tar.gz
4.20120329.tar.gz
[root@www ceshi]# ls | grep "4"."*.tar.gz"
4.20120328.tar.gz
4.20120329.tar.gz
只要过滤包含1、2、3着三个的都不行,只能过滤后面第二段日期没有的才行, ls | grep "1"."*.tar.gz"   , ls | grep "1.*.tar.gz" , ls | grep ‘1.*.tar.gz’都试过了还是不行,愁啊

论坛徽章:
0
6 [报告]
发表于 2012-03-30 11:02 |显示全部楼层
回复 14# chenyx


    大哥不行啊,你看
ls | grep "2\..*.tar.gz"
12.20120328.tar.gz
12.20120329.tar.gz
12.20120330.tar.gz
2.20120328.all.tar.gz
22.20120328.tar.gz
22.20120329.tar.gz
22.20120330.tar.gz
32.20120328.tar.gz
32.20120329.tar.gz
32.20120330.tar.gz
52.20120328.tar.gz
52.20120329.tar.gz
52.20120330.tar.gz
这样还是一下包含都出来了,我用find可以实现但是后面的比较久不行了,大哥帮我看下
#! /bin/bash
        dir=/backup/ttt
        for file in $(find /backup/ttt   -name "[0-9]*" -type d -exec basename {} \
         do
                echo $file
                cd "$dir/$file"
                num=`ls|wc -l`
                if [ $num -ge 5 ];then
                cd "$dir"
                        if [ ! -f $file.*.all.tar.gz ];then
                tar -zcf $file.`date '+%Y%m%d'`.all.tar.gz $file
                else
                    echo "you le"
                fi
                else
                cd "$dir"
                tar -zcf $file.`date '+%Y%m%d'`.tar.gz $file
                fi
        done
         for num2 in $(find /backup/ttt/ -name $file.*.tar.gz -exec basename {} \;|wc -l)
         do
        echo $num2      
for mfile in $(find /backup/ttt/ -name $file.*.tar.gz -exec basename {} \;)
         do

        while read  $mfile ;
                do
                if [ $num2 -gt 2 ] && [ "test $line -nt $line1 ;echo $?" =0 ];then
        rm -fr $line1
                fi
                done
         done
我这里的while read是想把第二个过滤出的 比如2.日期1.tar。gz 和2.日期2.tar.gz 这两个进行比较然后把旧的删除。但是这里我的逻辑乱了,
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP