-1

I am not sure what is wrong with my shell script but I am guessing that it has something to do with environment variables. My script runs gridcoinresearchd on my raspberry pi. I want it to turn off the pi lights and then turn them back on if my gridcoin balance increases. I am not sure if I am setting the variable correctly. I am not sure if the script and the shell and the parent shell are all set up to do what I am trying to do. Any help would be appreciated. Here is my script so far:


#This script turns off sleep mode, activates gridcoinresearchd and turns off the rasperry pi red and green lights. It then records the gridcoin balance and saves the balance as "PREVIOUSBALANCE". The script then checks every 15 minutes to see if the real Gridcoin balance "CURRENTBALANCE" has increased. If the balance has increased, the script turns the lights back on. This script must be run as sudo.

#Disable sleep mode
systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

#Activate gridcoinresearchd
gridcoinresearchd &
sleep 360

# Disable the red and green motherboard LEDs.
sh -c 'echo 0 > /sys/class/leds/led0/brightness'
sh -c 'echo 0 > /sys/class/leds/led1/brightness'


# Get and set "PREVIOUSBALANCE"
export PREVIOUSBALANCE=`gridcoinresearchd getinfo | grep "balance" /dev/stdin | tr '\n' ' ' | sed -e 's/[^0-9.]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' '`

# Get "CURRENTBALANCE"
export CURRENTBALANCE=`gridcoinresearchd getinfo | grep "balance" /dev/stdin | tr '\n' ' ' | sed -e 's/[^0-9.]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' '`

# If CURRENTBALANCE is greater than PREVIOUSBALANCE then turn on the lights.
if [ "CURRENTBALANCE" > "PREVIOUSBALANCE" ]; then
sh -c 'echo 14 > /sys/class/leds/led0/brightness'
sh -c 'echo 14 > /sys/class/leds/led1/brightness'
fi
clipovich
  • 45
  • 8
  • 1
    Using `>` inside the `[ ]` creates a file that is named what ever the name of the variable that is pointing to it, because it is treated as redirection instead of comparison some folks escapes that `>` but most folks use the new `[[ ]]` , or if using bash use the `(( ))` which is an arithmetic operator/evalutator, put a shebang on your script and try https://shellcheck.net to validate your script. – Jetchisel Jul 12 '20 at 08:47
  • Use `if [ "${CURRENTBALANCE}" > "${PREVIOUSBALANCE}" ]; then` – JGK Jul 12 '20 at 09:06
  • Btw.: `sh` ([Bourne-shell](https://en.wikipedia.org/wiki/Bourne_shell)) is usally not `bash` ([Bourne-again shell](https://en.wikipedia.org/wiki/Bash_(Unix_shell))). – Cyrus Jul 12 '20 at 09:11
  • 1
    Always run your problem scripts through [shellcheck](https://www.shellcheck.net/). – Shawn Jul 12 '20 at 09:29
  • 1
    There is no need to `export` a variable which is only used by the current script. The purpose of `export` is to expose a variable to swbprocesses. (So for example `PYTHONPATH` needs `export` in order for a Python subprocess to have access to it.) – tripleee Jul 12 '20 at 13:18

1 Answers1

1

Your error is in the if statement, in 'sh' you can't compare the arithmetique values using '< or >' symbols. So, you can use 'eq, gt, ge, lt, le'. You also forgot the '$' sign in your variables. here is the fix:

....
    
if [ "$CURRENTBALANCE" -gt "$PREVIOUSBALANCE" ]; then
        sh -c 'echo 14 > /sys/class/leds/led0/brightness'
        sh -c 'echo 14 > /sys/class/leds/led1/brightness'
fi
....
mchelabi
  • 154
  • 5
  • 1
    Thers's no need to wrap the commands in `sh -c` either. It would vaguely make sense if you needed to wrap them in sudo` but that's not the chse here. Simply `echo 14 >device` as part of the current script. – tripleee Jul 12 '20 at 13:15
  • Yes of course, it doesn't make sense. – mchelabi Jul 12 '20 at 14:29