LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2010, 12:21 PM   #1
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Rep: Reputation: 18
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?
 
Old 07-05-2010, 04:39 PM   #2
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
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?
 
Old 07-11-2010, 07:18 PM   #3
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Original Poster
Rep: Reputation: 18
zirias, I would like to make a script that changes the keymapping.

just want to create a test like this:

if [ $KBD = dvorak ]
then
 
Old 07-11-2010, 07:19 PM   #4
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Original Poster
Rep: Reputation: 18
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...

Last edited by cryingthug; 07-11-2010 at 07:23 PM. Reason: correction
 
Old 07-12-2010, 01:45 AM   #5
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
If you are in X11, you can try parsing output of "setxkbmap -print"
 
Old 07-12-2010, 09:35 PM   #6
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Original Poster
Rep: Reputation: 18
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)
 
Old 07-12-2010, 09:39 PM   #7
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Original Poster
Rep: Reputation: 18
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.
 
Old 07-12-2010, 09:42 PM   #8
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Original Poster
Rep: Reputation: 18
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?
 
Old 07-12-2010, 11:40 PM   #9
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
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.
 
Old 07-13-2010, 08:18 AM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by raskin View Post
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 ]
 
Old 07-13-2010, 08:36 AM   #11
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
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.
 
Old 07-15-2010, 09:32 PM   #12
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Original Poster
Rep: Reputation: 18
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.
 
Old 07-28-2010, 07:49 PM   #13
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Original Poster
Rep: Reputation: 18
[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.

Last edited by cryingthug; 07-28-2010 at 07:53 PM. Reason: post is solved.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
bash script: use the directory of the script file as variable? phling Linux - Newbie 12 01-16-2010 07:16 PM
Bash script - what does this variable mean? ${1##*/} purecharger Linux - General 7 11-11-2009 04:11 AM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 02:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration