Welcome, guest | Sign In | My Account | Store | Cart

This is probably common knowledge to the professionals but not so much for amateurs like myself.

This is a code snippet for the equivalent of BASIC's...

LET char$=INKEY$

As the timeout parameter cannot be less than 1 second then this is the only limitation...

It is a single line function which has a variable "char"...

Read the code for more information...

There are now two versions, edit out and choose which is best for you...

Bash, 82 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Version one...
Edit out as resquired...

#!/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 "$char...\n"
	if [ "$char" == "q" ]
	then
		printf "\nQuitting...\n\n"
		break
	fi
	if [ "$char" == "" ]
	then
		printf "Timeout works OK...\n"
	fi
	if [ "$char" == "b" ]
	then
		printf "Barry Walker...\n"
	fi
done



Version two...
Edit out as required...

#!/bin/bash

# Another INKEY$ function for bash!
inkey() { char="" ; stty -icanon min 0 time 1 ; char=`dd count=1 2> /dev/null` ; }
# 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

while true
do
	printf "Some prompt:- "
	# This is LET char$=INKEY$...
	inkey
	printf "$char...\n"
	if [ "$char" == "q" ]
	then
		printf "\nQuitting... \n\n"
		break
	fi
	if [ "$char" == "" ]
	then
		printf "Timeout works OK...\n"
	fi
	if [ "$char" == "b" ]
	then
		printf "Barry Walker...\n"
	fi
done

Enjoy finding simple solutions to often very difficult problems...

I really don't care how you vote this as it will be useful to someone...

Obviously with the downvote, not to professionals however...

Bazza, G0LCU...

1 comment

Barry Walker (author) 11 years, 1 month ago  # | flag

Someone voted it down.

Oh well, matters not...

Can't win 'em all...

Created by Barry Walker on Mon, 25 Mar 2013 (CC0)
Bash recipes (41)
Barry Walker's recipes (73)

Required Modules

  • (none specified)

Other Information and Tasks