当前位置: 首页 > news >正文

工控机做网站服务器兰州小的网络公司

工控机做网站服务器,兰州小的网络公司,维恩图在线制作网站,网站建设维护培训写了一个shell脚本#xff0c;需要向shell脚本中传参数供脚本使用#xff0c;达到的效果是传的参数可以是可选参数 下面是一个常规化的shell脚本#xff1a; echo 执行的文件名为: $0;echo 第一个参数名为: $1;echo 第二个参数名为: $2…写了一个shell脚本需要向shell脚本中传参数供脚本使用达到的效果是传的参数可以是可选参数 下面是一个常规化的shell脚本         echo 执行的文件名为: $0;echo 第一个参数名为: $1;echo 第二个参数名为: $2 正常的向shell脚本中传参数的方法为: ./test.sh 1 2 3 最后执行的结果为           执行的文件名为: ./test.sh第一个参数名为: 1第二个参数名为: 2 但是这个是只能按照顺序传递参数并且不能传递可选参数然后查资料发现了一个shell的getopts 用法  首先贴个例子 [helloGit shell]$ bash test.sh -a hello this is -a the arg is ! hello [helloGit shell]$ more test.sh #!/bin/bashwhile getopts a: opt; docase $opt ina)echo this is -a the arg is ! $OPTARG ;;\?)echo Invalid option: -$OPTARG ;;esac done getopts一共有两个参数第一个是-a这样的选项第二个参数是 hello这样的参数。 选项之间可以通过冒号:进行分隔也可以直接相连接表示选项后面必须带有参数如果没有可以不加实际值进行传递 例如getopts ahfvc: option表明选项a、h、f、v可以不加实际值进行传递而选项c必须取值。使用选项取值时必须使用变量OPTARG保存该值。 [helloGit shell]$ bash test.sh -a hello -b this is -a the arg is ! hello test.sh: option requires an argument -- b Invalid option: - [helloGit shell]$ bash test.sh -a hello -b hello -c this is -a the arg is ! hello this is -b the arg is ! hello this is -c the arg is ! [helloGit shell]$ more test.sh #!/bin/bashwhile getopts a:b:cdef opt; docase $opt ina)echo this is -a the arg is ! $OPTARG ;;b)echo this is -b the arg is ! $OPTARG ;;c)echo this is -c the arg is ! $OPTARG ;;\?)echo Invalid option: -$OPTARG ;;esac done [helloGit shell]$ 执行结果结合代码显而易见。同样你也会看到有些代码在a的前面也会有冒号比如下面的 情况一没有冒号 [helloGit shell]$ bash test.sh -a hello this is -a the arg is ! hello [helloGit shell]$ bash test.sh -a test.sh: option requires an argument -- a Invalid option: - [helloGit shell]$ more test.sh #!/bin/bashwhile getopts a: opt; docase $opt ina)echo this is -a the arg is ! $OPTARG ;;\?)echo Invalid option: -$OPTARG ;;esac done [helloGit shell]$ 情况二有冒号 [helloGit shell]$ bash test.sh -a hello this is -a the arg is ! hello [helloGit shell]$ bash test.sh -a [helloGit shell]$ more test.sh #!/bin/bashwhile getopts :a: opt; docase $opt ina)echo this is -a the arg is ! $OPTARG ;;\?)echo Invalid option: -$OPTARG ;;esac done 情况一输入 -a 但是后面没有参数的的时候会报错误但是如果像情况二那样就不会报错误了会被忽略。 while getopts :a:bc opt  #第一个冒号表示忽略错误字符后面的冒号表示该选项必须有自己的参数  参考 https://blog.csdn.net/xluren/article/details/17489667 但是这样还是无法满足可以输入可选参数的要求这样输入的参数名称也是固定的参数的数量也是固定的然后接下来了解了shell 的getopt用法。 看过官方文档后自己写了个小demo #!/bin/bash#获取对应的参数 没有的话赋默认值 ARGSgetopt -o a::b::l::n::t::p:: --long along::,blong::,llong::,plong:: -n example.sh -- $ if [ $? ! 0 ]; thenecho Terminating...exit 1 fi#echo $ARGS #将规范化后的命令行参数分配至位置参数$1,$2,...) eval set -- ${ARGS}while true; docase $1 in-a|--along)case $2 in)project1_namemaster;shift 2;;;*)project1_name$2;shift 2;;;esac;;-b|--blong)case $2 in)project2_namemaster;shift 2;;*)project2_name$2;shift 2;;;esac;;-l|--llong)case $2 in)layerlayer_1;shift 2;;*)layer$2;shift 2;;;esac;;-n)case $2 in)number1000;shift 2;;*)number$2;shift 2;;;esac;;-t)case $2 in)select_typetop;shift 2;;*)select_type$2;shift 2;;;esac;;-p)case $2 in)data_point;shift 2;;*)data_point$2;shift 2;;;esac;;--)shiftbreak;;*)echo Internal error!exit 1;;esacdoneproject1_name${project1_name:-master} project2_name${project2_name:-master} layer${layer:-layer_1} number${number:-100} select_type${select_type:-top} data_point${data_point:-} 这一部分为输入参数对应的字符 a::b::l::n::t::p::   #-o表示短选项两个冒号表示该选项有一个可选参数可选参数必须紧贴选项如-carg 而不能是-c arg #--long表示长选项 一目了然首先是根据传递过来的 是 -a  还是 -b  -l 进行判断如果只有-a后面没有参数的话 则赋默认值同时在 project1_name${project1_name:-master} project2_name${project2_name:-master} layer${layer:-layer_1} number${number:-100} select_type${select_type:-top} data_point${data_point:-} 这一步 也进行了默认值的赋值操作。如果上面没有 project1_name  则赋值为master 例子如下: 参数的默认值设置$cat myscript.shprint ${1:-hello}print ${2:-kshell}$sh myscript.shhellokshell  转载于:https://www.cnblogs.com/linwenbin/p/10800756.html
http://wiki.neutronadmin.com/news/215041/

