虽然很简单,但付出了去做,很有成就感!
实现过程:读入所有.c文件,加入注释;
读入.h文件,加注释
把作者及功能写入文件,shell脚本读入配置文件。
选项(没有实现)
AI:开头已加注释的文件不再写入遇到好多问题,多半是对shell的语法不熟悉,比如判断命令[]与里面的语句没有空格开,
还有把命令也写在判断命令[]里了,
另一类问题就是基本工具使用的不熟练,sed找第一行弄了半天,读文件第一次用,而那个
什么exec根本没用过。
还好最后完成了两个版本:
第一个是把指定的文件夹中所有的C文件,加上头注释;
第二个是指定了要加注释的文件和配置文件,配置文件的格式如下
姓名
一些信息
就是第一行为姓名,其余行为注释信息。
嗯,现在有些混乱,但挺有成就的。
insert.one.sh#!/bin/sh
tmp=.beijing
#usage
if [ $# -ne 2 ]
then
echo "usage:insert.sh source_file info_file"
exit
fi
filename=$1
info=$2
# see if innotation had been added in the past
if sed -n '1p' $filename | grep '\/\*' > /dev/null
then
#probably annotation inserted
echo $filename \\t"........it has already........."
exit
else
echo $filename \\t".......inserting........."
# try to redirect std_output to fd 4
# and then just echo ,no need to >>..
# but not success!
# exec 4<>$tmp
exec 8 read line
echo \/\* > $tmp
#file author
echo \ \* author\\t: \\t $line >> $tmp
#echo \ \* date\\t: \\t `date` >> $tmp
echo \ \* function\\t: >> $tmp
while :
do
read line if [ $? -ne 0 ]
then
exec 8 break
fi
echo \ \* \\t \\t \\t \\t $line >> $tmp
done
#echo \ \* \\t\\t\\t\\t $3 >> $tmp
echo \ \*\/ >> $tmp
# exec 4
#put $1 append to tmp
cat $filename >> $tmp
#put tmp to original file
cat $tmp > $filename
fi
#rm -f $tmpinsert.all.sh#!/bin/sh
tmp=.beijing
#usage
if [ $# -ne 3 ]
then
echo "usage:insert.sh dir author introduction"
exit
fi
for filename in `ls $1 | grep '\.[ch]'`
do
# echo $filename
if sed -n '1p' $filename | grep '\/\*' > /dev/null
then
#probably annotation inserted
echo $filename \\t"........it has already........."
continue
else
echo $filename \\t".......inserting........."
# try to redirect std_output to fd 4
# and then just echo ,no need to >>..
# but not success!
# exec 4<>$tmp
# exec >&4
echo \/\* > $tmp
#file author
echo \ \* author\\t: \\t $2 >> $tmp
#echo \ \* date\\t: \\t `date` >> $tmp
echo \ \* function\\t: >> $tmp
#file function
echo \ \* \\t\\t\\t\\t $3 >> $tmp
echo \ \*\/ >> $tmp
# exec 4
#put $1 append to tmp
cat $filename >> $tmp
#put tmp to original file
cat $tmp > $filename
fi
done
#rm -f $tmp