在工作和学习中,每一次都会对同样的文件进行重编辑。本脚本适用于使用 Linux 环境学习的人员,以及不含图形化界面的嵌入式设备中,对文件进行模板化创建。
程序非常简单,就两个文件,一个Shell脚本文件 : crtsh.sh,一个可配置文件:crtsh.mdlx (文件后缀名自己起的,没有什么含义)
先上代码,末尾会对代码中的函数以及使用方法进行简单的介绍。
脚本文件:crtsh.sh
#!/bin/bash
# Written by zhangxu <709821680@qq.com>
#配置文件路径
fileModel=crtsh.mdlx
#帮助
function echohelp(){
echo ""
echo " ========================================================"
echo " = ="
echo " = File creator V1.0 ="
echo " = auth:zhangxu ="
echo " = ="
echo " ========================================================"
echo " = ="
echo " = this tool is use for learning different language ="
echo " =please using 'crt XXX' the XXX is the created filename="
echo " = ="
echo " ====================== warning ========================="
echo " "
echo " the tools is update all time , please check the version!"
echo " "
echo " ========================================================"
echo " = ="
echo " = support type: ="
echo " = 1.Shell program : fileName ( XXX.sh ) ="
echo " = 2.C program : fileName ( XXX.c ) ="
echo " = 3.Cpp program : fileName ( XXX.cpp ) ="
echo " = 4.Python program : fileName ( XXX.py ) ="
echo " = ="
echo " = *.every program : fileName ( youself ) ="
echo " = you can change the file with change $fileModel ="
echo " = ="
echo " ========================================================"
echo " = ="
echo " = thank you for using ! ="
echo " = stay hungry ! ="
echo " = stay foolish ! ="
echo " = ="
echo " ========================================================"
echo ""
}
function noipt(){
echohelp
}
function cutman(){
echo $1 | cut -d . -f 2
}
function echoman(){
echo -e "you have create a file whitch named \033[36m $1 \033[0m , the file is used as $2 program"
}
function wi(){
echo $1 >> $2
}
#shell 模板
function createFileShell(){
touch $1
wi "#!/bin/bash" $1
wi "" $1
wi "" $1
chmod 755 $1
vi +3 $1
}
#C 模板
function createFileC(){
touch $1
wi "#include <stdio.h>" $1
wi "#include <stdlib.h>" $1
wi "" $1
wi "int main(int argc,char* argv[])" $1
wi "{" $1
wi "" $1
wi "}" $1
vi +6 $1
}
#C++ 模板
function createFileCpp(){
touch $1
wi "#include <iostream>" $1
wi "" $1
wi "using namespace std" $1
wi "" $1
wi "int main()" $1
wi "{" $1
wi "" $1
wi "}" $1
vi +7 $1
}
#Python 模板
function createFilePy(){
touch $1
wi "#!/usr/bin/python" $1
wi "# -*- coding: UTF-8 -*-" $1
wi "" $1
wi "" $1
vi +4 $1
}
#自定义模板
function createFileSelf(){
fileName=`echo $1 | cut -d . -f 1`
fileType=`cat $fileModel | head -1 | cut -d : -f 2`
fileStart=`cat $fileModel | head -n 2 | tail -1 | cut -d : -f 2`
fileAll=$fileName.$fileType
touch $fileAll
createFileByModel $fileAll $fileModel $fileStart
}
function createFileByModel(){
sed -n '3,$p' $2 >> $1
vi +$3 $1
}
function checkAll(){
fileName=`echo $1 | cut -d . -f 1`
fileType=`cat $fileModel | head -1 | cut -d : -f 2`
fileStart=`cat $fileModel | head -n 2 | tail -1 | cut -d : -f 2`
fileAll=$fileName.$fileType
if [ -f $fileAll ];then
echo "the file is already exist! please check the filename and the type you saved"
else
echo "now , the file is create as what your modle is"
createFileSelf $1
fi
}
#程序入口
if [ $# = 0 ];then
noipt
elif [ $# = 1 ];then
if [ `cutman $1` = "all" ];then
checkAll $1
elif [ -f $1 ];then
echo "the file is already exist! please check the filename"
else
if [ `cutman $1` = "sh" ];then
echoman $1 Shell
createFileShell $1
elif [ `cutman $1` = "c" ];then
echoman $1 C
createFileC $1
elif [ `cutman $1` = "cpp" ];then
echoman $1 C++
createFileCpp $1
elif [ `cutman $1` = "py" ];then
echoman $1 Python
createFilePy $1
fi
fi
else
echo "you should only input one filename!"
fi
可配置文件:crtsh.mdlx
type:c
stay:6
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char* argv[])
{
}
配置方法(很重要,不配置,只能手动调shell脚本了):
1.打开linux终端,进入放有crtsh.sh的目录下,将该脚本放置于 /bin 下(该步骤是为了让脚本全局可用,我的另一篇文章中有关于全局配置的小脚本,喜欢的朋友可以去看一下:实用小脚本 将写好的 sh 文件自动上传到 bin 目录)
move ./crtsh.sh /bin/crtsh
2.将配置文件置于任意位置,进入配置文件所在文件目录,记录路径
pwd
3.修改 /bin 路径下刚刚上传的 crtsh 中的配置文件路径,路径改为第二步中的路径位置 + 配置文件名称 (默认 crtsh.mdlx ,前提是你没改名)
vi /bin crtsh
到此为止,你就已经将前提操作做完了,接下来,就使用吧
使用方法:
crash XXX.type
crash 是脚本名称
XXX 改成你要创建的文件名称
type 改成你要创建的文件类型,即文件后缀名
例: crash test.c
注意事项:
1.生成固定模板文件
目前 V0.01 版本中 固定模板只做了 C语言 、C++ 、Python 和 Shell 脚本的文件创建,毕竟大家需要创建的模板各不相同,所以,这个还是希望大家看懂脚本的,自己使用 wi 函数拼接自己想要的固定模板
2.生成配置模板文件
顾名思义,文件内容全靠配。在 crtsh.mdlx 文件中,第一行,type 配置文件类型,第二行,stay 配置光标所在行,第三行到末尾,配置文件的内容(即文件模板,要生成什么内容)
这个必须将后缀名写成 .all
例:crash test.all
本脚本完全个人创造,如果有感兴趣或者有更多想法的朋友可以在留言中联系我,
一起学习,一起进步
希望这个小脚本可以帮助到你
stay hungry! stay foolish!