Bash One Liner INKEY$ Function...

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Bash One Liner INKEY$ Function...

Postby Bazza » Tue Mar 26, 2013 12:55 pm

This a simple one liner code snippet is a function that immitates the BASIC:-

LET char$=INKEY$

For bash scripts...

Just read the code for more info'.

Bazza...

Code: Select all
#!/bin/bash

# An INKEY$ function for bash!
inkey() { char="" ; read -p "" -n1 -s -t1 char ; }
# Similar to BASIC's LET char$=INKEY$

# Do you remember INKEY$ in BASIC programming?
# Example:-
#
# PRINT "Some prompt:- "
# some_label:
# LET char$=INKEY$
# IF char$="<some_character>" THEN <do_something>
# IF char$="" THEN <do_something_else>
# GOTO some_label

# This is just a test piece only...
while true
do
   printf "Some prompt:- "
   # This is LET char$=INKEY$...
   inkey
   printf "Key pressed:- '$char'...\n"
   if [ "$char" == "q" ]
   then
      printf "Quitting... \n"
      break
   fi
   if [ "$char" == "" ]
   then
      printf "Timeout works OK...\n"
   fi
   if [ "$char" == "b" ]
   then
      printf "Barry Walker...\n"
   fi
done
73...

Bazza, G0LCU...

Team AMIGA...
User avatar
Bazza
LXF regular
 
Posts: 1482
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron