Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Checking for STDIN

by dbmathis (Scribe)
on Jul 30, 2008 at 01:13 UTC ( [id://700975]=perlquestion: print w/replies, xml ) Need Help??

dbmathis has asked for the wisdom of the Perl Monks concerning the following question:

HI all,

How would I go about added a check for STDIN in my perl script so that I can choose between STDIN or a logfile without failing when my script is used within a cron job?

After all this is over, all that will really have mattered is how we treated each other.

Replies are listed 'Best First'.
Re: Checking for STDIN
by AltBlue (Chaplain) on Jul 30, 2008 at 01:31 UTC
      -t will not work because I am using my script in a cron job sometimes.

        What do you mean?!

        Save this code to a file and test it.

        #!/usr/bin/perl -wl use strict; open my $fh, '>', '/tmp/test.log' or die $!; print $fh 'STDIN ', ( -t STDIN ? 'IS' : 'is NOT' ), ' available' or di +e $!; close $fh or die $!;
Re: Checking for STDIN
by jethro (Monsignor) on Jul 30, 2008 at 01:53 UTC
    STDIN is for reading, a logfile usually for writing. Do you really want to read from the logfile ?

    Also, please put some dividing line between your text and your sig. I don't know if I'm the only one, but having to read your sig everytime because I get the impression it is part of the text is somewhat like getting spam.

      I have a perl script that at the moment has a command line option -logfile <file> where it gets input by reading the <file> into the script. I would like to be able to check to see if stdin is being piped into my file so that I can change the flow of my script and get my input for the pipe instead.

      When the -t option is used within a cron job the -t test evalutes to false or in my case it would make my script appear to be having input piped to it when in fact in my cron job I have -logfile <file> specified.

      Make sense?
        Sounds like what you actually want to do is check of -logfile was specified.
Re: Checking for STDIN
by Bloodnok (Vicar) on Jul 30, 2008 at 10:43 UTC
    Shell scripts tend to use the $- environment variable in order to distinguish between interactive and non-interactive e.g. cron, invocations - it being set for interactive invocations.

    HTH,

    Update

    Note, also, that if invoked non-interactively e.g. by cron(1), the environment is very basic i.e. none of the login scripts will have been run, so it's always best to invoke them in the cron(1) command e.g. * * * * * . $HOME/profile ; some_cmd > /dev/null 2>&1

    At last, a user level that overstates my experience :-))

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://700975]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2025-07-11 22:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.