当F1为hitter时执行某些操作的Linux脚本

当F1为hitter时执行某些操作的Linux脚本,linux,bash,Linux,Bash,我有这个脚本start.sh #!/bin/bash while[1] do read -sn3 key if [$key=="\033[[A"] then ./test1 else ./test2 fi done 我想设置一个永久循环检查,看看是否按了F1键。如果按下,则执行test1,否则执行test2。我确实在后台运行start.sh&以便其他程序可以运行 我出错了 而[1]命令未找到 意外标记“do”附近出现语法错误 [f==\033]:未找到命令 此外,该读取命令位于何处

我有这个脚本start.sh

 #!/bin/bash
while[1]
do 
read -sn3 key
if [$key=="\033[[A"]
then
  ./test1
else
  ./test2
fi
done
我想设置一个永久循环检查,看看是否按了F1键。如果按下,则执行test1,否则执行test2。我确实在后台运行start.sh&以便其他程序可以运行

我出错了 而[1]命令未找到 意外标记“do”附近出现语法错误 [f==\033]:未找到命令

此外,该读取命令位于何处?我输入了读过的内容,但没有找到

另外,如果try./start.sh&它会给出完全不同的行为。我输入一个密钥,它显示找不到该密钥。我想&只是在后台运行脚本

应该是

while :

试试这个:

#!/bin/bash

while true
do 
  read -sn3 key
  if [ "$key" = "$(tput kf1)" ]
  then
    ./test1
  else
    ./test2
  fi
done
使用
tput
生成控制序列更可靠,您可以在
man terminfo
中看到完整列表。如果
tput
不可用,您可以对大多数终端仿真器使用
$'\eOP'
,或对Linux控制台使用
$'\e[[A'
(字符串需要
$
,以使bash解释转义序列)


read
是一个
bash
内置命令-请尝试
help read

您的代码中有几个基本语法问题(请考虑在发布前使用它来清除这些问题),但这种方法本身是有缺陷的。点击“q”和“F1”会产生不同长度的输入

下面是一个脚本,它依赖于转义序列都在同一个读取调用中,这是肮脏但有效的:

#!/bin/bash
readkey() {
  local key settings
  settings=$(stty -g)             # save terminal settings
  stty -icanon -echo min 0        # disable buffering/echo, allow read to poll
  dd count=1 > /dev/null 2>&1     # Throw away anything currently in the buffer
  stty min 1                      # Don't allow read to poll anymore
  key=$(dd count=1 2> /dev/null)  # do a single read(2) call
  stty "$settings"                # restore terminal settings
  printf "%s" "$key"
}

# Get the F1 key sequence from termcap, fall back on Linux console
# TERM has to be set correctly for this to work. 
f1=$(tput kf1) || f1=$'\033[[A' 

while true
do
  echo "Hit F1 to party, or any other key to continue"
  key=$(readkey)
  if [[ $key == "$f1" ]]
  then
    echo "Party!"
  else
    echo "Continuing..."
  fi
done

你的shebang坏了,你是说
#!/bin/bash
?如果你正在使用bash,请阅读
帮助,同时了解正确的语法,或者看看它是否必须是F1?使用非转义键(如y/n,1/2,a/bYes)会容易得多,如果被术语捕获,F1将不起作用。例如对于XFCE终端,F!会显示帮助。如果是这种情况,你可以使用我必须禁用此命令,以便将其传递到
pts
设备。@lilzz
tput
是一个使用
terminfo
库生成控制序列的命令。其思想是它可以与各种不同的终端一起工作,它位于Debian/Ubuntu上的
ncurses bin
包中。如果您没有t、 您可以在此处使用控制序列-也可以使用
$()
是命令替换,它是一种在另一个命令中使用一个命令的stdout
的方法。很好!最好默认为
xterm
转义序列,而不是linux,或者自己检查
TERM
。它应该比各种终端仿真器阵列更可靠地为linux控制台设置。@Graeme这个问题的答案是代码使用Linux控制台序列,所以他们的终端很可能就是这样使用的。
#!/bin/bash
readkey() {
  local key settings
  settings=$(stty -g)             # save terminal settings
  stty -icanon -echo min 0        # disable buffering/echo, allow read to poll
  dd count=1 > /dev/null 2>&1     # Throw away anything currently in the buffer
  stty min 1                      # Don't allow read to poll anymore
  key=$(dd count=1 2> /dev/null)  # do a single read(2) call
  stty "$settings"                # restore terminal settings
  printf "%s" "$key"
}

# Get the F1 key sequence from termcap, fall back on Linux console
# TERM has to be set correctly for this to work. 
f1=$(tput kf1) || f1=$'\033[[A' 

while true
do
  echo "Hit F1 to party, or any other key to continue"
  key=$(readkey)
  if [[ $key == "$f1" ]]
  then
    echo "Party!"
  else
    echo "Continuing..."
  fi
done