经典实用的自动创建Bash脚本文件头的脚本 推荐
2016-08-11 17:46
561 查看
今天给大家展示一下,我自己写的一个自动创建Bash脚本文件头的脚本(名为create),希望能对初学脚本者带来一定的思维提示。毕竟对于一个经常写脚本的运维人员来说,每次写脚本的时候都需要重复的去写一遍文件头,也是一件很累赘的事情,既然我们学了脚本,为什么不让它来为我们减轻一下负担了。所以一个自动创建Bash脚本文件头的想法在我脑海里面产生了。
本脚本所需要实现的功能:
1,能够自动创建一个标准的Bash脚本文件头,带有详细注释信息
2,能够给新建完成的Bash脚本文件自动添加执行权限
创建成功后红色字体提示,查看所创建的新脚本的文件头已经比较完善,打开后补上一个当前脚本的具体描述后,就可以直接开始编辑脚本正文了。是不是也就为我们辛苦的运维工程师们节省了一些写脚本文件文件头的时间了。
脚本正文代码,方便想用的朋友:
#!/bin/bash
#Author:lovefirewall
#Version:1.0
#Create time:2016-08-08 08:08:08
#Description:
[ $# -ne 1 ] && echo -e "This script is used to create the file head for a new bash script;\nYou must bring a parameter as the prefix of the new script name." && exit 2
echo -e "#!/bin/bash\n\n#Author:wangjun\n#Version:1.0\n#Create time:`date '+%F %T'`\n#Description:" >$1.sh
chmod +x $1.sh
echo -e "\033[31m$1.sh creation success!\033[0m"
本脚本所需要实现的功能:
1,能够自动创建一个标准的Bash脚本文件头,带有详细注释信息
2,能够给新建完成的Bash脚本文件自动添加执行权限
[root@centos7 test]# echo $PATH .:/test:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin [root@centos7 test]# ll create -rwxr-xr-x. 1 root root 435 Aug 8 08:05 create [root@centos7 test]# cat create #!/bin/bash create脚本本身的文件头 #Author:lovefirewall #Version:1.0 #Create time:2016-08-08 08:08:08 #Description:This script is used to create the file head for a new bash script [ $# -ne 1 ] && echo -e "This script is used to create the file head for a new bash script;\nYou must bring a parameter as the prefix of the new script name." && exit 2 判断用户是否给了新文件名的前缀作为create脚本的参数 echo -e "#!/bin/bash\n\n#Author:lovefirewall\n#Version:1.0\n#Create time:`date '+%F %T'`\n#Description:" >$1.sh 自动创建脚本文件头的正文 chmod +x $1.sh 为新创建的脚本文件添加执行权限 echo -e "\033[31m$1.sh creation success!\033[0m" 红色字体提示新脚本头文件创建成功 [root@centos7 test]# create 没有带参数,发出强制用户带参数的提示 This script is used to create the file head for a new bash script; You must bring a parameter as the prefix of the new script name. [root@centos7 test]# echo $? 查看不带参数执行失败的退出码(验证与脚本代码是否一致) 2 [root@centos7 test]# create test test.sh creation success! 直接带上参数运行,新脚本创建成功 [root@centos7 test]# ll test.sh -rwxr-xr-x. 1 root root 89 Aug 8 08:30 test.sh 验证新创建的脚本有没有自动添加执行权限 [root@centos7 test]# cat test.sh #!/bin/bash 验证新创建的脚本的文件头格式是否标准 #Author:lovefirewall #Version:1.0 #Create time:2016-08-08 08:30:10 #Description: [root@centos7 test]#
创建成功后红色字体提示,查看所创建的新脚本的文件头已经比较完善,打开后补上一个当前脚本的具体描述后,就可以直接开始编辑脚本正文了。是不是也就为我们辛苦的运维工程师们节省了一些写脚本文件文件头的时间了。
脚本正文代码,方便想用的朋友:
#!/bin/bash
#Author:lovefirewall
#Version:1.0
#Create time:2016-08-08 08:08:08
#Description:
[ $# -ne 1 ] && echo -e "This script is used to create the file head for a new bash script;\nYou must bring a parameter as the prefix of the new script name." && exit 2
echo -e "#!/bin/bash\n\n#Author:wangjun\n#Version:1.0\n#Create time:`date '+%F %T'`\n#Description:" >$1.sh
chmod +x $1.sh
echo -e "\033[31m$1.sh creation success!\033[0m"
相关文章推荐
- Android shell命令行中过滤adb logcat输出的几种方法
- shell脚本学习
- Shell脚本编程基础
- Shell学习笔记1
- 无webshell的UDF提权
- shell-sed命令
- 三、Ubuntu 使用Xshell
- 调用API接口下载腾讯CDN访问日志
- Linux Shell(一)——Shell变量
- Linux Shell(一)——Shell变量
- xshell 跳板机配置
- shell小数进行比较
- 远程ssh登陆时报错:/bin/bash: Permission denied
- Linux--shell脚本之文本处理工具
- linux shell sed 实现字符串查找、插入实例
- shell编程的小练习
- Shell脚本学习(三)注释的用法
- 微软准备开源PowerShell
- Jmeter之Bean shell使用(一)
- bash脚本基础知识