Bash replacement to getchar | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Bash replacement to getchar

Shell Programming and Scripting


Tags
bash getchar

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-13-2009
Asafe Asafe is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 18 October 2009, 12:19 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Bash replacement to getchar

There's a replacement in bash for getchar or get functions of C and C++?Those functions read the next char avaliable in the input stream.
I've tried something like:

Code:
OLD_STTY=`stty -g`
  stty cbreak -echo
  look=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
  stty $OLD_STTY

But it is not working very well.
Sponsored Links
    #2  
Old Unix and Linux 08-13-2009
kshji's Unix or Linux Image
kshji kshji is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 26 March 2015, 2:39 AM EDT
Location: Finland
Posts: 616
Thanks: 0
Thanked 54 Times in 53 Posts
Generic for all shells

Code:
read -n1 kbd

If you not echo then use stty -echo before read and stty echo after.

Also this "old standard" works:

Code:
echo -n "Press key:"
stty -echo raw
c=$(dd bs=1 count=1 2>/dev/null )
stty echo -raw
echo " key:$c"

Ofcourse you can save current stty and return it, but usually echo is on and raw is off.

Ksh include KEYBD interrupt = you can trap KEYBD.

Code:
keybd()
{
    key="${.sh.edchar}"
}

trap 'keybd' KEYBD
while :
do
        read a
done

Sponsored Links
    #3  
Old Unix and Linux 08-13-2009
Asafe Asafe is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 18 October 2009, 12:19 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
echo -n "Press key:"
stty -echo raw
c=$(dd bs=1 count=1 2>/dev/null )
stty echo -raw
echo " key:$c"

It interpreters ctrl-c and ctrl-d literally... Is there a way to make it to be aborted by a signal?
    #4  
Old Unix and Linux 08-14-2009
kshji's Unix or Linux Image
kshji kshji is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 26 March 2015, 2:39 AM EDT
Location: Finland
Posts: 616
Thanks: 0
Thanked 54 Times in 53 Posts
With this you can debug:

Code:
trap 'echo "int shell"' HUP INT QUIT
echo -n "Press key:"
stty -echo raw
c=$(dd bs=1 count=1 2>/dev/null )
stty echo -raw
echo "<$c>"
echo $c | od -c

echo "press ctrl-c"
sleep 20

result: dd read one char from stdin. Also ctrl-c and ctrl-d are only chars. So you can test those chars.


Code:
trap ':'  HUP INT QUIT
c=""
while [ "$c" = "" ]
do
  # do c read, look previous dd or
  echo -n "key:"
  read -n 1 c 
  # test it
  case "$c" in
    \001) ;; # ctrl-A
    \003) ;; # ctrl-C
  esac
  # remove char ascii 01-04
  c=$(echo "$c" | tr -d "[\001-\004]" )
done

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need Replacement for sed dbsurf Shell Programming and Scripting 0 01-25-2008 05:47 PM
Regarding Replacement rajx UNIX for Dummies Questions & Answers 2 05-22-2007 09:19 AM
Replacement using sed handak9 UNIX for Dummies Questions & Answers 5 07-13-2004 11:28 AM
cpu replacement. help IMPTRUE UNIX for Dummies Questions & Answers 3 07-25-2003 08:40 AM
getchar() primal UNIX for Dummies Questions & Answers 2 10-09-2001 04:06 PM



All times are GMT -4. The time now is 04:54 PM.