工控机做网站服务器,兰州小的网络公司,维恩图在线制作网站,网站建设维护培训写了一个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