
私は現在Caps Lockステータスを取得するコードを見つけました(このノートブックのキーボードにはLEDインジケータがないため)。ここ。
#!/bin/bash
v=`xset -q | grep Caps`
echo ${v:7:17}
最初の部分がどのように機能するかを理解します。状態を照会し、「Caps」文字列を見つけて変数に保存します。私が理解していないのは次の行です。
echo ${v:7:17}
この行は、Caps Lockの状態に応じて「Caps Lock:off / on」を単に印刷します。数字とコロンは範囲を指定して不要な情報を印刷しないようですが、数字は私が見た方法で印刷された文字と一致しないようです。印刷される行全体は次のとおりです。
00: Caps Lock: オフ 01: Number Lock: オン 02: Scroll Lock: オフ
私が尋ねるのは、この範囲が正確に何を指定するのかです。本質的に意味ですかv:start:end
?使用範囲に関する情報を探しましたecho
が、何も見つかりませんでした。私のシステムのマンページにはこれに言及していませんecho
。
答え1
バラより部分文字列拡張。形式は${string:position:length}
次のとおりです。
$ x="123456789012345678901234567890"
$ echo ${x:0:0}
$ echo ${x:0:1}
1
$ echo ${x:0:2}
$ echo ${x:0:3}
123
$ echo ${x:1:3}
234
答え2
これは〜についてではありませんecho
。殻についてです。man bash
たとえば、このコマンドを使用して検索すると
man bash | grep -C5 {
この説明が表示されます。
${parameter:offset}
${parameter:offset:length}
Substring Expansion. Expands to up to length characters of the value of parameter starting at the character specified by
offset. If parameter is @, an indexed array subscripted by @ or *, or an associative array name, the results differ as
described below. If length is omitted, expands to the substring of the value of parameter starting at the character spec‐
ified by offset and extending to the end of the value. length and offset are arithmetic expressions (see ARITHMETIC EVAL‐
UATION below).
echo
部分文字列のみを印刷しますが、printf
同じことを行います。
完全な説明man bash
:
${parameter:offset}
${parameter:offset:length}
Substring Expansion. Expands to up to length characters of the value
of parameter starting at the character specified by offset. If
parameter is @, an indexed array subscripted by @ or *, or an asso‐
ciative array name, the results differ as described below. If length
is omitted, expands to the substring of the value of parameter start‐
ing at the character specified by offset and extending to the end of
the value. length and offset are arithmetic expressions (see ARITH‐
METIC EVALUATION below).
If offset evaluates to a number less than zero, the value is used as
an offset in characters from the end of the value of parameter. If
length evaluates to a number less than zero, it is interpreted as an
offset in characters from the end of the value of parameter rather
than a number of characters, and the expansion is the characters
between offset and that result. Note that a negative offset must be
separated from the colon by at least one space to avoid being con‐
fused with the :- expansion.
If parameter is @, the result is length positional parameters begin‐
ning at offset. A negative offset is taken relative to one greater
than the greatest positional parameter, so an offset of -1 evaluates
to the last positional parameter. It is an expansion error if length
evaluates to a number less than zero.
If parameter is an indexed array name subscripted by @ or *, the
result is the length members of the array beginning with ${parame‐
ter[offset]}. A negative offset is taken relative to one greater
than the maximum index of the specified array. It is an expansion
error if length evaluates to a number less than zero.
Substring expansion applied to an associative array produces unde‐
fined results.
Substring indexing is zero-based unless the positional parameters are
used, in which case the indexing starts at 1 by default. If offset
is 0, and the positional parameters are used, $0 is prefixed to the
list.