This question already has an answer here:
- Associative Arrays in Shell Scripts 2 answers
I am working on a UPS Monitoring project using NUT Server. My objective is to make a shell script which sends one command and in response receives the status and other parameters from the UPS.
for example
#!/bin/bash
status='upsc myups' # command to get the status of UPS
sleep 1
exit 0
This is working fine for me but if I declare the 'status' as an array, the response from the ups is stored as a single element
i.e.
#!/bin/bash
declare -a status #declare status as array
# command
status=$(upsc myups) or status=`upsc myups`
#number of array elements
echo ${status[@]}
exit 0
Number of Elements in status array:
1
Terminal Output/Array Output
echo ${#status[1]}
if I echo the array, the output looks like as follows:
Init SSL without certificate database
battery.capacity: 9.00 battery.charge: 90 battery.charge.low: 20
battery.charge.restart: 0 battery.energysave: no battery.protection: yes
ups.shutdown: enabled ups.start.auto: yes ups.start.battery: yes
ups.start.reboot: yes ups.status: OL CHRG ups.test.interval: 604800
ups.test.result: Done and passed ups.timer.shutdown: -1
ups.timer.start: -1
ups.type: offline / line interactive ups.vendorid: 0463
As this whole output is saved in a single element of "status" array. I am getting trouble using all the parameter separately for log purposes.
Desired Output:
battery.capacity: 9.00
battery.charge: 90
battery.charge.low: 20
battery.charge.restart: 0
battery.energysave: no
battery.protection: yes
How can I separate each parameter into an individual element of an array or variable?
Please help
Thank you