LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 02-20-2012, 06:55 PM   #1
michijo
Member
 
Registered: Apr 2011
Posts: 162

Rep: Reputation: 0
Bash Multiple If statement


I want a script that does multiple if statements. I have a rotating script that changes my keymap from US to Dvorak. But I want to add a 3rd statement or keymap for Esperanto. It only rotates two. This is what I have:

Code:
#!/bin/bash

keyboard_layout=`cat .keyboard_status.txt`
if [ "$keyboard_layout" == "Dvorak" ]
then
    setxkbmap us 
	echo "Us" > .keyboard_status.txt
elif [ "$keyboard_layout" == "Us" ]
then
	setxkbmap dvorak
	echo "Dvorak" > .keyboard_status.txt
elif [ "$keyboard_layout" == "Dvorak" ]
then
	setxkbmap epo
	echo "Esperanto" > .keyboard_status.txt
else
	setxkbmap us 
	echo "Us" > .keyboard_status.txt
fi
What might be wrong with this? It only alternates Dvorak and US and skips EPO, though I can manually change it to EPO in the terminal!

Thanks
 
Old 02-20-2012, 07:01 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,578
Blog Entries: 1

Rep: Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033
The script works correctly. It goes from top to bottom so it'll never reach the bit about Esperanto as whenever it finds that the current layout is Dvorak, it'll change it to Us (the first condition from the top meeting this criteria).

I can't think of an easy way to accomplish what you want (apart from perhaps introducing another variable PREVIOUS_LAYOUT)
 
Old 02-20-2012, 08:23 PM   #3
huaihaizi3
LQ Newbie
 
Registered: Oct 2009
Location: BeiJing China
Distribution: Fedora 15 x64
Posts: 17

Rep: Reputation: 0
Code:
#!/bin/bash

data_file=.keyboard_status.txt
keyboard_layout="$(cat ${data_file})"

case ${keyboard_layout} in
        Dvorak) key_name=Us     ;;
        Us)     key_name=Epo    ;;
        Epo)    key_name=Dvorak ;;
esac

setxkbmap ${key_name} 
echo "${key_name}" > ${data_file}
 
Old 02-20-2012, 09:16 PM   #4
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
Code:
# round ribbon

layout=( 'US' 'Dvorak' 'epo' )  # add as many you like
max=${#layout[*]}               # counts the entries in array layout; index is zero based
i=0

while [[ $max > $i ]];  do
    if [[ "$keyboard_layout" == ${layout[$i]} ]] ;
    then 
        (( j = $i + 1 ))
        if [[ "$j" <  "$max" ]] ; then
            echo "${layout[$j]}" > .keyboard_status.txt
            break
        else
            echo "${layout[0]}" > .keyboard_status.txt
            break
        fi  

    fi  
    (( i = $i + 1 ))
done
 
Old 02-20-2012, 11:50 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,515

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
Code:
        (( j = (i + 1) % max )) # Modular arithmetic
        if [[ "$j" <  "$max" ]] ; then
            echo "${layout[$j]}" > .keyboard_status.txt
            break
        else
            echo "${layout[0]}" > .keyboard_status.txt
            break
        fi
 
Old 02-21-2012, 12:38 AM   #6
michijo
Member
 
Registered: Apr 2011
Posts: 162

Original Poster
Rep: Reputation: 0
Easy Way

I figured out a much more easy way to do it. Its my fault for not reading the loop right. I changed it if it is Dvorak, then change it to US, if it is US, then change it to Esperanto. If it is Esperanto, change it to Dvorak, else change it to Esperanto again!

I have this mapped to my F2 key with xbindkeys. Now if I press F2 I can get the Dvorak, press again it is Qwerty, a 3rd time actually brings the Esperanto keyboard, and pressing it again goes back to Dvorak! A magic keymapping button! Thanks for your help anyway though!

Code:
#!/bin/bash

keyboard_layout=`cat .keyboard_status.txt`
if [ "$keyboard_layout" == "Dvorak" ]
then
    setxkbmap us 
	echo "Us" > .keyboard_status.txt
elif [ "$keyboard_layout" == "Us" ]
then
	setxkbmap epo
	echo "Esperanto" > .keyboard_status.txt
elif [ "$keyboard_layout" == "Esperanto" ]
then
	setxkbmap dvorak
	echo "Dvorak" > .keyboard_status.txt
else
	setxkbmap epo 
	echo "Espo" > .keyboard_status.txt
fi

Last edited by michijo; 02-21-2012 at 12:42 AM.
 
Old 02-21-2012, 02:01 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,627

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Personally I would use the case option presented by huaihaizi3 as it is much cleaner and should you need to add another format it is as simple as adding one line.
 
Old 02-21-2012, 08:22 AM   #8
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
scnr:
using "round ribbon" you have to add just a word
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] if statement with multiple tasks ted_chou12 Linux - Newbie 3 02-02-2011 07:59 PM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 07:00 AM
How to do multiple comaprison in a IF statement pdklinux79 Linux - Newbie 4 06-11-2008 11:34 PM
bash case statement with multiple command line options pwc101 Programming 16 06-01-2007 12:43 PM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 06:20 PM


All times are GMT -5. The time now is 02:34 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration