I may have missed details you provided, but I didn't think you had mentioned what you were using to cause your script to run. Without all the details, I can only offer some general comments.
Since I don't know what you're familiar with and what you aren't, I'll just try to cover the various factors that may affect the different ways in which you have tried to achieve your goal:
1) For me, if I try to use setleds
as a "standard" User, I don't have access to /dev/console
. But I can run setleds
as User root
2) So for me it seems that if I wanted to use setleds
in some script, I would want to make sure that the script runs as User root
3) If you try to use a X-Windows oriented command, then you may need one or more things to be available to the command, such as a value for the DISPLAY
environment variable, or a command option that tells the command which X Server to use.
4) If you use a command that expects access to a terminal device, but use it in an environment without a default terminal device, you'll need to connect the command to a terminal device explicitly.
5) If you test a command sequence in some sort of "terminal emulator" such as XTerm
, etc., the output of some commands may be affected by terminal mode settings related to tab character or other "white space" handling. If you then run the command in an environment without those considerations, the exact sequence of characters output by the command might be different.
Some of these factors can have a cumulative effect.
Beyond that, when I tested it in an XTerm window, the grep pattern you were using on the output of "xset -q", did not
work for me. I had to change it to:
xset -q | egrep 'Caps Lock:[[:space:]]+off'
before it would find the phrase indicating that the Caps Lock was off.
Just in general, if I was trying to create a script like yours, I would start by manually testing individual pieces of it in an XTerm or some such, to make sure that each step worked. When I tried the grep pattern your were using, I did it without the option to "quiet" the output of grep. In that way I could determine whether or not the correct output of xset
was being grabbed by grep, before using that grep pattern in a script. Yes, it's possible to just rely on the return code from the various forms of grep, but I prefer the comfort factor of seeing exactly what is output.
For me, either xset
will turn LED's on or off if I run them manually from a terminal window ( as long as I become root
to use setleds
). If they don't work for you when used manually in that way, then they can't be expected to work in a script. Depending on how you are causing your script to run, if you use xset
you might need to do something such as use the -display
option to specify an X Server which is communicating with your keyboard, in order to effectively connect the xset
command to your keyboard. The solution to the problem could be as simple as that.