Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何改进bash脚本-[1]选择&[2]case命令和[3]重新加载键绑定的复杂方法?_Linux_Bash_Keyboard Shortcuts - Fatal编程技术网

Linux 如何改进bash脚本-[1]选择&[2]case命令和[3]重新加载键绑定的复杂方法?

Linux 如何改进bash脚本-[1]选择&[2]case命令和[3]重新加载键绑定的复杂方法?,linux,bash,keyboard-shortcuts,Linux,Bash,Keyboard Shortcuts,长期潜伏者:我是一个彻头彻尾的修补匠,从来没有学过编码&我只是一个复制粘贴脚本的孩子。我想变得更好,所以我非常感谢关于如何改进我的kbdpicker脚本的想法 背景: 发行版=xubuntu 19.10 有一些程序我发现内置的键盘快捷键很难应用到肌肉记忆中,还有一些程序我不得不使用xdool来模拟鼠标运动,以windows7中的自动热键记忆为灵感创建快捷键 ==>如果我将numpad数字0-9设置为指定的键盘快捷键或xdotool命令,并且有一天我会为每个程序创建conky备忘单提醒,这对我来说

长期潜伏者:我是一个彻头彻尾的修补匠,从来没有学过编码&我只是一个复制粘贴脚本的孩子。我想变得更好,所以我非常感谢关于如何改进我的kbdpicker脚本的想法

背景:

发行版=xubuntu 19.10 有一些程序我发现内置的键盘快捷键很难应用到肌肉记忆中,还有一些程序我不得不使用xdool来模拟鼠标运动,以windows7中的自动热键记忆为灵感创建快捷键 ==>如果我将numpad数字0-9设置为指定的键盘快捷键或xdotool命令,并且有一天我会为每个程序创建conky备忘单提醒,这对我来说会容易得多 当然,我可以使用GUI设置>键盘来创建键绑定,但是它们会一直处于活动状态,我将无法使用numpad数字来输入数字 因此,我在$HOME/config/xfce4/xfcef/xfce perchannel xml文件夹中创建了多个版本的xfce-keyboard-shortcuts.xml文件,并创建了一个脚本bin/kbdpicker,它将特定于程序的配置文件复制/覆盖到活动配置文件::这意味着在终端中,我可以在默认numpad或自定义特定于程序的配置文件之间快速切换keybindings一旦脚本看起来更好并证明了自己,我实际上可能会将媒体密钥重新分配给不同的kbdpicker选择,而我从未使用过这些密钥。 这花了很多时间在谷歌上搜索,但我有点惊讶地说,kbdpicker脚本可以工作,但它是灾难性的不连贯,所以我真的很想就如何做得更好发表评论

重复case结构似乎是多余的,这样我就可以使用带有option方法的quickterminal命令或fulltui方法。 我真的应该有select命令吗?有没有更好的方法来做菜单选择器? 我真的需要强制我显式退出的Do/done循环吗?当我尝试不做某事时,我总是出错 我尝试了几种方法来刷新键盘快捷键,有点像。在那被改变之后。有更好的办法吗? 感谢阅读并提前感谢您的建议

祝福

阿提拉

ls$HOME/config/xfce4/xfconf/xfce perchannel xml | grep快捷方式

cat~/bin/kbdpicker

更新:好的,我处理了case结构的重复——这将使将来修改脚本变得更容易


一些快速提示我没有时间详细介绍,所有这些都在:

. 按照惯例,包括函数名在内的局部变量应该是snake\u case,导出的值应该是SHOUTY\u snake\u case。Bash变量不应混用。 . 是一款更便携的舍邦。 使用脚本的默认方式应为非交互式。 使用shellcheck获取更多提示。 以下几点提示:

不要在同一个问题中发布更新的代码。 这篇文章可能更适合你。
另外,看看StackOverflow的姐妹网站我投票决定结束这个问题,因为它属于CodeReviewhi@l0b0,只是意识到我从未说过谢谢
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
    #!/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    

#!/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