GNU bash, version 4.3.27
I'm running into an odd issue with a script that writes output to a file. The script ssh's to a list of servers and records the state/substate of a few services. I then store the results in an array.
If I type into the console window while its running however, the input gets stored into the array $result_arr
and I don't know why
#!/bin/bash
check_services()
{
server_number="$1"
local result
local result_arr
local error_flag="OK"
#Get service status
result=$(ssh server.$host -t "$cmds_services")
#trim carriage returns if present
result=$(echo "$result" | tr -d '\r')
#split result into array by line
readarray -t result_arr <<<"$result"
echo "$server_number,${result_arr[0]},${result_arr[1]},${result_arr[2]},${result_arr[3]},${result_arr[4]},${result_arr[5]},${result_arr[6]},${result_arr[7]}" >> "$OUTFILE"
}
main()
{
for server in $(cat /home/data/serverlist)
do
clear -x
echo "Gathering Data...Server $server"
check_services "$server"
done
}
When I spam 'a' and enter while the script runs, $OUTFILE
will look something like this:
1234,active,mounted,active,mounted,a,,a,a
instead of
1234,active,mounted,active,mounted,active,mounted,active,mounted
readarray -t result_arr < <( ssh ... | tr -d '\r')
to do without the intermediate mangling by echo and the command substitution (the latter would remove trailing empty lines). Those CRs you get are also possibly due tossh -t
, try e.g.ssh -t localhost echo foo |od -c
and the same without -t. – ilkkachu Oct 16 '23 at 20:32