[“ $ {1:0:1}” = '-']の意味


18

MySQLプロセスを起動する次のスクリプトがあります。

if [ "${1:0:1}" = '-' ]; then
    set -- mysqld_safe "$@"
fi

if [ "$1" = 'mysqld_safe' ]; then
    DATADIR="/var/lib/mysql"
...

このコンテキストで1:0:1はどういう意味ですか?


1
答えを知りたいのですが、これはSFにとってちょっと狭すぎる質問だと思います。私はそれをUnixサイトに移行することに投票しています。
マッシモ

回答:


19

-どうやら、これは破線の引数オプションのテストです。本当に奇妙です。bashから最初の最初の文字のみを抽出しようとして、非標準の展開を使用し$1ます。0先頭文字の指標であり、1文字列の長さです。では[ testそのように、それはまた次のようになります。

[ " -${1#?}" = " $1" ]

testただし、-破線の引数も解釈するため、どちらの比較も特に適していません。そのため、先頭のスペースを使用しています。

この種のことを行うための最良の方法-それが通常行われる方法-は次のとおりです。

case $1 in -*) mysqld_safe "$@"; esac

1
閉じる; の2番目のコロンに続く数字${1:0:1}は長さであり、インデックスではありません。
chepner

バシズムのやり方で[[[[ $1 == -* ]]
Arthur2e5

2
個人的には-testここで問題になるとは思いません。POSIXは、引数の数で意味の定義を提供します。2つの引数を取るこのようなオプションはないため、rawで記述するのは安全です。
Arthur2e5

@ Arthur2e5-あなたは正しいです-それらは問題ではないはずです-そして、ほとんどの場合、まったく問題はありません。それはまだ奇妙な方法です-うまく適合しません。何を[[ : [[するの?
mikeserv

1
@mikeservさて、ウェブページをご覧ください(他の場所から読んでいる場合)。私のコメントは「with START_CODE [[END_CODE:START_CODE [[$ 1 ==-*]] END_CODE」のようでした。最初[[は単なる構文名で、コロンは単なる句読点です。
Arthur2e5

11

これは$1、0 文字目から1文字目までの部分文字列を取ります。したがって、文字列の最初の文字と最初の文字のみを取得します。

bash3.2 manページ:

  ${parameter:offset}
  ${parameter:offset:length}
          Substring  Expansion.   Expands  to  up to length characters of
          parameter starting at the character specified  by  offset.   If
          length is omitted, expands to the substring of parameter start-
          ing at the character specified by offset.   length  and  offset
          are  arithmetic  expressions (see ARITHMETIC EVALUATION below).
          length must evaluate to a number greater than or equal to zero.
          If  offset  evaluates  to a number less than zero, the value is
          used as an offset from the end of the value of  parameter.   If
          parameter  is  @,  the  result  is length positional parameters
          beginning at offset.  If parameter is an array name indexed  by
          @ or *, the result is the length members of the array beginning
          with ${parameter[offset]}.  A negative offset is taken relative
          to  one  greater than the maximum index of the specified array.
          Note that a negative offset must be separated from the colon by
          at  least  one space to avoid being confused with the :- expan-
          sion.  Substring indexing is zero-based unless  the  positional
          parameters are used, in which case the indexing starts at 1.

6

最初の引数の最初の文字が$1ダッシュであることをテストしてい-ます。

1:0:1はパラメーター展開の値です${parameter:offset:length}

つまり:

  • 名前:という名前のパラメーター1、つまり:$1
  • 開始:最初の文字0から(0から番号付け)。
  • 長さ:1文字。

要するに:最初の位置パラメータの最初の文字$1
そのパラメーター展開は、(少なくとも)ksh、bash、zshで利用可能です。


テストラインを変更する場合:

[ "${1:0:1}" = "-" ]

バッシュオプション

他のより安全なbashソリューションは次のとおりです。

[[ $1 =~ ^- ]]
[[ $1 == -* ]]

これは引用に問題がないため安全です(内部で分割は実行されません[[

POSIXlyオプション。

古い、機能の低いシェルの場合、次のように変更できます。

[ "$(echo $1 | cut -c 1)" = "-" ]
[ "${1%%"${1#?}"}"        = "-" ]
case $1 in  -*) set -- mysqld_safe "$@";; esac

誤った引用に対してより耐性があるのは、caseコマンドのみです。

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.