The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bash: Nested functions and including other scripts FractalizeR Shell Programming and Scripting 12 08-26-2008 11:15 AM
Ask Linux.com: IT, Japanese, and crafting bigger and better bash scripts iBot UNIX and Linux RSS News 0 07-12-2008 09:20 AM
Bash Scripts - File generating JayC89 Shell Programming and Scripting 1 10-04-2007 09:58 AM
How to pass passwords to bash scripts? siegfried Shell Programming and Scripting 5 08-04-2006 05:59 PM
Bash Shell Scripts sonbag_pspl UNIX for Dummies Questions & Answers 2 08-27-2004 01:31 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-09-2008
neked neked is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 56
Nonblocking I/O in bash scripts

Hello,

Is there a way to perform nonblocking I/O reads from standard input in a bash script?

For example, in C, you can say:

Code:
int flags = fcntl(STDIN_FILENO, F_GETFL);
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
ch = fgetc(stdin);
the 'fgetc' function will not block on standard input but will attempt to read and return ch = -1 if there was no input waiting.

Bash has the builtin "read" command, which hangs on stdin until it can read some characters (or until it times out, if you set it to read with the -t option), but there seems to be no real way to imitate the C behavior described above. Is there?

Thanks,
Neked
  #2 (permalink)  
Old 10-10-2008
neked neked is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 56
Is this the appropriate forum for this topic, or should I move it elsewhere?
  #3 (permalink)  
Old 10-10-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361

Use stty and dd. Here is some sample code:

Code:
stty -echo -icanon time 0 min 0
while :
do
  key=$( dd bs=1 count=1 2> /dev/null; printf "." )
  key=${key%.}
  case $key in
    q) break ;;
    ?) printf "%d\n" "'$key" ;;
  esac
done
stty sane
  #4 (permalink)  
Old 10-10-2008
neked neked is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 56
Pretty damn cool cfajohnson.

Here is my implementation based on your stty idea and some modifications:

Code:
#!/bin/bash

if [ -t 0 ]; then
    stty -echo -icanon time 0 min 0
fi;

read line
echo "The input was, if any: " $line

if [ -t 0 ]; then
    stty sane
fi;
if that code was saved in an executable named test.sh, then you could do the following:

Code:
bash$ ./test.sh
The input was, if any: 
bash$ echo "test" | ./test.sh
The input was, if any: test
The reason I'm using the if [ -t 0 ]; statement is to make sure that standard input is coming from the terminal, not a pipe, otherwise you'll get annoying errors:
Code:
stty: standard input: Invalid argument
  #5 (permalink)  
Old 10-10-2008
neked neked is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 56
Thumbs up

This inspired me to do a much simpler version of the code above, without the use of stty:

Code:
#!/bin/bash
if [ ! -t 0 ]; then
    read line
    echo $line
fi
  #6 (permalink)  
Old 10-10-2008
neked neked is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 56
Unfortunately none of the approaches above work unless a terminal or a pipe is present, so if you have the aformentioned test.sh on a remote machine, then:

Code:
bash$ echo "test" | ssh remote_machine ./test.sh
test
bash$ ssh -t remote_machine ./test.sh <== using -t flag to force terminal creation

bash$ ssh remote_machine ./test.sh
<== it blocks here on the read
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:49 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0