Linux script execute something when f1 striker

I have this script start.sh

 #!/bin/bash
while[1]
do 
read -sn3 key
if [$key=="\033[[A"]
then
  ./test1
else
  ./test2
fi
done

I want to set up verification forever, check if the F1 key is pressed. If clicked, run test1 else test2. I started start.sh and worked in the background so that other programs can start.

I got an error while command [1] did not find syntax error near unexpected token 'do' [f == \ 033]: command not found

And where is this read command located? I print what I read, he did not find it.

Also, if try./start.sh and it gives a completely different behavior. I enter the key and it says that the key was not found. Although I run the script in the background

+3
source share
2 answers

( shellcheck , ), . "q" "F1" .

a script, , escape- , , :

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

:

#!/bin/bash

while true
do 
  read -sn3 key
  if [ "$key" = "$(tput kf1)" ]
  then
    ./test1
  else
    ./test2
  fi
done

tput , man terminfo. tput , $'\eOP' $'\e[[A' Linux ( $ , bash escape-).

read bash - help read.

0

All Articles