內陣列的所有元素,我想我的數組轉儲到用戶,使他們能夠看到的映射會是什麼樣子。我嘗試了這樣的陳述printf '%s\n' "${cluster_to_endpoint[@]}"
我試圖在下面的使用函數中轉儲它,但是我沒有得到我期待的輸出。慶典 - 如何轉儲使用
代碼進行:
#!/bin/bash
set -e
usage() {
echo "Usage: $0 -o oldcluster -n newcluster"
printf '%s\n' "${cluster_to_endpoint[@]}"
}
while getopts ":o:n:" opt; do
case $opt in
o) old="$OPTARG";;
n) new="$OPTARG";;
*) usage
exit 1
;;
esac
done
# Mapping
declare -A cluster_to_endpoint=(
[c1]=foobar2-01.us-east-1.my.com
[c2]=foobar2-02.us-east-1.my.com
[c3]=foobar2-03.us-east-1.my.com
[c4]=foobar2-04.us-east-1.my.com)
# Echo info
echo "Source cluster:${cluster_to_endpoint[$old]}"
echo "Target cluster:${cluster_to_endpoint[$new]}"
輸出:
-bash-4.1$ ./tst.sh -h
Usage: ./tst.sh -o oldcluster -n newcluster
期待:
Usage: ./tst.sh -o oldcluster -n newcluster
[c1]=foobar2-01.us-east-1.my.com
[c2]=foobar2-02.us-east-1.my.com
[c3]=foobar2-03.us-east-1.my.com
[c4]=foobar2-04.us-east-1.my.com
你必須得到您期望的輸出,如果你創建數組* *前嘗試打印一個更好的機會。 – rici