Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can i check tty from perl script? 1

Status
Not open for further replies.

lnbnatera

Technical User
Feb 14, 2006
54
HK
hi,

is there a way for me to determine whether perl executes in a tty for proper presentation of output?

let's say given script.pl...
from a terminal:
$ script.pl <enter>
some output...

but, from a crontab, no output should be generated on screen. the output would be an email to someone@domain.com
 
I'm sure there's a way to do what you're looking for, but I'm not sure what it is.

I do have a suggestion though - why not use some kind of switch to specify the output should go to email. Then you could run the script from your cron like 'script.pl -e' and leave the -e off if you want the output to go to STDOUT. You could either parse the arguments yourself or use a module like Getopt::Std to do the work for you.
 
thanks for the suggestion, but i think i will still search for that kind of approach because it simplifies the actuall script itself instead of having to type another argument. from a shell script point of view, its something like this...

tty 1>/dev/null 2>&1
if [ $? -eq 0 ]
then
INTERACTIVE=1 # Email output
else
INTERACTIVE=0 # Show output on screen
fi

however, i still don't want to use the exec() or the system() for this specific functionality.
 
Code:
if $ARGV[0] eq "" {
Code:
if $ARGV[0] ==0 {

Check if ANY parameters are passed to the script check out preldoc.perl.org, check out select (as in filehandles) for redirection

HTH

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
actually my script does not require any parameters
 
You might be able to tell by examining the $ENV variables. Maybe a little experimenting will lead to something.
 
Adding a switch complicates a script? Really? What if you're running interactively and still want an email? The functionality is already there in your script, why not use it?

Assuming that you have the code for writing to STDOUT or an email already in place, you can add a few lines to your script and make the -e switch work:
Code:
use Getopt::Std;
my %options;
unless (getopts('e', \%options)) { die "Invalid switch."; }
if ($options{e}) {print "Send an email";}
But, if you still don't like that idea, maybe this link to perlfaq8 will help you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top