Bash

使用 echo 列印輸出範圍

  • February 13, 2017

我從這裡找到了獲取目前 Caps Lock 狀態的程式碼(因為這台筆記型電腦的鍵盤沒有任何 LED 指示燈)。

#!/bin/bash
v=`xset -q | grep Caps`

echo ${v:7:17}

我了解第一部分的工作原理;它正在查詢狀態,然後查找字元串“Caps”並將其儲存到變數中。我不明白的是這裡的這一行:

echo ${v:7:17}

該行僅列印出“Caps Lock:關閉/打開”,具體取決於 Caps Lock 狀態。我想數字和冒號用於指定一個範圍,因此不會列印無關的資訊,但這些數字似乎與以我能看到的任何方式列印出來的字元都不對應。將列印出的整行類似於:

00:Caps Lock:關閉 01:Num Lock:開啟 02:Scroll Lock:關閉

我要問的是:指定的範圍到底是什麼?它是說v:start:end,本質上?我嘗試查找有關使用範圍的資訊echo,但沒有找到任何東西。我係統的手冊頁甚至沒有提到echo.

請參閱子字元串擴展。格式為${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

這不是關於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.

引用自:https://unix.stackexchange.com/questions/344666