Well, there is some cool stuff going on there, and thanks for the lightning response time, but I guess I was looking for a more indepth example. Perhaps the functionality I'm looking for in the module just isn't available and will be up to me to code. I'll try to outline the problem I'm encountering. Here's the script I'm working on currently. Please don't laugh. I'm new.

#!/usr/bin/perl -w use strict; use Getopt::Long; use Pod::Usage; my $autofs_conf = '/etc/auto.disks'; my $samba_conf = '/etc/samba/smb.conf'; my $device = ''; my $label = ''; my $prep = 0; my $configure = 0; my $reload = 0; my $makedirs = 0; Getopt::Long::Configure ("bundling", "permute", "auto_help", "auto_ver +sion"); GetOptions( 'autofs_conf|a=s' => \$autofs_conf, 'samba_conf|s=s' => \$samba_conf, 'configure|c' => \$configure, 'device|d=s' => \$device, 'label|l=s' => \$label, 'prep|p' => \$prep, 'reload|r' => \$reload, 'makedirs|m' => \$makedirs ); if ($prep) { if ($device && $label) { prep_disk($device, $label); } else { pod2usage(1); } } if ($configure) { if ($label) { make_conf(); } else { pod2usage(1); } } if ($makedirs) { if ($prep && $configure && $reload) { make_dirs(); } else { pod2usage(1); } } sub prep_disk { # saftey checks my ($device, $label) = @_; print "does $device exist? ... "; if (-e $device) { print "yes.\n"; } else { print "$device does not exist. exiting.\n"; exit } print "is $device a block device? ... "; if (-b $device) { print "yes.\n"; } else { print "no. you must use a block device. exiting.\n"; exit } print "checking for partition table ... "; unless ( `sfdisk -V -q $device` ) { $_ = `xfs_admin -l ${device}1`; /\".*/; print "$device contains a partition table and a filesystem labeled + $&. are you sure you want to continue? "; if ( <STDIN> =~ /n|no/i ) { exit; } } else { print "no partition table found.\n"; } # clean the disk print "cleaning the disk ...\n"; !system "dd", "if=/dev/zero", "of=$device", "bs=512", "count=1" or d +ie "couldn't clean disk\n"; print "done.\n"; # partition the disk print "partitioning the disk ...\n"; !system "/sbin/sfdisk -uM -q -L -O disksave --no-reread $device <<EO +F\n0,\nEOF" or die "couldn't partition the disk\n"; # create the filesystem print "creating the filesystem ... \n"; open DISKS, "/etc/auto.disks"; if ( grep( /$label/, <DISKS> ) ) { print "entry for $label exists. choose a different label. exiting. +\n"; close DISKS; exit; } else { !system "mkfs.xfs -f -L $label ${device}1" or die "couldn't create + filesystem\n"; print "done.\n"; } } sub make_conf { # add an entry to autofs &concat_conf ($autofs_conf, "$label -fstype=xfs :LABEL=$label +\n", $label); # add an entry to /etc/samba/smb.conf &concat_conf ($samba_conf, "[$label]\n path = /disks/$label\n +", $label); if ($reload) { reload_conf("autofs", "smb"); } } sub concat_conf { # concatenate an a text file and optionally check it for an existing + keyword first my ($file, $text, $test) = @_; print "adding entry to $file ... "; open FILE, "$file"; if ( $test && grep( /$test/, <FILE> ) ) { print "entry exists, skipping.\n"; close FILE; } else { open FILE, ">>$file"; print FILE "$text"; close FILE; print "done.\n"; } } sub remove_lines { my ($file, $search) = @_; my @lines; open INFILE, "<$file"; while (<INFILE>) { push @lines, $_ unless ($_ =~ /$search/i); } close INFILE; open OUTFILE, ">$file"; print OUTFILE @lines; close OUTFILE; } sub reload_conf { # reload samba and autofs print "reloading service configurations ...\n"; foreach (@_) { !system "service $_ restart" or die "couldn't restart $_\n"; } print "done.\n"; } sub make_dirs { # setup some dirs and chmod 'em print "creating dirs ... "; !system "ls /disks/$label" or die "couldn't list /disks/$label\n"; system "chmod -R 777 /disks/$label"; system "mkdir /disks/$label/Scans_In"; system "mkdir /disks/$label/Projects"; system "mkdir /disks/$label/Renders"; system "chmod -R 777 /disks/$label"; print "done.\n"; } sub get_line { print $_[0]; chomp (my $line = <STDIN>); $line; } __END__ =head1 NAME Prep Disk =head1 SYNOPSIS prep_disk [options] =head1 OPTIONS --configure|-c set for services configuration. used with -l --device|-d [device] block device name, such as /dev/sdb --help|-h this information --label|-l [label] xfs filesystem label --makedirs|-m set to create directory structure on target. d +epends on -c, -p, and -r --prep|-p set for disk preparation. used with -d and -l --reload|-r set to reload services =head1 DESCRIPTION B<This program> will read the given input file(s) and do someting useful with the contents thereof. =cut

The script works, but it's clunky. I wanted to be able to reference a subroutine with the --prep option for instance, but the --prep option uses two scalars --device and --label. Those two variables can be set on the command line. If I reference --prep as a subroutine instead of using my cludgey IF expressions, --disk and --label MUST preceed --prep on the command line. I was hoping to be able to require the dependent options within the getopt module, but I'm not sure if it's possible or what the syntax would be. I am guessing that it isn't possible or that I will have to think harder about what's going on here and learn more about programming in general. :-D

2005-01-05 Janitored by Arunbear - added readmore tags, as per Monastery guidelines


In reply to Re^2: Getopt::Long subroutine usage by rockneybot
in thread Getopt::Long subroutine usage by rockneybot

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.