I need to test to see if bash variables have something in them or if they are empty.
I am writing a script that rotates aws keys.
I came up with this test:
keys=$(aws iam list-access-keys --user-name "$user_name" --profile="$aws_key" | grep -i accesskeyid | awk '{print $2}' | sed 's/"//g')
key1=${keys%$'\n'*}
key2=${keys#*$'\n'}
echo "$key1"
echo "$key2"
echo
if [[ "$key1" != "^$" ]]; then
echo "Key1: $key1"
elif [[ "$key2" != "^$" ]]; then
echo "Key 2: $key2"
else
echo "There is no key data."
fi
And when I run the script it first prints the two variables to assure they have values. But when I run the test to see if they have values, only the first variable prints out:
AKIAIDQFZ62EMKNSZ7PQ
AKIAIPSNLAJKBLH2CLOQ
Key1: AKIAIDQFZ53EMKNSZ7PQ
How can I effectively test the contents of variables key1 and key2 to be sure they have contents?
Reason for this is that AWS users may have one or two keys, and can vary by user.