Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1165446
  • 博文数量: 241
  • 博客积分: 4385
  • 博客等级: 上校
  • 技术积分: 2383
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-07 23:13
文章分类

全部博文(241)

文章存档

2013年(1)

2012年(8)

2011年(62)

2010年(109)

2009年(61)

分类: LINUX

2010-11-30 14:01:24

ctsOpenClose.sh

#/bin/sh
# usage: ./ctsOpenClose.sh [path] (-o,-c)
#     -o: Open cts
#     -c: Close cts
# ps: This script only support two keywords

##### set the value you want here #####
keyword1="&2"
keyword2="HELLO"
filename="inpath.sh"

##### get path and parameter #####
path="./"
if [ $# -eq 0 ]
then
    echo "Usage: $0 path paramater" >&2
    exit 1
elif [ $# -eq 1 ]
then
    para=$1
elif [ $# -eg 2 ]
then
    path=$1
    para=$2
fi

##### check parameter #####
case $para in
-o) echo "Open cts" ;;
-c) echo "Close cts" ;;
*) echo "Invalid parameter" >&2
exit 1
esac

##### main #####
files=`find $path -name $filename`
for file in $files
do
    if [ $para == "-c" ]
    then
        ./addComment.sh "$file" "$keyword1" "$keyword2"
    else    
        ./delComment.sh "$file" "$keyword1" "$keyword2"
    fi    
    echo $file
done    

exit 0

addComment.sh

#/bin/sh

if [ $# -ne 3 ]
then
    echo "Usage: $0 filename keyword1 keyword2" >&2
    exit 1
fi    

keyword1="$2"
keyword2="$3"

addComment()
{
    while read LINE
    do
        if [ -z "$(echo $LINE | grep $keyword1)" ] && [ -z "$(echo $LINE | grep $keyword2)" ]
        then
            echo $LINE >>$2
        elif [ ! -z "$(echo $LINE | grep $keyword1)" ]
        then
            echo $LINE | grep $keyword1 | sed -e "s/^/#/g" >>$2
        elif [ ! -z "$(echo $LINE | grep $keyword2)" ]
        then
            echo $LINE | grep $keyword2 | sed -e "s/^/#/g" >>$2
        fi
    done < $1        
}

touch "$1.tmp"
addComment "$1" "$1.tmp"
rm -rf $1
mv "$1.tmp" "$1"

exit 0

delComment.sh

#/bin/sh

if [ $# -ne 3 ]
then
    echo "Usage: $0 filename keyword1 keyword2" >&2
    exit 1
fi    

keyword1="$2"
keyword2="$3"

delComment()
{
    while read LINE
    do
        if [ -z "$(echo $LINE | grep $keyword1)" ] && [ -z "$(echo $LINE | grep $keyword2)" ]
        then
            echo $LINE >>$2
        elif [ ! -z "$(echo $LINE | grep $keyword1)" ]
        then
            echo $LINE | grep $keyword1 | sed -e "s/#//g" >>$2
        elif [ ! -z "$(echo $LINE | grep $keyword2)" ]
        then
            echo $LINE | grep $keyword2 | sed -e "s/#//g" >>$2
        fi
    done < $1        
}

touch "$1.tmp"
delComment "$1" "$1.tmp"
rm -rf $1
mv "$1.tmp" "$1"

exit 0

阅读(1339) | 评论(1) | 转发(0) |
0

上一篇:find,whereis,locate,which

下一篇:vsftpd的配置

给主人留下些什么吧!~~

chinaunix网友2010-12-01 15:26:24

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com