Please excuse my uber noob and sometimes redundant questions, however I am new to Perl so I have run into a few road blocks, specifically with getopt::std. I created an rsync perl script that will transfer gz files from a local destination to a remote destination using a key, a semaphore, etc. Anyway, what I am currently trying to do is to pass arguments to the command line using getopt::std that would allow to just drop flags if I wish to rsync data using different IP/host, key, username, etc. I have been told that this is possible but I don't quite understand how to implement that with getopt::std. I was going to go with ARGV, but my team would prefer I go with getopt. I have read up on getopt::std but to be honest, there isnt much covered on the module. That or I am not quite understanding how it works in a script if I don't have specify variables for the flags. Below is the script I am trying to implement getopt::std to. Any help towards the right direction would be greatly appreciated.

#!/usr/bin/perl -w ### set initial variables $user = '<USERNAME>'; $host = '<IP/HOSTNAME>'; $priv_key = '</KEY/DIR/FILE>'; $remote_dir = "<REMOTEDIR>"; $source_dir = '<SOURCEDIR>'; $log_dir = '<LOGDIR>'; ### set system commands/configs/flags my $rm_cmd = '/bin/rm -rf'; my $touch_cmd = '/bin/touch'; my $ssh_cmd = '/usr/bin/ssh -i'; my $DEBUG = 2; ### set rsync commands/flags my $rsync_cmd = '/usr/bin/rsync -Pav -e'; my $del_flag = "--remove-source-files"; ### set file names/extensions my $semaphore = "rsync_running.lock"; my $log_file = "rsync.output.log"; my $file_ext = "*.gz"; sub filesize { use File::Find; find( sub { -f and ( $size += -s ) }, $source_dir ); $size = sprintf( "%.02f", $size / 1024 / 1024 ); open STDOUT, ">> $log_dir/$log_file" or die "$0: open: $!"; open STDERR, ">>& STDOUT" or die "$0: dup: $!"; print "Directory '$source_dir' contains $size MB\n"; } sub getLoggingTime { ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = gmtime(time); $nice_timestamp = sprintf( "%04d%02d%02d %02d:%02d:%02d", $year + 1900, $mon + 1, $mday, $hour, $min, $sec ); return $nice_timestamp; } sub rsyncfiles { if ( -f "$source_dir/$semaphore" ) { print "Rsync running, skipping this session.\n" if ( $DEBUG > +2 ); } else { system("$touch_cmd $source_dir/$semaphore"); my $push_cmd = "$rsync_cmd \"$ssh_cmd $priv_key\" $del_flag $source_dir/$file_ext $us +er\@$host:$remote_dir"; print "Running: $push_cmd\n" if ( $DEBUG > 1 ); my $return = system($push_cmd); if ($return) { print "rsync failed"; } } system("$rm_cmd $source_dir/$semaphore"); } @timeData = gmtime(time); my $timestamp = getLoggingTime(); open STDOUT, ">> $log_dir/$log_file" or die "$0: open: $!"; open STDERR, ">>& STDOUT" or die "$0: dup: $!"; print "The script ran at $timestamp \n"; &filesize; &getLoggingTime; &rsyncfiles;

Thanks all!


In reply to getopt::std to pass arguments to command line help by hextor

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.