加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

数组 – 获取数组的第一个索引

发布时间:2020-12-15 17:09:14 所属栏目:安全 来源:网络整理
导读:我有一个未从0索引的数组: arr=([2]=aaa bbb ccc ddd) 我需要得到数组的第一个索引.有很多事情我尝试过并且有效: for index in "${!arr[@]}"; do first_index=$index breakdone first_index=$(awk '{ print $1 }' "${!arr[@]}") first_index=$(cut -d' ' -
我有一个未从0索引的数组:
arr=([2]=aaa bbb ccc ddd)

我需要得到数组的第一个索引.有很多事情我尝试过并且有效:

for index in "${!arr[@]}"; do
    first_index=$index
    break
done
first_index=$(awk '{ print $1 }' <<< "${!arr[@]}")
first_index=$(cut -d' ' -f1 <<< "${!arr[@]}")
first_index=${!arr[@]}
first_index=${first_index%% *}
ind=("${!arr[@]}")
first_index=${ind[@]:0:1}

我真正想要的是:

${!arr[@]:0:1}

鉴于这个语法对于数组${arr [@]:0:1}非常有效并且非常干净:

>在没有外部工具,临时数组,临时变量,循环等的情况下,是否有类似的,更清晰的方法为索引执行此操作?我的尝试似乎使这么简单的任务过于复杂
> ${!arr [@]:0:1}实际发生了什么?

My attempts seem to overcomplicate such an easy task

这个任务可能看起来很简单,但可能不是一个常见的用例,因此没有语法上的容易,特别是如果你可以使用其他内置功能来实现它.

你有很多选择,还有一个:

put_first_index_in () {
    printf -v "${1?}" "%s" "${2?}"
}

put_first_index_in first_index "${!arr[@]}"

What is actually happening in ${!arr[@]:0:1}?

Indirect variable expansion:

If the first character of parameter is an exclamation point (!),
and parameter is not a nameref,it introduces a level of variable
indirection. Bash uses the value of the variable formed from the rest
of parameter as the name of the variable; this variable is then
expanded and that value is used in the rest of the substitution,
rather than the value of parameter itself. This is known as
indirect expansion.

随着!作为第一个字符,支持三种可能的东西:${!parameter}(上面给出的间接扩展),${!prefix *},${!prefix @}(扩展为与前缀匹配的变量名)和${ !name [*]},${!name [@]}(展开到名称数组的索引).

文档建议只有${!parameter}支持进一步替换,因为它只是为此提及而不是为其他提及.所以bash尝试执行以下操作:

>展开arr [@]
>对从(1)获得的字符串应用间接扩展
>从间接扩展获得的字符串中获取0:1子字符串

由于标识符中不是有效字符,因此我们会收到该错误:

$foo=SHELL
$echo ${!foo}
/bin/zsh
$echo ${!foo:0:1}
/
$foo="SHELL "
$echo ${!foo}
bash: SHELL : bad substitution
$arr=([2]=SHELL bbb ccc ddd)
$echo ${!arr[@]:0:1}
bash: SHELL bbb ccc ddd: bad substitution

因此,这只适用于单个元素的数组,例如:

$arr=([2]=SHELL)

正如所料:

$echo ${!arr[@]:0:1}
/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读