Sponsored Content
Full Discussion: Bash INKEY$ function...
Top Forums UNIX for Dummies Questions & Answers Bash INKEY$ function... Post 302785495 by wisecracker on Monday 25th of March 2013 05:51:01 PM
Old 03-25-2013
Bash INKEY$ function...

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...

Issued as Public Domain...

Code:
#!/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

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need assistance with bash function

Hi all! I need a little help with an imbedded fuction I am trying to write. What I am trying to do is go to a series of hosts and get the contents of an XML file, storing some of the data, along with the name of the host it was gathered from. My intent is to store this in an array for later... (1 Reply)
Discussion started by: _jade_
1 Replies

2. Shell Programming and Scripting

Need help in using power function in Bash

Hi, I would like to use a power function in Bash in an awk '{ } . e.g pow(2,3)=8 Any suggestion? I try to find online resources but most of them stated by using BC. Please advise. Thanks. (3 Replies)
Discussion started by: ahjiefreak
3 Replies

3. Shell Programming and Scripting

bash search function

I want to have a function with a similar interface: search *.cpp asdf that will search recursively all directories for *.cpp file, containing line 'asdf' inside. I tried this: function search { find . -name "$1" | xargs grep -li $2; } But it doesn't work all the time. For example, if i run it... (3 Replies)
Discussion started by: doze
3 Replies

4. Shell Programming and Scripting

[Bash]Function returning a boolean

Hello all, I would like to know if it is possible to return a the result of a boolean expression from a function like this function() { # some code return || } and what will be the return value ? Thank you for help. (6 Replies)
Discussion started by: dolphin06
6 Replies

5. Shell Programming and Scripting

In bash getting weird output from function ?

My script- result="" times() { echo "inside the times function" result=8 echo "Inside function $result" return $result } result=$(times) echo "the value is "$? echo "the value of result $result" when I run I get this, why the value still remain 0. $ ./func the value is 0 the value... (5 Replies)
Discussion started by: boy18nj
5 Replies

6. Shell Programming and Scripting

BASH function error

Hey everyone. I am currently testing my first function based BASH script. The ultimate goal is going to be moving logs from point A to point B (or if B is down, to point C). Part of this involves the following function: function testAlive{ ping -c 1 -q $1 } Now when I run ping -c... (1 Reply)
Discussion started by: msarro
1 Replies

7. Shell Programming and Scripting

Bash function

startvm() { startguest } Is there a way use one line to get this ? actually I want startvm=startguest (5 Replies)
Discussion started by: yanglei_fage
5 Replies

8. Shell Programming and Scripting

Yes or No selection within bash function

I need to add a selection within the bash function below and am having some trouble doing so. phox2b() { printf "\n\n" printf "What is the id of the patient getting Phox2B analysis : "; read id printf "Is this an intronic variant? Y/N "; read match_choice case... (5 Replies)
Discussion started by: cmccabe
5 Replies

9. Shell Programming and Scripting

Bash function, for BSD

I am putting this thread to shell-threads, because it is about how to make a function work properly. I need a hint for declaring a function right, it has been more than a year I did not work that straight with bash. So my aim is to turn off the eth0 (as it would be in linux, and bge0 in bsd ),... (2 Replies)
Discussion started by: 1in10
2 Replies

10. Shell Programming and Scripting

Bash function problem

I am trying to figure out why I am having a "problem" with some functions in a bash script I am running. The reason for air quoting is that the functions are working, they are just not displaying anything to screen when called from another function. Here's an example: function Create_Input {... (6 Replies)
Discussion started by: dagamier
6 Replies
XPRINTF(3)						   BSD Library Functions Manual 						XPRINTF(3)

NAME
asxprintf, dxprintf, fxprintf, sxprintf, xprintf, vasxprintf, vdxprintf, vfxprintf, vsxprintf, vxprintf -- extensible printf SYNOPSIS
#include <printf.h> int asxprintf(char ** restrict ret, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, ...); int dxprintf(int fd, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, ...); int fxprintf(FILE * restrict stream, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, ...); int sxprintf(char * restrict str, size_t size, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, ...); int xprintf(printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, ...); #include <stdarg.h> int vasxprintf(char ** restrict ret, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, va_list ap); int vdxprintf(int fd, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, va_list ap); int vfxprintf(FILE * restrict stream, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, va_list ap); int vsxprintf(char * restrict str, size_t size, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, va_list ap); int vxprintf(printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, va_list ap); DESCRIPTION
These extensible printf (see xprintf(5)) variants behave like their normal printf counterparts (see printf(3)) without 'x' in the name (except sxprintf() and vsxprintf() behave like snprintf() and vsnprintf(), respectively). The domain argument must be a pointer to a printf domain structure, as returned by one of the functions described in xprintf_domain(3). The loc argument should be an extended locale (see xlocale(3)) or NULL, which means to use the current locale in effect (either the per-thread locale if set, or the global locale by default). SEE ALSO
printf(3), xlocale(3), xprintf_domain(3), xprintf(5) Darwin Aug 19, 2012 Darwin
All times are GMT -4. The time now is 11:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy