要求一行代码和要求不可读的代码一样好。如果您想以适当的方式执行此操作,请阅读
jq
在while循环中执行命令,并根据需要删除不需要的字符。
#!/usr/bin/env bash
# declare an associative array, the -A defines the array of this type
declare -A _my_Array
# The output of jq is separated by '|' so that we have a valid delimiter
# to read our keys and values. The read command processes one line at a
# time and puts the values in the variables 'key' and 'value'
while IFS='|' read -r key value; do
# Strip out the text until the last occurrence of '/'
strippedKey="${key##*/}"
# Putting the key/value pair in the array
_my_Array["$strippedKey"]="$value"
done< <(jq -r '.Parameters[] | "\(.Name)|\(.Value)"' json)
# Print the array using the '-p' or do one by one
declare -p _my_Array
或者用传统的方式打印数组
for key in "${!_my_Array[@]}"; do
printf '%s %s\n' "${key}" "${_my_Array[$key]}"
done