2010. 5. 29. 19:47
#! /bin/sh

usage(){
    echo "Usage: $0 [-a] [-f] [-h] [-p] [-s] [Directory]"
    echo " [Option] -a (-A) : directory and file"
    echo "          -f (-F) : output to file (dtree.text)"
    echo "          -h (-H) : help"
    echo "          -s (-S) : with block-number"
    exit 1
}

DIR=`pwd`
OPT=""
OPT1=""

tailr() {
    awk '{
        line[NR] = $0   
    }
    END {
        for(i=NR; i>0; i--) print line[i]
    }'
}

for kinta in $*
do
    case $kinta in
        -[aA]) OPT="-a"  ;;
        -[sS]) OPT1="-s" ;;
        -[fF]) dtree.sh $OPT $OPT1 $DIR > dtree.text
               echo " dtree.text [done]"
               exit 0    ;;
        -[hH]) usage     ;;
        *)  if [ -d $kinta ] ;then
                echo -n ""
            else
                echo -n "Directory or Option not found : "
                echo $kinta
                usage
            fi
            DIR=`(cd $kinta; pwd)`
            break 1
            ;;
    esac
done

#du | awk | tailr

du $OPT $DIR |

awk '
BEGIN {
    save_cnt=0
    base_cnt = length("'"$DIR"'")
    for(i=1; i<100; i++)
        nest[i]=0
    opt="'"$OPT1"'"
}
{
    cnt=1
    for(i=length($2); i>base_cnt; --i) {
        if(substr($2,i,1) == "/")
            cnt++
    }
    len=0
    for(scnt=length($2); scnt; --scnt) {
        if(substr($2,scnt,1) == "/") {
            scnt++
            break
        }
        len++
    }

    nest[cnt] = 1
    if (cnt < save_cnt) {
        nest[save_cnt] = 0
    }
    else if (cnt > save_cnt) {
        for(i=1; i<=save_cnt; ++i) {
            if(nest[i] == 1)
                printf "    |"
            else
                printf "     "
        }
        printf "\n"
    }
    for(i = 1; cnt != 1 && i <= cnt; ++i) {
        if (nest[i] == 1)
            printf "    |"
        else
            printf "     "
    }
    if (cnt == 1) {
        if (opt == "-s" )
            printf "   %s(%d)\n", $2, $1
        else
            printf "   %s\n", $2
    }
    else {
        if (opt == "-s")
            printf "-- %s(%d)\n", substr($2, scnt,len), $1
        else
            printf "-- %s\n", substr($2, scnt, len)
    }
    save_cnt = cnt
}
' | tailr


'Linux > ShellScript' 카테고리의 다른 글

ANSI Escape(ESC) character  (0) 2010.11.02
쉘내부 명령어  (0) 2010.10.28
[Script] 파일 점검 프로그램  (0) 2010.05.23
[if연산] test 관련 연산자  (0) 2010.05.23
쉘(Shell) 환경  (0) 2010.05.16
Posted by devanix