Reputation: 395
I have a situation where I need to echo:
a variable - plain text - the result of a function call
Im so confused about what the various symbols mean when echoing
Here are my functions:
#!/bin/bash
getLogTimestamp() {
echo $(date +%Y-%m-%dT%H:%M:%S.130Z)
}
getDisplayName(){
echo $(awk -F'=' '/^CARBON_LITE_PLAYER_ID=.*/ { print $2;}' /home/benja/app/.env)
}
getDeviceIPAddress(){
echo $(hostname -i)
}
getSystemUptime(){
echo $(uptime)
}
getDateTimeInfo(){
echo "$(timedatectl status | awk '/Warning/{exit} 1')"
}
getMemoryInfo() {
arr=( $(free -h) )
keys=("${arr[@]::8}")
vals=("${arr[@]:8:12}")
for i in ${!keys[@]}; { printf -v data[i] "%s: %s, " "${keys[i]}" "${vals[i]}"; }
data="${data[@]}"
echo ${data%,*}
}
getDiskInfo()
{
arr=($(df -h))
keys=("${arr[@]::6}")
vals=("${arr[@]:7:100}")
for i in ${!keys[@]}; { printf -v data[i] "%s: %s, " "${keys[i]}" "${vals[i]}"; }
data="${data[@]}"
echo ${data%,*}
}
getDisplayState(){
STATE=$(/opt/vc/bin/tvservice -s | awk '/state/ {print $2}')
case $STATE in
$STATE=0x40001 )
echo VC_SDTV_NTSC, VC_HDMI_UNPLUGGED
;;
$STATE=0x40002 )
echo VC_SDTV_NTSC, VC_HDMI_ATTACHED
;;
$STATE=0x120002 )
echo VC_SDTV_ATTACHED, VC_SDTV_CP_INACTIVE, VC_HDMI_ATTACHED
;;
$STATE=0x120005 )
echo VC_SDTV_ATTACHED, VC_SDTV_CP_INACTIVE, VC_HDMI_UNPLUGGED, VC_HDMI_DVI
;;
$STATE=0x120016 )
echo VC_SDTV_ATTACHED, VC_SDTV_CP_INACTIVE, VC_HDMI_ATTACHED, VC_HDMI_DVI, VC_HDMI_HDCP_UNAUTH
;;
$STATE=0x12001a )
echo VC_SDTV_ATTACHED, VC_SDTV_CP_INACTIVE, VC_HDMI_ATTACHED, VC_HDMI_HDMI, VC_HDMI_HDCP_UNAUTH
;;
$STATE=0x12001a )
echo VC_SDTV_ATTACHED, VC_SDTV_CP_INACTIVE, VC_HDMI_ATTACHED, VC_HDMI_HDMI
;;
$STATE=0x120009 )
echo VC_SDTV_ATTACHED, VC_SDTV_CP_INACTIVE, VC_HDMI_UNPLUGGED, VC_HDMI_HDMI
;;
esac
}
In a separate file I am calling these functions:
#Logging
. /home/benja/app/scripts/log-helper-functions.sh
LOGTIMESTAMP=getLogTimestamp
# RSH -We are logging this data every time this script runs (chron job)
echo $LOGTIMESTAMP "Display Name: $(getDisplayName)"
echo $LOGTIMESTAMP "Device IP: $(getDeviceIPAddress)"
echo $LOGTIMESTAMP "System UpTime: $(getSystemUptime)"
echo $LOGTIMESTAMP "DateTime Info: $(getDateTimeInfo)"
echo $LOGTIMESTAMP "Memory Info: $(getMemoryInfo)"
echo $LOGTIMESTAMP "Disk Info: $(getDiskInfo)"
echo $LOGTIMESTAMP "Display State: $(getDisplayState)"
# RSH end
This is an example of the output:
getLogTimestamp Display Name: UAT-101429
getLogTimestamp Device IP: 10.0.0.120
getLogTimestamp System UpTime: 23:26:06 up 19:54, 1 user, load average: 1.66, 0.82, 0.47
getLogTimestamp DateTime Info: Local time: Thu 2020-04-09 23:26:06 EDT
Universal time: Fri 2020-04-10 03:26:06 UTC
RTC time: n/a
Time zone: America/New_York (EDT, -0400)
Network time on: yes
NTP synchronized: no
RTC in local TZ: yes
getLogTimestamp Memory Info: total: 174M, used: 42M, free: 91M, shared: 388M, buff/cache: 317M, available: Swap:, Mem:: 0B, 605M: 0B
getLogTimestamp Disk Info: Filesystem: /dev/root, Size: 30G, Used: 4.6G, Avail: 23G, Use%: 17%, Mounted: /
getLogTimestamp Display State:
Problems: Obviously the $TIMESTAMP is not resolving, and the Display State is not resolving.
What am I doing wrong?
TIA -Ron
Upvotes: 0
Views: 36
Reputation: 242343
The right hand side of an assignment doesn't normally run. You need to tell bash to run it and use the output:
LOGTIMESTAMP=$(getLogTimestamp)
The case
statement has a different syntax. Each case consists of just the value, you already stated the $STATE
in the case $STATE
line, no need to repeat it on each line.
case "$STATE" of
0x40001) ...
Upvotes: 2