Che*_*evy 171 arrays bash slice
查看bash(1)手册页中的"Array"部分,我没有找到切片数组的方法.
所以我想出了这个过于复杂的功能:
#!/bin/bash
# @brief: slice a bash array
# @arg1: output-name
# @arg2: input-name
# @args: seq args
# ----------------------------------------------
function slice() {
local output=$1
local input=$2
shift 2
local indexes=$(seq $*)
local -i i
local tmp=$(for i in $indexes
do echo "$(eval echo \"\${$input[$i]}\")"
done)
local IFS=$'\n'
eval $output="( \$tmp )"
}
Run Code Online (Sandbox Code Playgroud)
像这样使用:
$ A=( foo bar "a b c" 42 )
$ slice B A 1 2
$ echo "${B[0]}" # bar
$ echo "${B[1]}" # a b c
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?
Pau*_*ce. 269
请参阅Bash 页面中的Parameter Expansion部分man.A[@]返回数组的内容:1:2,从索引1开始获取长度为2的片段.
A=( foo bar "a b c" 42 )
B=("${A[@]:1:2}")
C=("${A[@]:1}") # slice to the end of the array
echo "${B[@]}" # bar a b c
echo "${B[1]}" # a b c
echo "${C[@]}" # bar a b c 42
echo "${C[@]: -2:2}" # a b c 42 # The space before the - is necesssary
Run Code Online (Sandbox Code Playgroud)
注意,保留"ab c"是一个数组元素(并且它包含额外空间)的事实.
Nic*_*kin 45
还有一个方便的快捷方式,可以从指定的索引开始获取数组的所有元素.例如,"$ {A [@]:1}"将是数组的"尾部",即没有第一个元素的数组.
version=4.7.1
A=( ${version//\./ } )
echo "${A[@]}" # 4 7 1
B=( "${A[@]:1}" )
echo "${B[@]}" # 7 1
Run Code Online (Sandbox Code Playgroud)