经典实用的自动创建Bash脚本文件头的脚本
今天给大家展示一下,我自己写的一个自动创建Bash脚本文件头的脚本(名为create),希望能对初学脚本者带来一定的思维提示。毕竟对于一个经常写脚本的运维人员来说,每次写脚本的时候都需要重复的去写一遍文件头,也是一件很累赘的事情,既然我们学了脚本,为什么不让它来为我们减轻一下负担了。所以一个自动创建Bash脚本文件头的想法在我脑海里面产生了。
本脚本所需要实现的功能:
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"
