ksh: how to probe stdin? ksh: how to probe stdin? unix unix

ksh: how to probe stdin?


EDIT: You are running on HP-UX

Tested [ -t 0 ] on HP-UX and it appears to be working for me. I have used the following setup:

/tmp/x.ksh:

#!/bin/ksh/tmp/y.ksh

/tmp/y.ksh:

#!/bin/kshtest -t 0 && echo "terminal!"

Running /tmp/x.ksh prints: terminal!

Could you confirm the above on your platform, and/or provide an alternate test setup more closely reflecting your situation? Is your script ultimately spawned by cron?


EDIT 2

If desperate, and if Perl is available, define:

stdin_ready() {  TIMEOUT=$1; shift  perl -e '    my $rin = "";    vec($rin,fileno(STDIN),1) = 1;    select($rout=$rin, undef, undef, '$TIMEOUT') < 1 && exit 1;  '}stdin_ready 1 || 'stdin not ready in 1 second, assuming terminal'

EDIT 3

Please note that the timeout may need to be significant if your input comes from sort, ssh etc. (all these programs can spawn and establish the pipe with your script seconds or minutes before producing any data over it.) Also, using a hefty timeout may dramatically penalize your script when there is nothing on the input to begin with (e.g. terminal.)

If potentially large timeouts are a problem, and if you can influence the way in which your script is called, then you may want to force the callers to explicitly instruct your program whether stdin should be used, via a custom option or in the standard GNU or tar manner (e.g. script [options [--]] FILE ..., where FILE can be a file name, a - to denote standard input, or a combination thereof, and your script would only read from standard input if - were passed in as a parameter.)


This strategy works for bash, and would likely work for ksh. Poll 'tty':

#!/bin/bashset -aif [ "$( tty )" == 'not a tty' ]then    STDIN_DATA_PRESENT=1else    STDIN_DATA_PRESENT=0fiif [ ${STDIN_DATA_PRESENT} -eq 1 ]then    echo "Input was found."else    echo "Input was not found."fi


Why not solve this in a more traditional way, and use the command line argument to indicate that the data will be coming from stdin?

For an example, consider the difference between:

echo foo | cat -

and

echo foo > /tmp/test.txt

cat /tmp/test.txt