一个自动生成带菜单演示的c语言模板的SHELL脚本-建立自己的c数据结构与算法库系列(4)

脚本的功能:
1。创建文件夹FelixTest,文件夹中包含文件:主程序文件(main),menu_c.h,menu_c.c.main中生成了菜单显示代码,文件中根据参数生成注释
2.生成Makefile
假设此脚本文件名为:makeTCtmp
调用例子:makeTCtmp -f testList -a Felix -th list.h -n 3 -i
这样将在当前目录下生成一FelixTest文件夹,在文件夹中生成四个文件Makefile menu_c.h menu_c.c testList.c
参数:
-f 生成主文件的名称
-n 自定义菜单选项数
-i 马上手动输入菜单
-a 作者


脚本如下:

#!/bin/bash
#a function to return a argument in argsi
#distinguish the system
#The command to view the files
LESS=less
############3
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
}

CURDIR="`pwd`"
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
       -th)#ADT header to view
        i=$(($i+1))
         getargs "$mainArgs" "$i"
         viewfiles=$argsi;;
       -tc)#test for
         description="${description}/n*it test for $CURDIR";;
       -de)#file's description
        i=$(($i+1))
         getargs "$mainArgs" "$i"
         description=${description}/n${argsi};;
       -a)#author
        i=$(($i+1))
         getargs "$mainArgs" "$i"
         author=$argsi;;
       -f)#filename
        i=$(($i+1))
         getargs "$mainArgs" "$i"
        filename=$argsi;;
       -n)#menuCount
        i=$(($i+1))
         getargs "$mainArgs" "$i"
        menuCount=$argsi ;;
       -i)#input menu
              input="y"
              quitShowInput="you change the menu in menu_c.c later.";;
       -h)#see helps
     echo -e " usage:/nmakeCtmp -d [directory to input] -f [fileanme] -n [menu count] -h(help) -i(input menu) -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;;
       *)
       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
if test -z $menuCount
 then
echo "error:miss menu count.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
menu_h="/*n*author:$author/n*create date:`date`/n*last update:`date`/n*description:/n*/n*/n*//n#include <stdio.h>/n#ifndef __MENU____/n#define __MENU____/n#define SELECT() ((___sel=___select())!='0')/n#define SELECTION ___sel/nchar ___sel;/nchar ___select();/n/*/n define the menu:/n*//n#endif"
menu_c="/*n*author:$author/n*create date:`date`/n*last update:`date`/n*description:/n*/n*/n*//n#include /"menu_c.h/"/nchar *___menu[]={/n"
menu_c_tail="void ___showMenu()/n{/n printf(/"please select:///n/");/n char **p=___menu;/n while(*p)printf(/"%s///n/",*p++);/n printf(/":>/");/n}/nchar ___select()/n{/nchar c;/n ___showMenu();/n while((c=getchar())=='///n'); /n return c;/n}"
main="/*n*author:$author/n*create date:`date`/n*last update:`date`/n*description:$description/n*/n*/n*///n#include /"menu_c.h/"/n#include<stdio.h>/n#include<stdlib.h>/n/nint main()/n{/n/nwhile(SELECT())/n{/n  switch (SELECTION)/n  {"
i=1
if test "$input" = "y"
then
 echo "input menus:>"
fi
while  test "$i" -le "$menuCount"
do
  if test "$input" = "y"
  then
  echo -n ">$i."
  read mm
  menu_c="$menu_c     /n/"$i.$mm/","
  else
  menu_c="$menu_c     /n/"$i./","
  fi
 main="$main     /n      /*$mm*//n      case '$i':/n    break;"
  i=$(($i+1))
 
done
if [ -n "viewfiles" ];then
viewcontent="case '9':/n    system(/"$LESS ../${viewfiles}/");/n    break;"
viewfilemenu="9.Print the file ${viewfiles}"
fi
menu_c="$menu_c     /"${viewfilemenu}/",/n    /n/"0.EXIT/",/nNULL/n};/n$menu_c_tail"

main="$main      /n    $viewcontent/n     default:break;/n  }/n}/nreturn 0;/n}/n"
#create files
if [ -d FelixTest ] ;then
echo "directory FelixTest do exists,go on anyway?(y/n)"
read re;
if [ "`echo $re|tr a-z A-Z`" = "N" ] ;then
  exit;
fi
else
mkdir FelixTest
fi
cd FelixTest
#create makefile
makebody="include ~/.FelixInit/nobjects=menu_c.o ${filename}.o/n${filename}:/$(objects)/n/t/$(cc) /$^ /$(CFLAGS) -o /$@/nclean:/n/t-rm *.o/n/t-rm ${filename}"
echo -e $makebody>Makefile
echo -e $main>${filename}.c
echo -e $menu_h>"menu_c.h"
echo -e $menu_c>"menu_c.c"
echo -e "files menu_c.h/menu_c.c and template $filename have been created ./n$quitShowInput/nedit it with vi(y/n) now?"

read re
if test "$re" = "y"
 then
vi ${filename}.* menu_c.h menu_c.c Makefile
fi
fi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值