0

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

5
  • The variable assignment in bash does not allow the $ on the left hand side, afaik the only way it will work is using infamous eval , or I'm just imagining things :-) Commented Apr 14, 2020 at 12:10
  • @Jetchisel : I don't see any $VAR=value error. If you think of the $STATE=0x40001 stuff, these are supposed to be wildcard patterns matched by case, not assignments. Still, the case doesn't look right, but for different reasons. Commented Apr 14, 2020 at 12:16
  • @user1934428, why not provide an answer to this question? Commented Apr 14, 2020 at 12:20
  • Because there is already one, by choroba. Commented Apr 14, 2020 at 12:27
  • 1
    @RSH try shellcheck.net Commented Apr 14, 2020 at 12:33

1 Answer 1

2

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) ...
Sign up to request clarification or add additional context in comments.

2 Comments

Ah gotcha! Now the $LOGTIMESTAMP is showing correctly, but the DisplayState is still empty: 2020-04-10T00:04:35.130Z - Display State:
getDisplayState(){ STATE=$(/opt/vc/bin/tvservice -s | awk '/state/ {print $2}') case $STATE in 0x40001) echo VC_SDTV_NTSC, VC_HDMI_UNPLUGGED ;; ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.