繁体   English   中英

如何在Linux中将bash脚本分配给键盘键

[英]How to assign a bash script to a keyboard key in Linux

我想将bash脚本分配给Linux中的键盘键,以在多个命令之间切换。 我研究了这个问题,并遇到了以下bash脚本:

#!/bin/sh

TOGGLE=$HOME/.toggle

if [ ! -e $TOGGLE ]; then
    touch $TOGGLE
    command1
    rm $TOGGLE
    command2
fi

问题是我不知道如何修改脚本以添加第三或第四命令。 我实际上是想回答这个问题,我认为这是适合他的方法。

如何在脚本中的多个命令之间切换?

您需要的不仅仅是一个简单的切换(只有两个状态); 您需要一个包含要更新的值的文件。 一个简单的四态示例:

# Script that rotates between English, Russian, French, and Finnish keyboards

# Name of the state file
state_file=$HOME/.keyboard_state

# Ensure that the state file exists; initialize it with 0 if necessary.
[ -f "$state_file" ] || printf '0\n' > "$state_file"

# Read the next keyboard to use
read state < "$state_file"

# Set the keyboard using the current state
case $state in
    0) setxkbmap en ;;
    1) setxkbmap ru ;;
    2) setxkbmap fr ;;
    3) setxkbmap fi ;;
esac

# Increment the current state.
# Could also use state=$(( (state + 1) % 4 ))
state=$((state + 1))
[ "$state" -eq 4 ] && state=0

# Update the state file for the next time the script runs.
printf '%s\n' "$state" > "$state_file"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM