linux中用shell去掉源码注释
来源:互联网 发布:北京数据录入 编辑:程序博客网 时间:2024/05/01 02:04
Linux中使用Shell脚本去掉源码注释的方法
该文件用来终极删除注释,操作系统限linux
#!/bin/bash
###the script use to delete comment
if [ -z "$1" ];then #检测是否输入了源文件
echo "ipput the source file"
exit 1
fi
postfix=`echo $1 |cut -f 2 -d '.'` #取文件的后缀,以便根据不同的文件进行不同的处理
if [ -n "$2" ];then #存放处理源文件产生的信息的文件
target_file=$2 #如果给出了就直接调用
touch ${target_file}
else
prefix=`echo $1|cut -f 1 -d '.' ` #如果没有给出的话则生成一个和源文件名字一样,但后缀为temp的文件
target_file=${prefix}.temp
touch ${target_file}
fi
case "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 -f
function del_comment_file()
{
#C++模式注释的删除。
#Delete the line begins with //
#首先删除//在行首的行
sed -i '/^[ \t]*\/\//d' $file
#Delete the line ends with //
#注意已经除去了 //在行首的情况,下面匹配除去URL的//部分,因为代码中有一部分中有
#URL,形如fun("ftp://")
sed -i 's/\/\/[^"]*//' $file
#删除以C语言中注释只在一行的行
sed -i 's/\/\*.*\*\///' $file
#Delete the lines between /* and */
#删除C语言中注释跨行的情况
sed -i '/^[ \t]*\/\*/,/.*\*\//d' $file
}
function del_comment()
{
#$Dir=`pwd`
for file in `ls`;do
case $file in
*.c )
del_comment_file
;;
*.cpp )
del_comment_file
;;
*.h )
del_comment_file
;;
* )
if [ -d $file ];then
cd $file
del_comment
cd ..
fi
;;
esac
done
}
Dir=$1
if [ ! -e $Dir ];then
echo "The Directory isn't exist."
exit
fi
cd $Dir
del_comment
#!/bin/bash
###the script use to delete comment
if [ -z "$1" ];then #检测是否输入了源文件
echo "ipput the source file"
exit 1
fi
postfix=`echo $1 |cut -f 2 -d '.'` #取文件的后缀,以便根据不同的文件进行不同的处理
if [ -n "$2" ];then #存放处理源文件产生的信息的文件
target_file=$2 #如果给出了就直接调用
touch ${target_file}
else
prefix=`echo $1|cut -f 1 -d '.' ` #如果没有给出的话则生成一个和源文件名字一样,但后缀为temp的文件
target_file=${prefix}.temp
touch ${target_file}
fi
case "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 -f
function del_comment_file()
{
#C++模式注释的删除。
#Delete the line begins with //
#首先删除//在行首的行
sed -i '/^[ \t]*\/\//d' $file
#Delete the line ends with //
#注意已经除去了 //在行首的情况,下面匹配除去URL的//部分,因为代码中有一部分中有
#URL,形如fun("ftp://")
sed -i 's/\/\/[^"]*//' $file
#删除以C语言中注释只在一行的行
sed -i 's/\/\*.*\*\///' $file
#Delete the lines between /* and */
#删除C语言中注释跨行的情况
sed -i '/^[ \t]*\/\*/,/.*\*\//d' $file
}
function del_comment()
{
#$Dir=`pwd`
for file in `ls`;do
case $file in
*.c )
del_comment_file
;;
*.cpp )
del_comment_file
;;
*.h )
del_comment_file
;;
* )
if [ -d $file ];then
cd $file
del_comment
cd ..
fi
;;
esac
done
}
Dir=$1
if [ ! -e $Dir ];then
echo "The Directory isn't exist."
exit
fi
cd $Dir
del_comment
0 0
- linux中用shell去掉源码注释
- 用shell脚本去掉源码的注释
- 利用Shell脚本去掉C/C++源码中的注释
- Linux Shell脚本去掉几类常见文件中的注释
- LINUX SHELL 注释
- Linux SHELL脚本 注释
- linux shell中用&&代替if
- Linux shell 去掉滴滴声音
- linux文本中去掉#号的注释
- 【转】linux shell -- 块注释
- 去掉注释
- linux中用shell获取时间,日期
- linux中用shell获取时间,日期
- Linux shell 源码
- linux shell 的多行注释
- 如何去掉linux配置文件的注释行和空行
- Linux Shell系列教程之(四)Shell注释
- 使用shell脚本统计源码文件中的注释行数.(// , /**/)
- WWDC 2013 Session笔记 - iOS7中的多任务
- Media Queries
- 利用cookie和session实现购物记录和购物车
- 法布尔与昆虫记
- Validate Binary Search Tree
- linux中用shell去掉源码注释
- Java 增强型的for循环 for each 【转载】
- cocos2dx 实现gallery (五)
- AndroidStudio第一次使用细节
- Android ANR 问题
- 完美解决MySql数据库乱码问题
- js正则表达式语法
- 作业4.19
- Codeforces Round #298 (Div. 2)——A.B.C.D.E