6

Integer interpreted as a variable

 2 years ago
source link: https://www.codesd.com/item/integer-interpreted-as-a-variable.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Integer interpreted as a variable

advertisements

I am working on an audio visual art installation, using a Raspberry Pi with Raspbian Jessie. The audio or video is started after login via a bash script. This script runs fine on its own.

My issue has been with trying to schedule the loop to run only during certain times of the day using an if statement. This has been placed at the beginning on the loop to check the time before a new file would start.

HOUR=$(date +"%H")
echo "The hour is $HOUR"
if [ $HOUR -gt 22 || $HOUR-lt 8 ]
then
        sleep 60
        continue
fi

I have declared HOUR as an integer with declare -i HOUR at the beginning of the script.

The script outputs this error, then continues.

./start_audio.sh: line 49: 13: command not found

I believe the variable is set correctly because $HOUR [13] is shown in the error. So why is $hour being interpreted as a command when it has been declared as an integer?

Tried changes like adding quotes or removing spaces, as answers to other questions have suggested.


Enclose your variables in double quotes (not required here, but as a general best practice) and and separate out the checks into two with a || in between (-o is mostly deprecated):

if [ "$HOUR" -gt 22 ] || [ "$HOUR" -lt 8 ]

Or, you can use [[ ]]:

if [[ "$HOUR" -gt 22 || "$HOUR" -lt 8 ]]




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK