[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Question about bash read function and matching ESC



On Wed, 1 Oct 2003 HdV@DTO.TUDelft.NL wrote:

> Hi,
>
> I am trying to match the ESC key in bash. The code below works as
> intended when I try to match a 'normal' key like 's' or so. But it will
> not for the escape key. This makes me expect this has something to do
> with the high-bit stuff, but I haven't figured out how to work around
> it. Anyone have a suggestion?

I have found a way to do what I want. To me it is ugly, but at least it does
work as intended. For the benefit of the archives (and those searching them)
here's the code:

echo Some prompt (press ESC to exit)
if read -n 1 -s -t $TIMEOUT KEY ; then
  KEY=$( echo $KEY | od -t x1 | awk '{ print $2}' )
  if [ "$KEY" == '1b' ]; then
    echo BAILOUT: nothing changed
    exit 0
  fi
  echo Do something useful here...
else
  echo TIMEOUT: nothing changed
  exit 0
fi

Grx HdV



Reply to: