带回显的打印输出范围

带回显的打印输出范围

我找到了获取当前大写锁定状态的代码(因为这台笔记本电脑的键盘没有任何 LED 指示灯)这里

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

echo ${v:7:17}

我了解第一部分是如何运作的;它查询状态,然后查找字符串“Caps”并将其存储到变量中。我不明白的是这里的这一行:

echo ${v:7:17}

该行仅打印出“Caps Lock:off/on”,具体取决于 Caps Lock 状态。我想数字和冒号是用于指定范围的,因此不会打印无关的信息,但数字似乎与我所看到的任何方式打印出来的字符都不对应。将打印出的完整行类似于:

    00:大写锁定:关闭 01:数字锁定:打开 02:滚动锁定:关闭

我要问的是:这个范围到底指定了什么?它是说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.

相关内容