2

Long term lurker: I'm a total tinkerer, never learned to code & I'm only just at copy&paste script-kiddie level. I'd like to get better so I'd be grateful for ideas about how to improve my "kbdpicker" script.

Context:

  • distro = xubuntu 19.10
  • there's a few programs where I find the built-in keyboard-shortcuts hard to commit to muscle memory and in chromium (+/- a few others) I have to use xdotool to simulate mouse movements to create shortcuts (inspired by memories of autohotkeys in windows7) ==> it'd be much easier for me if I had the numpad digits 0-9 set to specified keyboard-shortcuts or xdotool-commands (and one day I'll create conky cheatsheet reminders for each program)
  • I can of course use the GUI settings > keyboard to create the keybindings BUT then they'll be active all the time and I won't be able to use the numpad digits to enter numbers
  • so I created multiple versions of the xfce-keyboard-shortcuts.xml file in my $HOME/config/xfce4/xfconf/xfce-perchannel-xml folder and a script bin/kbdpicker which copies/ overwrites from the program-specific profile to the active one :: it means in the terminal I can quickly swap between the default numpad or one of my custom program specific keybindings (once the script's looking better and proved itself I might actually re-assign the media keys - which I never use - to different kbdpicker selections).

It took a lot of googling but I'm a bit stunned to say that the kbdpicker script works but it's disastrously inelegent so I'd really like comments about how it could be done better.

  • It seems redundant to repeat the case structure just so that I can have either the "quick terminal command with option" method or the "full TUI" method.
  • Should I really have the select command at all, is there a better way to do a menu picker?
  • Do I really need the do/done loop that forces me to explicitly exit? (when I tried without the do/ done I kept getting errors)
  • I tried several methods to get the keyboard-shortcuts to be refreshed (a bit like doing . .bashrc after that's been changed). Is there a better way?

Thanks for reading and thanks in advance for any suggestions.

best wishes

Atilla

ls $HOME/config/xfce4/xfconf/xfce-perchannel-xml | grep shortcuts

xfce4-keyboard-shortcuts__0_nil__.xml
xfce4-keyboard-shortcuts__GEANY__.xml
xfce4-keyboard-shortcuts__GIMP__.xml
xfce4-keyboard-shortcuts__HEYDOC__.xml
xfce4-keyboard-shortcuts.xml

cat ~/bin/kbdpicker

    #!/bin/bash

    #  Script:    kbdpicker
    #  Created:   2020.05.04

    # https://linuxize.com/post/bash-select/            ===> select & case examples
    # https://forum.xfce.org/viewtopic.php?id=11607     ===> successfully kill & restart daemons
    # https://forum.xfce.org/viewtopic.php?id=8860      <=== failed ideas for daemons
    # https://forum.xfce.org/viewtopic.php?id=7878      <=== failed ideas for daemons

    FoldeR="$HOME/.config/xfce4/xfconf/xfce-perchannel-xml"
    FilE="$FoldeR/xfce4-keyboard-shortcuts.xml"

    function runRoutine { 
    PS3="Select the kbdsetup profile to be used: "
    select kbdprofile in blank_default heydoc gimp geany exit; do
        case $kbdprofile in
            blank_default) 
                kill -9 $(pidof xfconfd) &
                kill -9 $(pidof xfsettingsd) &
                cp $FoldeR/xfce4-keyboard-shortcuts__0_nil__.xml $FilE
                xfsettingsd &
                /usr/lib/x86_64-linux-gnu/xfce4/xfconf/xfconfd &
                exit
                ;;
            heydoc)
                kill -9 $(pidof xfconfd) &
                kill -9 $(pidof xfsettingsd) &
                cp $FoldeR/xfce4-keyboard-shortcuts__HEYDOC__.xml $FilE 
                xfsettingsd & 
                /usr/lib/x86_64-linux-gnu/xfce4/xfconf/xfconfd &
                exit
                ;;
            gimp) 
                kill -9 $(pidof xfconfd) &
                kill -9 $(pidof xfsettingsd) &
                cp $FoldeR/xfce4-keyboard-shortcuts__GIMP__.xml $FilE  
                xfsettingsd & 
                /usr/lib/x86_64-linux-gnu/xfce4/xfconf/xfconfd &
                exit
                ;;
            geany)
                kill -9 $(pidof xfconfd) &
                kill -9 $(pidof xfsettingsd) &
                cp $FoldeR/xfce4-keyboard-shortcuts__GEANY__.xml $FilE  
                xfsettingsd & 
                /usr/lib/x86_64-linux-gnu/xfce4/xfconf/xfconfd &
                exit
                ;;
            exit)
                exit
                ;;
            *) 
                echo $red "Invalid option >> $REPLY" $cyn
                ;;
        esac
    done
    }


    case $1 in 
            1) 
                kill -9 $(pidof xfconfd) &
                kill -9 $(pidof xfsettingsd) &
                cp $FoldeR/xfce4-keyboard-shortcuts__0_nil__.xml $FilE
                xfsettingsd &
                /usr/lib/x86_64-linux-gnu/xfce4/xfconf/xfconfd &
                exit
                ;;
            2)
                kill -9 $(pidof xfconfd) &
                kill -9 $(pidof xfsettingsd) &
                cp $FoldeR/xfce4-keyboard-shortcuts__HEYDOC__.xml $FilE 
                xfsettingsd & 
                /usr/lib/x86_64-linux-gnu/xfce4/xfconf/xfconfd &
                exit
                ;;
            3) 
                kill -9 $(pidof xfconfd) &
                kill -9 $(pidof xfsettingsd) &
                cp $FoldeR/xfce4-keyboard-shortcuts__GIMP__.xml $FilE  
                xfsettingsd & 
                /usr/lib/x86_64-linux-gnu/xfce4/xfconf/xfconfd &
                exit
                ;;
            4)
                kill -9 $(pidof xfconfd) &
                kill -9 $(pidof xfsettingsd) &
                cp $FoldeR/xfce4-keyboard-shortcuts__GEANY__.xml $FilE  
                xfsettingsd & 
                /usr/lib/x86_64-linux-gnu/xfce4/xfconf/xfconfd &
                exit
                ;;
            *) 
                runRoutine 
                ;;
    esac    

UPDATE: ok so I dealt with the repetition of the case constructions - which will make it much easier to change the script in future

#!/bin/bash

#  Script:    kbdpicker_2
#  Created:   2020.05.04

# https://linuxize.com/post/bash-select/            ===> select & case examples
# https://forum.xfce.org/viewtopic.php?id=11607     ===> kill & restart daemons
# https://forum.xfce.org/viewtopic.php?id=8860      <=== failed ideas for daemons
# https://forum.xfce.org/viewtopic.php?id=7878      <=== failed ideas for daemons

source $HOME/bin/bashcolours # import the colour schema 
###  CODES: rst blk red grn ylw blu mag cyn wte 
###  PREFIX: ...  (b)right   (d)im   (u)nderline   (BG) 
###  USAGE: 'echo $cyn"TextToPrint" $rst'

FoldeR="$HOME/.config/xfce4/xfconf/xfce-perchannel-xml"
FilE="$FoldeR/xfce4-keyboard-shortcuts.xml"


case $1 in 
        1) 
            kbdprofile="blank_default"
            echo $red "selected: $kbdprofile"
            ;;
        2)
            kbdprofile="heydoc"
            echo $red "selected: $kbdprofile"
            ;;
        3) 
            kbdprofile="gimp"
            echo $red "selected: $kbdprofile"
            ;;
        4)
            kbdprofile="geany"
            echo $red "selected: $kbdprofile"
            ;;
esac


if [[ -z $kbdprofile ]]
then 
    echo $cyn
    PS3="Select the kbdsetup profile to be used: "
    select kbdprofile in blank_default heydoc gimp geany exit
    do
         echo $red "selected: $kbdprofile"
    break
    done
fi

case $kbdprofile in
    blank_default) 
        kill -9 $(pidof xfconfd) &
        kill -9 $(pidof xfsettingsd) &
        cp $FoldeR/xfce4-keyboard-shortcuts__0_nil__.xml $FilE
        xfsettingsd &
        /usr/lib/x86_64-linux-gnu/xfce4/xfconf/xfconfd &
        exit
        ;;
    heydoc)
        kill -9 $(pidof xfconfd) &
        kill -9 $(pidof xfsettingsd) &
        cp $FoldeR/xfce4-keyboard-shortcuts__HEYDOC__.xml $FilE 
        xfsettingsd & 
        /usr/lib/x86_64-linux-gnu/xfce4/xfconf/xfconfd &
        exit
        ;;
    gimp) 
        kill -9 $(pidof xfconfd) &
        kill -9 $(pidof xfsettingsd) &
        cp $FoldeR/xfce4-keyboard-shortcuts__GIMP__.xml $FilE  
        xfsettingsd & 
        /usr/lib/x86_64-linux-gnu/xfce4/xfconf/xfconfd &
        exit
        ;;
    geany)
        kill -9 $(pidof xfconfd) &
        kill -9 $(pidof xfsettingsd) &
        cp $FoldeR/xfce4-keyboard-shortcuts__GEANY__.xml $FilE  
        xfsettingsd & 
        /usr/lib/x86_64-linux-gnu/xfce4/xfconf/xfconfd &
        exit
        ;;
    exit)
        exit
        ;;
    *) 
        echo $red "Invalid option >> $REPLY" $cyn
        ;;
esac
Atilla
  • 21
  • 2

1 Answers1

0

Some quick tips (I won't have time to go into details, all of these are mentioned on Greg's Wiki:

  1. kill -9 does not belong in scripts.
  2. By convention local variables (including function names) should be snake_case, exported values should be SHOUTY_SNAKE_CASE. No Bash variables should be mixed case.
  3. Use More Quotes™.
  4. #!/usr/bin/env bash is a more portable shebang.
  5. The default way to use a script should be non-interactive.
  6. Use shellcheck to get more tips.

A couple meta-tips:

  1. Don't post your updated code in the same question.
  2. This post is probably better suited for Code Review.
l0b0
  • 55,365
  • 30
  • 138
  • 223