Chinaunix

标题: shell练习题!!帮帮忙 [打印本页]

作者: a65751844    时间: 2012-03-27 14:09
标题: shell练习题!!帮帮忙
  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`这句话实现啊
作者: Shell_HAT    时间: 2012-03-27 19:29
  1. for file in $(find /backup/ttt -type d -name "[0-9]")
  2. do
  3.     echo $file
  4.     num=`ls "$file" | wc -l`
  5.     if [ $num -le 5 ];then
  6.         echo 1
  7.     else
  8.         echo 2
  9.     fi
  10. done
复制代码

作者: w630866139    时间: 2012-03-27 21:28
回复 2# Shell_HAT

第一行括号里面的是什么意思啊?没见过哎、、、求解释~
   
作者: Shell_HAT    时间: 2012-03-27 21:42
回复 3# w630866139


命令替换
作者: w630866139    时间: 2012-03-27 21:45
回复 4# Shell_HAT

汗、我知道是命令替换、只是没见过/backup/ttt -type d -name、我是想问下这个

   
作者: Shell_HAT    时间: 2012-03-27 22:17
回复 5# w630866139


find /backup/ttt -type d -name "[0-9]"
http://bbs.chinaunix.net/thread-441883-1-1.html
作者: w630866139    时间: 2012-03-27 22:38
回复 6# Shell_HAT


    谢谢!
作者: a65751844    时间: 2012-03-28 15:12
本帖最后由 a65751844 于 2012-03-28 15:23 编辑

回复 6# Shell_HAT


    谢了大哥。已经搞定了
作者: a65751844    时间: 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,为什么明明存在但是这样判断确是不存在
作者: Shell_HAT    时间: 2012-03-29 15:48
回复 9# a65751844


把双引号删掉
作者: a65751844    时间: 2012-03-29 16:05
回复 10# Shell_HAT


    。。琢磨半天刚才删了OK了,大哥nb
作者: a65751844    时间: 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’都试过了还是不行,愁啊
作者: chenyx    时间: 2012-03-30 09:57
ls | grep "1\..*.tar.gz"
1.20120328.tar.gz
1.20120329.tar.gz

作者: a65751844    时间: 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 这两个进行比较然后把旧的删除。但是这里我的逻辑乱了,




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2