手机版
你好,游客 登录 注册
背景:
阅读新闻

自动生成C、C++、shell程序基本框架脚本

[日期:2016-04-27] 来源:Linux社区  作者:A嘴角上扬 [字体:大 中 小]

自动生成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

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-04/130740.htm

linux
相关资讯       C++ 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款