#!/bin/bash###the script use to delete commentif [ -z "$1" ];then #檢測(cè)是否輸入了源文件 echo "ipput the source file" exit 1fipostfix=`echo $1 |cut -f 2 -d ’.’` #取文件的后綴,以便根據(jù)不同的文件進(jìn)行不同的處理if [ -n "$2" ];then #存放處理源文件產(chǎn)生的信息的文件 target_file=$2 #如果給出了就直接調(diào)用 touch ${target_file}elseprefix=`echo $1|cut -f 1 -d ’.’ ` #如果沒(méi)有給出的話則生成一個(gè)和源文件名字一樣,但后綴為temp的文件 target_file=${prefix}.temp touch ${target_file}ficase "postfix" in sh ) echo "it is shell script !" sed ’s/[[:space:]]#.*//g’ $1 |sed ’/^#/d’|\ sed ’/^[[:space:]]*$/d’ | sed ’/^$/d’ >${target_file} echo "the source file is $1,target file is ${target_file}" ;; c|java) echo "c or java program" sed ’s/\/\*.*\*\///g’ $1|sed ’/\/\*/,/.*\*\//d’ |\ sed ’s/\/\/.*//g’ |\ sed ’/^[[:space:]]*$/d’ |sed ’/^$/d’ >${target_file} echo "the source file is $1,target file is ${target_file}" ;; *) echo "unknown file type !" rm -f ${target_file} ;;esac//-------------------------#!/bin/sh -ffunction del_comment_file(){#C++模式注釋的刪除。#Delete the line begins with //#首先刪除//在行首的行 sed -i ’/^[ \t]*\/\//d’ $file#Delete the line ends with //#注意已經(jīng)除去了 //在行首的情況,下面匹配除去URL的//部分,因?yàn)榇a中有一部分中有#URL,形如fun("ftp://")sed -i ’s/\/\/[^"]*//’ $file#刪除以C語(yǔ)言中注釋只在一行的行sed -i ’s/\/\*.*\*\///’ $file#Delete the lines between /* and */#刪除C語(yǔ)言中注釋跨行的情況sed -i ’/^[ \t]*\/\*/,/.*\*\//d’ $file}function del_comment(){#$Dir=`pwd`for file in `ls`;docase $file in*.c )del_comment_file;;*.cpp )del_comment_file;;*.h )del_comment_file;;* )if [ -d $file ];thencd $filedel_commentcd ..fi;;esacdone }Dir=$1if [ ! -e $Dir ];thenecho "The Directory isn’t exist."exitficd $Dirdel_comment
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。