![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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); 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 |
|
||||
|
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;
Code:
bash$ ./test.sh The input was, if any: bash$ echo "test" | ./test.sh The input was, if any: test Code:
stty: standard input: Invalid argument |
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|