一个自动生成c程序模板的SHELL脚本-建立自己的c数据结构与算法库系列(3)

本文介绍了一个用于自动化创建C源文件、头文件及Makefile的bash脚本。该脚本可以根据用户输入生成带有注释的C文件和头文件,并创建包含配置选项的Makefile。此外,还支持立即编辑生成的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    如果你使用有点”IDE“的环境,自动生成的东西一大堆,那这个就不必用啦。我从WINDOWS转入LINUX没多久,所以玩玩^_^.
   不说废话啦,调用此脚本将实现:
  1。生成c文件和相应的头文件
  2。在生成的文件中自动生成注释
  3。生成Makefile,此Makefile中导入一个配置文件.FelixInit内容是:
      FELIXMAKESO=no
      FELIXLIBNAME=libfelix.a
      FELIXLIBALIAS=felix
      FELIXLIBSONAME=libfelix.so
      FELIXDEST=/root/programming/FelixAlg/lib
      FELIXHEADER=/root/programming/FelixAlg/header
      FELIXHOME=/root/programming
      cc=gcc
      CFLAGS=-g -I$(FELIXHEADER) -L$(FELIXDEST) -l$(FELIXLIBALIAS)


脚本文件如下:
#!/bin/bash
#a function to return a argument in argsi
function getargs()
{
  j=2
  tmpStr=$1
  while test "$j" -le "$2"
   do
       index="`expr index "$tmpStr" ' '`"
       j=$(($j+1))
       len="`expr length "$tmpStr"`"
       tmpStr=`expr substr "$tmpStr" "$(($index+1))" $len`
   done
    index=`expr index "$tmpStr" ' '`
  if test "$index" = "0"
   then
      argsi=$tmpStr
   else
     argsi="`expr substr "$tmpStr" 1 $(($index-1))`"
 fi
 return 1
}
mainArgs=$*
#echo a:$mainArgs
input="n"
i=1
quitShowInput="you can alter the menu in menu_c.h"
#check arguments
while test "$i" -le $#
do
   getargs "$mainArgs" "$i"
 case $argsi in
       -de)#file's description
        i=$(($i+1))
         getargs "$mainArgs" "$i"
         description=$argsi;;
       -a)#author
        i=$(($i+1))
         getargs "$mainArgs" "$i"
         author=$argsi;;
       -f)#filename
        i=$(($i+1))
         getargs "$mainArgs" "$i"
        filename=$argsi;;
       -h)#see helps
     echo -e " usage:/nmakeCtmp -d [directory to input] -f [fileanme] -a [author] -de [file 's description]" 
    exit;;
       -d)#object diretory
        i=$(($i+1))
         getargs "$mainArgs" "$i"
        directory="$argsi"
        if test -d "$directory"
         then
          echo "error:invalid directory"
          exit
         fi;;
       -e)#edit it immediately
         immedia=true
         ;;
       -c)#add c file
         addc=true
        ;;
       *)
       echo "errors:contain errors with arguments,makeCtmp -h to get helps"
        exit
   esac
        i=$(($i+1))
done
if test -z $directory
 then
  directory="."
 fi
cd $directory
if test -z $filename
  then
  echo "error:miss file name.print makeCtmp -h to get helps"
  exit
fi
 re=y
if test -f "$filename"
then
 echo "error: file exists.replace it(y/n)?";
 read re
fi
if test "$re" = "y"
then

#create makefile
makebody="include ~/.FelixInit/nobjects=${filename}.o/n/$(objects): ${filename}.h/n/t/$(cc) -c /$(CFLAGS) /$*.c /nclean:/n/trm *.o/ninstall:/$(objects)/n/tar rcs  /$(FELIXDEST)//$(FELIXLIBNAME) /$^/n/tcp ${filename}.h /$(FELIXHEADER) "
 echo -e $makebody>Makefile

comment="/**********************************/n*author:${author}/n*last update:`date`/n*description:/n*/n*/n*/n*/"
body="#ifndef ___`echo ${filename}|tr a-z A-Z`___/n#define ___`echo ${filename}|tr a-z A-Z`___/n/n/n/n/n#endif"

echo -e "$comment/n$body">${filename}.h
if [ "$addc" = "true" ]
then
 cbody="${comment}/n#include /"${filename}.h/"/n"
 echo -e $cbody>${filename}.c
fi
if [ "$immedia" = "true" ]
then
 vi ${filename}.h ${filename}.c
 fi
fi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值