This script may be run via cli, and i want to also be able to have another script call it, and thus get some sort of error codes.. now.. if a human is running it via cli, i print out usage, but if a script runs it.. i need codes .. or something.. im getting a little outerspacy with this...

In the office we have a crazy long text file that controls options on a scanner. This scanner (one of those fifty bujillion grand metal things) reads this userdirs.txt file to know what options to give the person manning it- to send t he scan to. It basically puts it in a preset path in the network.

So, if you choose, "marcus salvatore", the scan might go to $pathprepend/msalvatore/incoming

The format for this text file, userdirs.txt, is a little freaky (there are over 800 choices of destinations). So I made a script that autogenerates it according to various things. Trust, me, this was needed.
This script is cli, and lets you provide some arguments as to waht you want to do. For example.. To add a destination.. you can..

I want to make an interface for it, so people on an intranet can access it via a web interface. So, I'm writing a cgi for it. Basically an interface that allows them to specify from a dropdown , entering new destinations and deleting others.

So my script will be called via system, backticks or exec .. from a cgi.

So my question.. how should my ./script return error codes?? print STDERR 2344; # code nums ??? should I be printing to STDOUT , to then optionally save the output as I was doing with >, or doesn't print inherently print to STDOUT?

I want to keep ./script as a cli interface.

the script for curiosity's sake:

#!/usr/bin/perl -w use strict; use Conf2Hash; use Getopt::Std; my $o={}; getopts('ga:d:l:',$o); sub usage { print STDERR <<USAGE; (generate_userdirs.pl, Leo Charre, lcharre\@dyercpa.com, v 0.1) OPTIONS: -a add/this/dir -l "label for this dir" -d delete/this/dir -g print new USERDIRS.TXT ro STDOUT after change it does not actually delete the dir, it deletes the ref to it +in USERDIRS.TXT EXAMPLE 1: imagine we want to add sfarrow to the list of people that come up +in the scanner, make sure /var/www/DAMS-Content/userfiles/sfarrow/incoming exists run this command : # /var/www/DAMS-Content/generate_userdirs.pl -a userfiles/sfarrow/ +incoming -l "Samantha Farrow" that command will make the change to labeled.conf and add the user if you also want to output the change and rewrite the old USERDIRS +.TXT, then do this command: # /var/www/DAMS-Content/generate_userdirs.pl -a userfiles/sfarrow/ +incoming -l "Samantha Farrow" -g > /var/www/DAMS-Content/USERDIRS.TXT +; EXAMPLE 2: to simply regenerate USERDIRS.TXT: # /var/www/DAMS-Content/generate_userdirs.pl -g > /var/www/DAMS-Co +ntent/USERDIRS.TXT; EXAMPLE 3: to delete the above entry from USERDIRS.TXT: # /var/www/DAMS-Content/generate_userdirs.pl -d userfiles/sfarrow/ +incoming -g > /var/www/DAMS-Content/USERDIRS.TXT; NOTES: this file expects to be /var/www/DAMS-Content/generate_userdirs.pl USAGE } (defined $$o{g} or defined $$o{a} or defined $$o{d} ) or (usage() and +exit); # are there cli opts? if ($$o{a} and $$o{d}){ print STDERR "ERROR: can't tell me to add AND delete at the same t +ime\n"; usage(); exit; } if ($$o{l} and !$$o{a}){ print STDERR "ERROR: can't just give me a label (-l $$o{l})\n"; usage(); exit; } #load conf my $C = { pwd=>'/var/www/DAMS-Content', HEADER=>'', BODY=>'', count=>1 }; conf2hash( file=>"$$C{pwd}/header.conf", hashref=>$C ); # get the labeled ones, users inside my $U= {}; conf2hash( file=>"$$C{pwd}/labeled.conf", hashref=>$U ); ################################################ # now labeled.conf changes may happen #{{{ if (defined $$o{a}){ $$o{a} or die ("what about content for what we are adding? $!"); -d "$$C{pwd}/$$o{a}" or (print STDERR "ERROR: [$$C{pwd}/$$o{a}] d +oes NOT exist as a dir\n" and usage() and exit); unless (defined $$o{l}){ $$o{l} = label_from_dir($$o{a}); } $$U{$$o{a}}=$$o{l}; hash2conf(file=>"$$C{pwd}/labeled.conf", hashref=> $U); } if (defined $$o{d}){ $$o{d} or (print STDERR "ERROR: what about content for what we are + deleting?\n $!\n" and usage() and exit); delete $$U{$$o{d}}; hash2conf(file=>"$$C{pwd}/labeled.conf", hashref=> $U); } #}}} # this is 0 only if cli was -g 0 defined $$o{g} or exit; # get the client dirs my $cdi ="$$C{pwd}/Clients"; my @d = sort (split(/\n/, `find $cdi -type d -name incoming -follow`)) +; # START BODY #####################################################3 $$C{BODY} = "[Users]\n"; for (keys %$U){ $$C{BODY}.= line($C,$_,$$U{$_}); } for (@d){ my $dir = $_; $dir=~s/^$$C{pwd}\///; $$C{BODY}.= line($C,$dir); } # START HEADER ####################### $$C{HEADER}="[PreferredServer]\n"; $$C{HEADER}.="Server=".$$C{Server}."\n"; $$C{HEADER}.="[RoutingID]\n"; $$C{HEADER}.="NextID=$$C{count}\n"; print $$C{HEADER}; print $$C{BODY}; exit; # usage : line($C,'userfiles/this/incoming','optlabel'); sub line { #{{{ my ($C,$dir,$label)=@_; $C or die($!); $dir or die($!); unless ($label){ $label = label_from_dir($dir); } $dir=~s/\//\\/g; my $line = "$label=$dir,$label,Dyer05,$$C{count},0\n"; $$C{count}++; return $line; } #}}} sub label_from_dir { my $dir = shift; my $label = $dir; $label=~s/\/incoming$//; $label=~s/^.+\/+//; return $label; }

Edited by planetscape - deleted excessive whitespace

( keep:2 edit:10 reap:0 )


In reply to how should a cli script return error codes? by leocharre

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.