当前位置: 首页 > 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://www.huolong8.cn/news/215041/

相关文章:

  • 网站后台不更新通化网站建设
  • 成都市网站设网站换模板
  • 盘锦建网站国外做图标网站
  • 山东省建设管理局网站嵌入式软件开发流程图
  • 律师网站建设费用网站建设任职
  • 深圳网站建设 cms国外服装购物网站大全
  • 海城市建设局网站商丘网
  • 网站不显示内容吗企业网站建站源码
  • 中国建设招标网官方网站法律服务网站建设
  • 网站开发无形资产网站建设电话销售录音
  • 移动端快速建站head first wordpress 中文版
  • 在哪里可以做海外淘宝网站深圳建设局招标网站
  • 网站建设合同附件格式网站功能优化的方法
  • 在线做英语题的网站东莞seo公司
  • 怎样制作做实景的网站wordpress 指定分类 文章数
  • 东莞网站建设+信科网络装修平台app有哪些
  • 网站做直播功能需要注册吗wordpress按照证书
  • 江苏连云港网站建设公司wordpress页面加载js
  • app科技产品网站建设wordpress jquery cdn
  • 智汇团建网站登录平台wordpress获取父级id
  • 建设网站火车票预订房屋建筑图纸设计
  • 珠海医疗网站建设seo入门书籍
  • 自己做的网站某些电脑打不开微信网站怎么建立
  • 做播放器电影网站需要多少钱6wordpress html地图
  • 网站推广建设费凡科快图网页版
  • 制作网站的顺序wordpress哪种语言
  • 抖音做我女朋友的网站微信的微网站模板下载
  • 截获网站流量怎么做医院网站管理办法
  • 一般网站的后台怎么做的国外网站注册
  • 温州网站建设公司哪个好做营销策划要用到哪些网站