相关文章:

  • 网站建设推广服务合同范本官方商城
  • 汨罗哪里有网站开发的公司电话做网站插背景图片如何变大
  • 建设网站免费模板下载阿里云手机版网站建设
  • 品牌网站建设怎么做seo教学平台
  • 网站开发维护求职信moodle ual wordpress
  • 网站设计常用字体中文域名网站怎么发布信息
  • 可以做盗版漫画网站吗wordpress分类目录浏览权限
  • 县级门户网站建设运营成本专业的手机网站开发
  • 做企业网站一般用什么服务器怎么让别人访问我建的网站
  • 承德的网站建设公司武功做网站
  • 淘宝客 网站 备案wordpress模板免费下载
  • 天津建设银行官方网站wordpress cos存储
  • 网络设计网站建设类网站模板做网站沈阳本地
  • 广告公司做网站的效果怎么样南京网站制作步骤
  • 事业单位网站建设费入什么科目漯河网站推广多少钱
  • app软件程序开发搜索引擎优化技术有哪些
  • 问题反馈的网站怎么做网站如何做搜索
  • 企业查询学历周口网站优化
  • 做外贸网站怎么样暴雪是不是快倒闭了
  • 网站建设及安全管理文档国外网站空间购买
  • 网站建设中需求分析说明书南充市住房和城乡建设局网站
  • 郑州网站建设及托管互联网创业项目网
  • 网站数据库建设怎么建设食品网站
  • 美发网站源码公文写作 课程中心网站建设
  • 丹阳网站设计公司手机如何建网站
  • 专业写作网站wordpress 2m附件
  • 高端品牌网站建设需要注意什么怎么样推广自己的店铺和产品
  • 西安电商平台网站建设杭州专业网站
  • 龙泉市建设局网站重庆网站到首页排名
  • 团队氛围建设 网站网站更名策划方案