in reply to Re: Getopt::Long subroutine usage
in thread Getopt::Long subroutine usage
#!/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
2005-01-05 Janitored by Arunbear - added readmore tags, as per Monastery guidelines
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Getopt::Long subroutine usage
by graff (Chancellor) on Jan 05, 2005 at 06:02 UTC | |
by rockneybot (Novice) on Jan 05, 2005 at 08:21 UTC | |
|
Re^3: Getopt::Long subroutine usage
by Solo (Deacon) on Jan 05, 2005 at 15:40 UTC | |
by rockneybot (Novice) on Jan 05, 2005 at 19:41 UTC |