refactored code for table output
This commit is contained in:
parent
1073f0cf90
commit
74c350be8f
1 changed files with 16 additions and 13 deletions
|
@ -1,18 +1,21 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
declare -r URL="https://api.opensensemap.org/boxes/59d7de4c66f66a0010797868/sensors"
|
function get_data {
|
||||||
declare -r TMPFILE="/tmp/sense-data"
|
local -r URL="https://api.opensensemap.org/boxes/59d7de4c66f66a0010797868/sensors"
|
||||||
|
local JSON="$(curl --silent "$URL" | tr '{[,' "\n")"
|
||||||
|
|
||||||
curl --silent "$URL" | tr '{[,' "\n" > "$TMPFILE"
|
echo "$JSON" | grep -e "title" -e "unit" -e "value" -e "createdAt" | cut -d'"' -f4
|
||||||
|
}
|
||||||
|
|
||||||
RAW_VALUES="$(grep -e "title" -e "unit" -e "value" -e "createdAt" "$TMPFILE" | cut -d'"' -f4)"
|
function print_table {
|
||||||
VALUECOUNT=$(grep -c "title" "$TMPFILE")
|
while mapfile -n 4 -t line && ((${#line[@]})); do
|
||||||
|
# parse timestamp with date
|
||||||
|
line[3]="$(date --date="${line[3]}")"
|
||||||
|
# join lines with |
|
||||||
|
{ local IFS='|'; echo "${line[*]}"; }
|
||||||
|
done | column \
|
||||||
|
--table --separator='|' --table-columns="Measurement,Unit,Value,Date" \
|
||||||
|
--table-order="1,3,2,4" --table-right="Value"
|
||||||
|
}
|
||||||
|
|
||||||
IFS=$'\n' RAW_VALUES=(${RAW_VALUES})
|
get_data | print_table
|
||||||
|
|
||||||
declare -a VALUES
|
|
||||||
for i in $(seq 1 $VALUECOUNT); do
|
|
||||||
raw_index=$(( $i * 4 - 4 ))
|
|
||||||
VALUES[$i]="${RAW_VALUES[$raw_index]}:\t${RAW_VALUES[$raw_index + 2]} ${RAW_VALUES[$raw_index + 1]}\t(as of: ${RAW_VALUES[$raw_index + 3]})"
|
|
||||||
echo -e "${VALUES[$i]}"
|
|
||||||
done
|
|
||||||
|
|
Reference in a new issue