Is there an easy check to tell if a script is being invoked from within a DOS shell vs from a browser

This may be a little more than you are really looking for, but it is a quick example of how to extend your tool.

#!/usr/bin/perl -w use strict; use Getopt::Long; use vars qw: %switches %actionTable @logActions :; %actionTable = ( 'email' => \&_logEmail, 'browser' => \&_logBrowser, 'console' => \&_logConsole, 'file' => \&_logFile, ); $|++; &GetOptions( \%switches, "email:s", ## script provides a default if none given "file=s", ## MUST have a file name if you want to log to a +file "console", ## console can be target "browser", ## or browser can be target( or both.. ) "url=s", ## url to begin from "help" ## jsut a usage thing ); ## are we just looking for help? if ( $switches{ 'help' } ) { print <<EOHELP; switches: --url <url> site you want to work on --email <address or default if blank> to save output to ema +il --file <file to open> to save output to file --console to use console format --browser to use browser format --help this page EOHELP exit(0); } ## build our @logActions so we know what we have to log to if ( defined( $switches{ 'email' } ) ) { ## we have an email, but it may be empty string, so use default ## notice class act style here! $switches{ 'email' } = 'root@localhost' if ( $switches{ 'email' } +eq "" ); ## open email here if you are going to use it ## ie open( EMAIL, "| /usr/sbin/sendmail -blah )... push( @logActions, 'email' ); } if ( defined( $switches{ 'file' } ) ) { ## open our file, do your security checking too ## open( FILE, '>' . $switches{ 'file' } ) ... push( @logActions, 'file' ); } if ( $ENV{ 'REMOTE_HOST' } ) { ## don't print console data if from browser ## this could be better, but for the sake of an example it'll do $switches{ 'console' } = 0; $switches{ 'browser' } = 1; } ## note no real error checking if no file/email/console/browser select +ed ## legal and illegal combinations are up to the reader push( @logActions, 'console' ) if $switches{ 'console' }; push( @logActions, 'browser' ) if $switches{ 'browser' }; ## do you stuff print '-' x 11, ' START ', '-' x 11, "\n"; output( 'test message a' ); output( 'test message b' ); output( 'test message c' ); print '-' x 11, ' END ', '-' x 11, "\n"; exit(0); sub output { my $text = shift; defined( $actionTable{ $_ } ) && &{$actionTable{ $_ }}( $text ) fo +reach @logActions; } sub _logConsole { print "Console: $_[0]\n"; } sub _logBrowser { print "Browser: <p>$_[0]</p>\n"; } sub _logEmail { ## print EMAIL "$_[0]\n"; print "Email: $_[0]\n"; } sub _logFile { ## print FILE "$_[0]\n"; print "File: $_[0]\n"; }

I know I find the command line much easier to work with, so I hope this helps you. Also, remember this is just an example, there is a lot left to work out.

Will perl for money
JJ Knitis
(901) 756-7693
gt8073a@industrialmusic.com


In reply to Re: Outputting to browser vs DOS...how can you tell? by gt8073a
in thread Outputting to browser vs DOS...how can you tell? by S_Shrum

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.