自動生成C、C++、shell程序基本框架腳本

      該腳本根據使用者給出的擴展名生成不同程序的框架,這大大節省了我們在編程過程中的重複輸入基本框架的時間。

 

#!/bin/bash

declare -i cc_flag=0,c_flag=0,sh_flag=0

function main {
if [ $# -le 0 ];then
  echo -e "\033[31mUsage: $0 <cpp_file_name | c_file_name | sh_file_name>\033[0m"
  exit 1
fi

if [ -e $1 ];then
  return 0
fi

# identify which template should be made accroding to the file suffix
if echo $1 | egrep ".*\.cc|cpp\>" &> /dev/null; then
  cc_flag=1
elif echo $1 | egrep ".*\.c\>" &> /dev/null; then
  c_flag=1
elif echo $1 | grep ".*\.sh\>" &> /dev/null; then
  sh_flag=1
else
  echo -e "\033[31mBad file! Not a C++_file or C_file or SH_file\033[0m";
fi

# accroding the flag to make template
if [ $cc_flag -eq 1 ];then
cat >> $1 <<EOF
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    return 0;
}
EOF
elif [ $c_flag -eq 1 ];then
cat >> $1 << EOF
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    return 0;
}
EOF
elif [ $sh_flag -eq 1 ] ;then
cat >> $1 << EOF
#!/bin/bash
# date : `date`
EOF
fi
}

main $*
vim $1


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章