bash实例
2024-08-29 21:18:45
1写一个脚本,完成如下功能(使用函数):
1、脚本使用格式:
mkscript.sh [-D|--description "script description"] [-A|--author "script author"] 文件名
2、如果文件事先不存在,则创建;且前几行内容如下所示:
#!/bin/bash
# Description: script description
# Author: script author
#
3、如果文件事先存在,但不空,且第一行不是“#!/bin/bash”,则提示语法错误并退出;如果第一行是“#!/bin/bash”,则使用vim打开脚本;把光标直接定位至最后一行
4、打开脚本后关闭时判断脚本是否有语法错误
如果有,提示输入y继续编辑,输入n放弃并退出;
如果没有,则给此文件以执行权限;
mkscript.sh文件代码如下:
#!/bin/bash
#
echo $ > a
#echo "1: $1"
#echo "2: $2"
#echo "Count for args is: $#" #文件不存在
if [ ! -f "$2" ]; then
touch "$2"
echo "#!/bin/bash" >> $
description=`sed 's/\[\|\]\|"//g' aa |awk '{print $1}' | awk -F-- '{print $2}'`
#echo $description
#description
#echo ${#description}
#把description中的首字母大写
#echo ${description::}
first_up_1=`echo ${description::} | tr '[a-z]' '[A-Z]'`
echo "#$first_up_1${description:1:${#description}}:":`sed 's/\[\|\]\|"//g' aa |awk '{print $2,$3}'` >> $ author=`sed 's/\[\|\]\|"//g' aa |awk '{print $4}' | awk -F-- '{print $2}'`
#echo $author
#计算author的长度
#echo ${#author}
#把author中的首字母大写
#echo ${author::}
first_up_2=`echo ${author::} | tr '[a-z]' '[A-Z]'`
echo "#$first_up_2${author:1:${#author}}:"`sed 's/\[\|\]\|"//g' aa |awk '{print $5,$6}'` >> $
echo "#" >> $
#文件存在
else
#文件为空
if [ ! -s $ ]; then
echo "file is empty"
#文件不空
else
#第一行不是“#!/bin/bash”
firstLine=`sed -n '1p' $`
#字符串比较符两边一定要有空格
if [ "$firstLine" != "#!/bin/bash" ]; then
echo "Error command" && exit
#第一行是“#!/bin/bash”
else
#使用vim打开脚本;把光标直接定位至最后一行
vim + $
#关闭文件 #判断脚本有无语法错误
bash -n $
#有语法错误
if [ $? -ne ]; then
echo "$2 has grammar mistakes"
read -p "Pls input y(continue this edit)/n(quit without saved): " choice
while true;do
if [[ $choice == y ]] ;then
#继续编辑
vim + $
elif [[ $choice == n ]] ;then
exit
else
echo -n "Error,"
fi
read -p "Input again y(continue this edit)/n(quit without saved): " choice
done
else
#赋予文件执行权限
chmod +x $
fi
fi
fi
fi
运行命令是:
mkscript.sh '[-D|--description "script description"] [-A|--author "script author"]' test
最新文章
- (转)IC验证概述
- JS MD5
- scala中的trait
- HttpModule和Http Handler (比较与区别)
- jdk线程的同步问题
- Android实例-MotionSensor加速度(XE8+小米2)
- 查看堵塞的SQL
- 关于开发板不能ping通外网IP
- Ubuntu常用软件推荐,图文详细说明及下载
- 技术七Gitservergitolite要构建和操作方便
- 小符号反映大问题,Shell中下划线_与变量的关系。
- 如何通过CSS实现背景图片自动平铺或拉伸至整个屏幕(自适应大小)
- vue2.0中使用less
- python 全栈开发,Day81(博客系统个人主页,文章详情页)
- ThinkPHP 3.1.2 查询方式 -4
- WebShell代码分析溯源(第1题)
- dsu on tree题表
- json_encode让URL内容斜杠/不转义
- 2015.7.12js-11(DOM基础)
- elk----es settings--logstash--performance---bigdesk---logstash plugin online/offline
热门文章
- 牛客网Java刷题知识点之什么是匿名内部类、匿名内部类的使用原则、匿名内部类初始化、匿名内部类使用的形参为何要为final 和 案例
- Linux开机启动服务
- 算法导论课后习题解答 第一部分 练习1.1-1->;1.1-5
- Java获取服务器系统默认编码格式
- jetty启动(server-connector-handle-start-join)
- 邮箱/邮件地址的正则表达式及分析(JavaScript,email,regex)
- mitmweb的使用
- C++运算符重载讲解与经典实例
- Maven 中maven-assembly-plugin插件的使用笔记 SpringBoot环境
- Python 之excle的读写