3

我正在挖掘 mysql_safe (试图添加一些选项),我遇到了他们用来从启动代理分配变量的这种 bash 脚本技术:com.mysql.mysqld.plist(我在 mac 上)。

现在 mysqld_safe 不知道 LaunchCtl 正在调用它,所以我假设 LaunchCtl 将任何选项转换为命令行参数,但我发现这个 sytnax 很有趣。有人知道这是如何工作的吗?

我了解 Bash 中 Case/Switch 的基础知识:

case "$myval" in
  switch1) do something;;
  switch2) do something;;
        *) do whatever fallthrough logic;;
esac      

默认情况下使用 * 在下面的脚本块中,arg 值为:“--basedir=”或“--datadir=”或“--pid-file=”等但是 * 中的 * 是怎么回事那里?
那是switch语句中的正则表达式吗? 有反向引用?

for arg do
  # the parameter after "=", or the whole $arg if no match
  val=`echo "$arg" | sed -e 's;^--[^=]*=;;'`
  # what's before "=", or the whole $arg if no match
  optname=`echo "$arg" | sed -e 's/^\(--[^=]*\)=.*$/\1/'`
  # replace "_" by "-" ; mysqld_safe must accept "_" like mysqld does.
  optname_subst=`echo "$optname" | sed 's/_/-/g'`
  arg=`echo $arg | sed "s/^$optname/$optname_subst/"`
  arg=`echo $arg | sed "s/^$optname/$optname_subst/"`
  case "$arg" in
    # these get passed explicitly to mysqld
    --basedir=*) MY_BASEDIR_VERSION="$val" ;;
    --datadir=*) DATADIR="$val" ;;
    --pid-file=*) pid_file="$val" ;;
    --plugin-dir=*) PLUGIN_DIR="$val" ;;
    --user=*) user="$val"; SET_USER=1 ;;
    ...
    ...
    *)
      if test -n "$pick_args"
      then
        append_arg_to_args "$arg"
      fi
      ;;
  esac
4

2 回答 2

5

它们不是正则表达式;它们是文件名扩展模式,也称为“globs”。

*匹配零个或多个任意字符,并?匹配任何单个字符。

更多信息:http ://www.gnu.org/s/bash/manual/bash.html#Pattern-Matching

于 2011-10-13T19:08:45.197 回答
3

如果您有最新版本的 bash,您可以使用真正的正则表达式来解析 arg,并为捕获的组访问 bash 数组 BASH_REMATCH:

for arg; do
    if [[ $arg =~ ^--([^=]+)=(.*) ]]; then
        optname=${BASH_REMATCH[1]}
        val=${BASH_REMATCH[2]}
        optname_subst=${optname//_/-}
        case "$optname" in 
            basedir) MY_BASEDIR_VERSION="$val" ;;
            datadir) DATADIR="$val" ;;
            ...
        esac
    else
        do something with non-option argument
    fi
done
于 2011-10-13T19:09:31.350 回答