【set】关于 关于shell中 set -- 使用说明(new)

MySQL 官方的Dockerfile中有如下关于set的一段代码:

 

用于判断该脚本后面的参数是否以“-”开始,它考虑的是启动mysqld是带参数的情况,如果有的话,就将mysqld和参数作为变量存到$@中。

关于set --,Manual文档的说明如下:

If no arguments follow this option, then the positional parameters are unset. Otherwise, the positional parameters are set to the args, even if some of them begin with a -.

我们构造一个测试脚本测试一下:

# cat test.sh

####################

#!/bin/bash

if [ "${1:0:1}" = '-' ]; then

        set -- mysqld "$@"

fi

echo '$@: '"$@"

echo '$1: '"$1"

########################

 

关于$@与$*的区别,$@指每个位置参数参数都是一个独立的""引用字串,这就意味着参数被完整地传递,而$*则指所有位置参数只被一个""引用,相当于一个参数。

参考

set命令

https://man.linuxde.net/set

Bash 脚本 set 命令教程

http://www.ruanyifeng.com/blog/2017/11/bash-set.html

What's “set — ”$progname“ ”$@“” means in shell script?

https://stackoverflow.com/questions/20088290/whats-set-progname-means-in-shell-script

分析Mysql 5.6的Dockerfile

https://www.cnblogs.com/ivictor/p/4832832.html

 
 


作者:Bogon
链接:https://www.jianshu.com/p/7bac1ec4ddab
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

原文地址:https://www.cnblogs.com/wxwgk/p/15169928.html