Bash question about bash variable and testing for said variable

#!/bin/bash
var = $(< /sys/class/leds/input3::numlock/brightness)
if $var = 0 
then
	notify-send 'Keyboard alert' 'Numlock Off.' --icon=dialog-information
else
	notify-send 'Keyboard alert' 'Numlock On.' --icon=dialog-information
fi
exit 0

Can anyone tell me what is wrong with that as I have searching and searching as well as changing code over and over to no avail.
Now it complains that
var is not a command
well duh, it’s a variable that I want to test against.
The reason for this script is because my computer does not, sadly, have an LED for Numlock.
So any suggestions would be appreciated

Well… ok that’s not stackoverflow here, but that should be more correct:

#!/usr/bin/env bash

var=$(cat /sys/class/leds/input3::numlock/brightness)
if   [[ $var -eq 0 ]]; then
	notify-send 'Keyboard alert' 'Numlock Off.' --icon=dialog-information
elif [[ $var -eq 1 ]]; then
	notify-send 'Keyboard alert' 'Numlock On.' --icon=dialog-information
fi

exit 0

You see there were a number of errors… variables for example must not have spaces between and conditions are like this: [[ expr != expr ]] in bash. Maybe have look at Bash scripting cheatsheet for a quick view. :wink:

What also should be mentioned is shellcheck to check your script for bad syntax.

3 Likes

Thanks for the help and I installed shellcheck and that is helping, at least somewhat.

PS: I joined stackoverflow. I need all the help I can get.

May I also suggest these two resources:

Solved. Kinda diff from original

#!/bin/bash
sleep 0.5
if cat /sys/class/leds/input3::numlock/brightness |grep "1"
then 
	notify-send 'Keyboard alert' 'Numlock On.' --icon=dialog-information
else
	notify-send 'Keyboard alert' 'Numlock Off.' --icon=dialog-information
fi
exit 0

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.