如何改进我的 bash 脚本 - [1] select & [2] case 命令和 [3] 重新加载键绑定的曲折方法?



长期潜伏者:我是一个彻头彻尾的修补匠,从来没有学会编码&我只是在复印&粘贴脚本儿童级别。我想变得更好,所以我很感激关于如何改进我的"kbdpicker"脚本的想法。

上下文:

  • distro=xubuntu 19.10
  • 在一些程序中,我发现内置的键盘快捷键很难用于肌肉记忆,而在铬(+/-其他一些程序(中,我必须使用xdotool模拟鼠标移动来创建快捷键(灵感来自windows7中的自动热键记忆(===>如果我把数字0-9设置为指定的键盘快捷键或xdotool命令,对我来说会容易得多(总有一天我会为每个程序创建骗子备忘单提醒(
  • 我当然可以使用GUI设置>键盘来创建键绑定,但然后它们将一直处于活动状态,我将无法使用数字键盘输入数字
  • 因此,我在$HOME/config/xfce4/xfconf/xfce-perchannel-xml文件夹中创建了xfce-keyboard-shortcuts.xml文件的多个版本,并创建了一个脚本bin/kbdpicker,它将程序特定的配置文件复制/覆盖到活动配置文件::这意味着在终端中,我可以在默认的数字键盘或自定义的程序特定的键绑定之间快速切换(一旦脚本看起来更好并证明了自己,我实际上可能会将媒体密钥(我从未使用过(重新分配给不同的kbdpicker选择(

它花了很多谷歌搜索,但我有点震惊地说,kbdpicker脚本有效,但它是灾难性的不雅,所以我真的很想评论如何做得更好。

  • 重复事例结构似乎是多余的,这样我就可以使用"带选项的快速终端命令"方法或"完整TUI"方法
  • 我真的应该有select命令吗?有更好的方法来进行菜单选择吗
  • 我真的需要强制我显式退出的Do/done循环吗?(当我尝试不做的时候,我总是出错(
  • 我尝试了几种方法来刷新键盘快捷键(有点像更改后的. .bashrc(。有更好的方法吗

感谢您的阅读,并提前感谢您的任何建议。

向致以最良好的祝愿

Atilla

ls$HOME/config/xfce4/xfconf/xfce通道xml|grep快捷键

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

更新:好的,所以我处理了案例结构的重复-这将使将来更改脚本变得更容易

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

一些快速提示(我没有时间详细介绍,所有这些都在Greg的Wiki上提到:

  1. kill -9不属于脚本
  2. 按照惯例,局部变量(包括函数名(应为snake_case,导出值应为SHOUTY_SNAKE_CASE。没有Bash变量应该是混合大小写
  3. 使用更多报价™.
  4. #!/usr/bin/env bash是一个更便携的shebang
  5. 使用脚本的默认方式应该是非交互式的
  6. 使用shellcheck获取更多提示

几个元提示:

  1. 不要在同一个问题中发布更新的代码
  2. 这篇文章可能更适合代码审查

最新更新