1

I have the following script to execute "xset led" command on keypress but I cannot make it work for the command does not actually execute. Here is the script:

#!/bin/bash
res=$(echo "$(xset q)" | tr ";" "Scroll Lock")
if [[ $res == *"Scroll Lock: on"* ]]; then
    echo $(xset led on)
else
    echo $(xset led off)
fi

However when I execute echo $(xset led on) in the shell it works. Executing the script above outputs an empty line, though.

Burgi
  • 6,551
  • I would normally put $res in quotes for comparison and use a single equals sign, i.e. if [ "$res" = *"Scroll Lock: on"* ]; then ... fi, but it seems like your script should work regardless. Oh, but tr will only take the first character from Scroll Lock, not the whole thing! – wvxvw Nov 07 '17 at 09:26
  • I am new to the bash script but $res somehow contains what I need - the if-else statement works as expected but I don't know how to fire xset led on/off so that my keyboard backlight will turn of or on respectively. – Любомир Борисов Nov 07 '17 at 13:47
  • 1
    Well, if that's all you need, you don't need to call xset inside a subshell, you could replace echo $(xset led on) with just xset led on with virtually the same effect. Also, you might want to put set +x before anything in your script to make Bash log every line it executes: this way you will know when something breaks. – wvxvw Nov 07 '17 at 14:11

1 Answers1

0

The problem was actually in executing the wrong command in the if-else statement. When the backlight is on I executed xset led on instead of off. A rookie mistake was