简体   繁体   English

使用Bash读取算术比较中的数组元素数

[英]Read Number of Array Elements in Arithmetic Comparison using Bash

I am trying to see if an AWS user has more than one access key on an account. 我正在尝试查看一个AWS用户在一个账户上是否具有多个访问密钥。 I get the number of access keys with this line: 我通过以下行获得访问密钥的数量:

readarray old_access_keys < <(aws iam list-access-keys --user-name "$aws_user_name" --profile="$aws_key" | jq -r '.AccessKeyMetadata[].AccessKeyId')

And if he has more than one access key, the script should return: 如果他有多个访问密钥,则脚本应返回:

if (( "${!old_access_keys[@]}" > 1 )); then
    printf "User already has maximum keys allowed for this account.\\n\\n"
    return
  else
    ...some commands...
 fi

But when I run this script I get and error when I do that comparison: 但是,当我运行此脚本时,进行比较时会出错:

./aws_key_utils.sh: line 480: ((: 0 1 > 1 : syntax error in expression (error token is "1 > 1 ") ./aws_key_utils.sh:第480行:((:0 1> 1:表达式中的语法错误(错误标记为“ 1> 1”))

How can I compare the number of elements in the array against 1 correctly? 如何正确比较数组中元素的数量与1?

"${!old_access_keys[@]}" is wrong syntax to get number of elements in array. "${!old_access_keys[@]}"语法错误,无法获取数组中的元素数。 "${!old_access_keys[@]}" will return all indices (or keys in associative array) of array. "${!old_access_keys[@]}"将返回数组的所有索引(或关联数组中的键)。

To get number of elements in array use: 要获取数组中的元素数量,请使用:

if (( "${#old_access_keys[@]}" > 1 )); then
   printf "User already has maximum keys allowed for this account.\\n\\n"
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM