在shell脚本的功能中,我看到类似这样的内容
func()
{
local x
x=${1:3:1}
...
}
x=${1:3:1}
是什么意思?我知道$1
,$2
和$3
是函数的参数。那么上述陈述是否意味着x = $1:$2:$3
?
此外,如果有人可以建议我如何谷歌搜索这样的特殊字符,这真的很有帮助吗?任何标准关键字?我试着搜索"":"在shell脚本'等等。但是在尝试搜索特殊字符时结果是随机的。
答案 0 :(得分:5)
这在shell中称为参数扩展。
$ {PARAMETER:OFFSET:LENGTH}
这个只能扩展参数值的一部分,给定一个开始的位置,也许是一个长度。如果省略LENGTH,则参数将扩展到字符串的末尾。如果LENGTH为负数,则将其作为字符串的第二个偏移量,从字符串的末尾开始计算。
OFFSET和LENGTH可以是任何算术表达式。 OFFSET从0开始,而不是从1开始。
例如,假设参数是一个字符串,
MYSTRING ="在接受的内容中保持自由,在发送内容时要保守#34;
回声$ {MYSTRING:34:13}
以上将为您提供以下内容
保守
因为它将计算第33个(索引从0开始)字符,该字符将以字符" c"开头。然后计算(13个字符)长度。
因此,在您的情况下,$ 1是您传递给脚本的参数,然后它会偏移其中的3个字符并获取长度为1的字符串并将其初始化为x。
在此处阅读更多内容:http://wiki.bash-hackers.org/syntax/pe#substring_expansion
答案 1 :(得分:2)
这是一个参数扩展,是许多以${
开头的一部分。
赞$ {parameter:-word},$ {parameter:= word},$ {parameter:?word},$ {parameter:+ word}和其他几个。
这个(特定于ksh,bash和zsh):${parameter:offset:length}
从offset开始提取长度字符(可选,如果缺少,则参数中的其余字符串)。有关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.
答案 2 :(得分: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).
答案 3 :(得分: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可以摆弄数据的所有不同语法方式,因此将“参数替换”提交给内存会有所回报。字符串操作恰好是参数替换之前的章节。
答案 4 :(得分:0)
试试这个:
设置ABCDEFG
echo $ {1:3:1}
它正在获得一个子串。通常,$ {}引用数组变量(在本例中为chars数组)