LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Keymapping Variable for Bash Script? (https://www.linuxquestions.org/questions/programming-9/keymapping-variable-for-bash-script-818109/)

cryingthug 07-05-2010 12:21 PM

Keymapping Variable for Bash Script?
 
Is there a variable which will tell you which keyboard mapping you are using?

I want to write a bash script that will change my keymapping from Dvorak keyboard to US keyboard. I cannot seem to find a variable to read from.
I was going to use the "if [ ] then; statement".

Anybody have any ideas?

zirias 07-05-2010 04:39 PM

No, there's no variable for that. Keyboard mappings are a lot of data (mapping symbols to scancodes, basically, so they are large tables).

There are different mechanisms on the console and in X. In X, the command "xmodmap -pk" will print the keyboard mapping currently in use, but that is most definitely NOT what you want.

So, I think you should explain what you want to do (what's the problem you are trying to solve by writing this script)? One thing I do not understand is why you need to know the current keymap in order to set a new one?

cryingthug 07-11-2010 07:18 PM

zirias, I would like to make a script that changes the keymapping.

just want to create a test like this:

if [ $KBD = dvorak ]
then

cryingthug 07-11-2010 07:19 PM

zirias, I would like to make a script that changes the keymapping.

just want to create a test like this:
Code:

if [ $KBD = dvorak ]
then
      setxkbmap us
fi

I am beginning to think that this will not work...

raskin 07-12-2010 01:45 AM

If you are in X11, you can try parsing output of "setxkbmap -print"

cryingthug 07-12-2010 09:35 PM

getting there...
 
good advice...

This is what I have so far.

Code:

#!/bin/bash
 
  US="pc+us"
  if [ setxkbmap -print | grep xkb_s | awk '{print $4}' = $US ]
  then
          printf "%s\n" "The keymap is set to us"
  fi

And all I see is this:
Code:

keyboard.sh: line 6: [: missing `]'
awk: cmd. line:1: fatal: cannot open file `=' for reading (No such file or directory)


cryingthug 07-12-2010 09:39 PM

revision...
 
I changed it to this:

Code:

#!/bin/bash
US="pc+us"
if [ "setxkbmap -print | grep xkb_s | awk '{print $4}'" = "$US" ]
then
        printf "%s\n" "The keymap is set to us"
fi

And then I got this:

Code:

(nothing)
At least there is no error message.

cryingthug 07-12-2010 09:42 PM

next step
 
then I ran this command:
Code:

bash -x keyboard.sh
And I got this:
Code:

+ US=pc+us
+ '[' 'setxkbmap -print | grep xkb_s | awk '\''{print }'\''' = pc+us ']'

I guess the plus signs mean that those lines have problems right?

raskin 07-12-2010 11:40 PM

Well, you have simply a shell scripting problem.

[ = ] compares two its arguments in the shell sense. Now let us see what gets passed in your examples.

Code:

[ setxkbmap -print | grep xkb_s | awk '{print $4}' = $US ]
the first argument is "setxkbmap", the second is "-print" (and not "=") etc. It gets interpreted absolutely not like you want.

Code:

[ "setxkbmap -print | grep xkb_s | awk '{print $4}'" = "$US" ]
Now that is better. But look: you compare string "setxkbmap -print | grep xkb_s | awk '{print $4}'" with "$4" expanded (probably to empty string) with the value of "US" environment variable (most likely empty). Equality is very unlikely.

What is a way that can work? You want to get the value of command execution. There is "$()" construct for that.

Code:

echo "$(setxkbmap -print | grep xkb_s | awk '{print $4}')"
This command will show you the value that this command prints now. This is the value you can put in double-quotes in the right side.

Code:

[ "setxkbmap -print | grep xkb_s | awk '{print $4}'" = "pc+us" ]
Or something like that is what you need.

ntubski 07-13-2010 08:18 AM

Quote:

Originally Posted by raskin (Post 4031486)
with the value of "US" environment variable (most likely empty).

Uh, he set US in the previous line.

Also the grep is kind of redundant if you're piping to awk anyway.
Code:

[ "$(setxkbmap -print | awk '/xkb_s/{print $4}')" = $US ]

raskin 07-13-2010 08:36 AM

Oops, yes, my fault - didn't read the first post in the chain attentively enough.

By the way, cryingthug, "+" in the beginning of the line just means that Bash debugger prints the executed command in this line.

cryingthug 07-15-2010 09:32 PM

Thanks!
 
Raskin, Ntubski you guys are amazing! I thanked the both of you. This is the forum to come to. I'm learning more and more everyday.

cryingthug 07-28-2010 07:49 PM

[solved]
 
Hi!, this is what I ended up doing.

Code:

#!/bin/bash
KBD=$(setxkbmap -print | grep xkb_s | awk '{print $4}')
case $KBD in
        "\"pc+us\"") setxkbmap dvorak ;;
        "\"pc+us(dvorak)\"") setxkbmap us ;;
esac

It works perfectly.


All times are GMT -5. The time now is 01:49 AM.