文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>向(指定文件)(指定行)[添加,删除]注释

向(指定文件)(指定行)[添加,删除]注释

时间:2010-11-30  来源:kevin33643

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

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载