LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash Multiple If statement (http://www.linuxquestions.org/questions/programming-9/bash-multiple-if-statement-930397/)

michijo 02-20-2012 05:55 PM

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

sycamorex 02-20-2012 06:01 PM

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)

huaihaizi3 02-20-2012 07:23 PM

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}


uhelp 02-20-2012 08:16 PM

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


ntubski 02-20-2012 10:50 PM

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


michijo 02-20-2012 11:38 PM

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


grail 02-21-2012 01:01 AM

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.

uhelp 02-21-2012 07:22 AM

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


All times are GMT -5. The time now is 12:55 PM.