I'm running a command from a bash 4 prompt to gather the output from AWS CLI commands that pull the output of an AWS SSM run document. I can have it output in multiple formats including text or json (default). I am, unsuccessfully so far, attempting put this output into an array so I can loop through the output until every value in the array equals 2 or higher.
#!/bin/bash
aws ec2 reboot-instances --instance-ids `aws ec2 describe-instances --filters "Name=tag:RebootGroup,Values=01" --query 'Reservations[].Instances[].InstanceId' --output text`
sleep 30
completeLoop=false
while [ ! ${completeLoop} ]
do
ssmID=$(aws ssm send-command --document-name "AWS-RunPowerShellScript" --document-version "1" --targets '[{"Key":"tag:RebootGroup","Values":["01"]}]' --parameters '{"commands":["$wmi = Get-WmiObject -Class Win32_OperatingSystem ","$uptimeMinutes = ($wmi.ConvertToDateTime($wmi.LocalDateTime)-$wmi.ConvertToDateTime($wmi.LastBootUpTime) | select-object -expandproperty \"TotalMinutes\")","[int]$uptimeMinutes"],"workingDirectory":[""],"executionTimeout":["3600"]}' --timeout-seconds 600 --max-concurrency "50" --max-errors "0" --region us-west-2 --output text --query "Command.CommandId")
declare -a a
readarray -t upTimeArray <<< $(aws ssm list-command-invocations --command-id "$ssmID" --details --output json | jq '.CommandInvocations[].CommandPlugins[].Output')
if [[ " ${upTimeArray[@]} " -gt 5 ]]; then
echo "Uptime is greater than 5 minutes."
completeLoop=true
else
completeLoop=false
fi
done
I've made some progress here but now I am trying to figure out how to remove the carriage return/new line from the output.
Here is my array simplified to just output the value of the items in the array. I assume I need to use sed to strip the '\r\n' from each line but I am having trouble doing so.
declare -a a
readarray -t upTimeArray <<< $(aws ssm list-command-invocations --command-id "$ssmID" --details --output json | jq '.CommandInvocations[].CommandPlugins[].Output')
for i in "${upTimeArray[@]}"
do
echo $i
done
is returning the following
"1\r\n"
"1\r\n"
I need it to return just "1" for each line so I can iterate over the array until each equals 2 or greater.
EDIT #2
I made progress with help provided here but eventually fully solved my issues with the question and scripting in this second question https://stackoverflow.com/questions/65362975/bash-aws-cli-trying-to-figure-out-how-to-validate-an-array-of-uptimes-with-2-ch
$my_array
is equivalent to${my_array[0]}
aws ... | jq ...
command look like?aws...
command produces, so we can't recommend anything to process its output. (I'd suggest you add it to your question)\r\n
as visible characters in the response, or are you talking about their non-visible equivalents?