2016-11-16 749 views
1

一个shell脚本的功能里面我看到这样的事情

func() 
{ 
local x 
x=${1:3:1} 
... 
} 

是什么x=${1:3:1}意思?我知道$1,$2$3是函数的参数。那么上面的陈述意味着x = $1:$2:$3

此外,如果有人可以建议我如何谷歌搜索这样的特殊字符真的很有帮助?任何标准关键字?我尝试搜索“什么是”:“在shell脚本中”等。但是,当试图搜索特殊字符时,结果是随机的。

回答

5

这在shell中称为参数扩展。

$ {PARAMETER:OFFSET:LENGTH}

这一次只能扩展参数值的部分,因为开始的位置,也许长度。如果忽略LENGTH,则参数将被扩展到字符串的末尾。如果LENGTH为负,则将其作为字符串的第二个偏移量,从字符串的末尾开始计数。

OFFSET和LENGTH可以是任何算术表达式。该OFFSET从0开始,而不是在1

比如让说的参数是一个字符串,

MyString的=“在你接受什么样的自由主义和保守你发送”

回声$ {MyString的:34:13}

上述会给你以下

CON servative

因为它会计算第33个(索引从0开始)字符,它将以字符“c”开始,然后计数(13字符)长度。

因此,在您的情况下,$ 1是您传递给脚本的参数,然后将其偏移3个字符并采用长度为1的字符串并将其初始化为x。

更多在这里阅读:http://wiki.bash-hackers.org/syntax/pe#substring_expansion

1

使用手册页,所有信息都在那里。 man bash

${parameter:offset:length} 
      Substring Expansion. Expands to up to length characters of the 
      value of parameter starting at the character specified by off‐ 
      set. 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 specified by offset 
      and extending to the end of the value. length and offset are 
      arithmetic expressions (see ARITHMETIC EVALUATION below). 
+0

谢谢。顺便说一下,我使用的是adb shell。是adb shell bash? –

+1

'if [adb = bash];然后回声“是的,它是”;否则回声“不,它是不同的”; fi' –

+0

它说不,它是不同的。但我试图打印$ adb。它是空的。这是正确的方法来检查? –

2

这是一个参数扩展,许多与${开始的一部分。

Like $ {parameter:-word},$ {parameter:= word},$ {parameter:?word},$ {parameter:+ word}等等。

这一个(专用于ksh,bash和zsh):${parameter:offset:length}从偏移处开始提取长度字符(可选,如果缺失,参数中字符串的其余部分)。随着bash手册中描述的几个细节。

${name: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 
    specified by offset and extending to the end of the value. length and 
    offset are arithmetic expressions (see ARITHMETIC 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 confused with the :- expansion. 

    If parameter is @, the result is length positional parameters 
    beginning 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 ${parameter[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 undefined 
    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. 
0

试试这个:

集ABCDEFG

回声$ {1:3:1}

这是得到一个子。通常$ {}是指数组变量(在这种情况下是字符数组)

1

x = $ {1:3:1}是什么意思?

它的子切断,英文:使用字符串中$1,拉1字符开始于索引3(其中索引是从0开始)。所以如果$1 === "foobar",那么${1:3:1} === "b"

我知道$ 1,$ 2和$ 3是函数的参数。那么上面的陈述意味着x = $ 1:$ 2:$ 3?

没有,邻接代表字符串连接:x="$1$2$3"$1$2,并且$3串接的字符串的结果。

此外,如果有人可以建议我如何谷歌搜索这样的特殊字符是真的有帮助吗?任何标准关键字?我尝试搜索“什么是”:“在shell脚本中”等。但是,当试图搜索特殊字符时,结果是随机的。

bash parameter substitution通常让你进入球场。我知道我不记得bash可以用数据处理的所有不同的语法方式,因此将“参数替换”提交给内存是有好处的。字符串操作恰好是参数替换之前的章节。

相关问题