-1

I have a bash script to set the keyboard type of my external and internal laptop keyboards (so as they are both usable and output the correct characters).

The main part of the script works fine, but I have a problem with the switch / case for capturing the command line options.

this is the full script

#!/bin/bash


###functions first

usage()
{
echo "script to set the internal and external keyboards to french and english repectively"

}


setKeyBoardMap()
{
    echo "setting dual keyboard."
    echo "setting keyboard types..."
    setxkbmap -device 13 fr
    setxkbmap -device 10 gb

    echo "done"
}


##the main show

#capture command line parameters


if ["$1" = ""]; then
    #no command line parameters
    setKeyBoardMap
    exit 0
else

while [ "$1" != " "]; do
    case $1 in
        -h | --help)    usage
                exit 1
                ;;
    -*|--*)
    echo "Unknown option $1"
    echo "please use -h or --help for usage details"
    exit 1
    ;;

    #add any switches in here
    #you may decide to modify this to set the keyboard
    #according to your prefered type.


    esac

done

fi

When I call this from my home directory I get the following

:~$ bash -x 2Keyb.sh -h
+ '[-h' = ']'
2Keyb.sh: line 31: [-h: command not found
+ '[' -h '!=' ' ]'
2Keyb.sh: line 37: [: missing `]'

I'm not sure what I'm missing but I'm sure it is in my while statement.

I know that having this construct for command line arguments is a bit excessive, but I plan to make this usable on any laptop to set the keybaord language between any 2.

But first I need to get it working as it.

If I don't pass any arguments the script functions perfectly...

Thanks in advance, your help is appreciated.

David

DaveM
  • 734
  • 4
  • 11
  • 35
  • 1
    `["$1" = ""]` is syntactically invalid. You have similar problems throughout. Please use shellcheck.net to debug prior to posting on SO. – Todd A. Jacobs Apr 19 '16 at 18:08
  • 1
    which strangely is the same as : [ "$1" != "" ]; do which is from the page on http://linuxcommand.org/wss0130.php#detecting. A clue along the lines of 'its a problem with spacing' I would have accepted as an answer. But thanks for shellcheck.net I didn't know of that site. – DaveM Apr 19 '16 at 18:35
  • You don't get to invent your own syntax. You are ignoring required spaces. Good luck! – Todd A. Jacobs Apr 19 '16 at 19:48
  • @CodeGnome et al I wasn't even aware that bash had a whitespace issue. It's never stated in any of the tutorials that i have seen, at least not clearly things like 'when troubleshooting bash scripts remember white space is important' would be really usefull ;) – DaveM Apr 22 '16 at 09:21

1 Answers1

0

while [ "$1" != " "]; do ^^ space missing here Also as mentioned by Codegnome there is shellcheck, a static analyzer for shell scripts. I personally use the shellcheck binary in combination with vim and the syntastic plugin -> very useful to find this kind of errors.

jandob
  • 651
  • 5
  • 14
  • to be honest I had hoped that CodeGnome would put his comment as a solution. I would give you the correct answer confirmation, but I still prefere CodeGnome's answer as it has a link to shellcheck.net, and it helped me help myself... If you add the same, you can grab his upvote ;) – DaveM Apr 20 '16 at 19:37
  • added in a link to shellcheck.net. Now I can accept your answer. – DaveM Apr 20 '16 at 19:43
  • Thanks, but i rejected the edit since i already edited it second before your addition. – jandob Apr 20 '16 at 19:46