Chinaunix首页 | 论坛 | 博客
  • 博客访问: 259373
  • 博文数量: 74
  • 博客积分: 1470
  • 博客等级: 上尉
  • 技术积分: 793
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-25 21:01
文章分类

全部博文(74)

文章存档

2011年(1)

2010年(32)

2009年(32)

2008年(9)

我的朋友

分类: LINUX

2009-09-28 09:21:02

此shell脚本完成:根据用户输入的主目录,显示其下各个子目录中的.txt和.sh文件的数目

#!/bin/sh

#stat one dirctory
stat()
{
    #init the var
    sh_num=0
    txt_num=0

    #check the .sh file
    for name in `ls $1`; do
        suffix=`echo $name | awk '/\.sh$/{print}'`
        if [ -n "$suffix" ]; then
        #if [ $suffix ]; then
            sh_num=$(expr $sh_num + 1)
            continue
        fi

        suffix=`echo $name | awk '/\.txt$/{print}'`
        if [ -n "$suffix" ]; then
        #if [ $suffix ]; then
            txt_num=$(expr $txt_num + 1)
        fi
    done

    echo ".sh file:\t"$sh_num
    echo ".txt file:\t"$txt_num
}

recur()
{
    for d in $*; do
        if [ -d "$d" -a -x "$d" ];then
            echo "${PWD}/$d"
            stat "${PWD}/$d"
            cd "$d"
            recur `ls`
            cd ..
        fi
    done
}

#input check
if [ ! -d "$1" ]; then
    echo "Usage: ./menu.sh dir"
    echo "The directory must be exist"
    exit 0 ;
fi

#save the pwd
PWD=`pwd`

cd $1
recur `ls $1`

#restore the pwd
cd $PWD


阅读(1108) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~