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
|
阅读(1333) | 评论(1) | 转发(0) |