Statement to do something if <> calls $remove.
if ($remove) {
$remove = lc($remove);
my $removeq = $remove;
my %remove_cmd = ( remove => "rmquedev -d $removeq -q $removeq" );
print "Are you sure you want to remove print queue \"$removeq\" [Y
+/n]?";
$_ = <>;
chomp($_);
$_ = "Y" if ( length($_) == 0 );
if ( $_ =~ /[Yy]/ ) {
# Wait for user-input
system( $remove_cmd {'remove'} );
print
"Prepairing to remove \"$removeq\" from the CC_print.printers file.\n"
+;
sleep 2;
&clean_the_file();
}
else {
die "You chose NOT to remove queue \"$removeq\"\n";
}
}
Sub-process being called
sub clean_the_file {
my $sourcefile = "/u/ccsys/CC_print.printers";
# Keep a dedicated backup of CC_print.printers
if ( !-t "/root/CC_print.printers.bkp" ) {
system("touch /root/CC_print.printers.bkp");
system("cp -R $sourcefile /root/CC_print.printer.bkp");
}
if ( -t "/root/CC_print.printers.bkp" ) {
system("cp -R $sourcefile /root/CC_print.printer.bkp");
# grab the sourcefile and the string to remove as the first and second
+ arguments
my ( $sourcefile, $removeq ) = @_;
# create a temporary file
system("touch /u/ccsys/CC_print.printers.bkp_mkpq");
my $tempfile = "/u/ccsys/CC_print.printers.bkp_mkpq";
# extract lines from $sourcefile which do NOT have the string to
+ remove
# note that the \' inserts ticks so that strings with spaces can
+ work, too
`grep -v $removeq $sourcefile > $tempfile`;
sleep 1;
print "Waiting to unlink..\n";
# delete source file, then rename working file
unlink($sourcefile);
rename( $tempfile, $sourcefile );
system("rm -rf $tempfile");
}
}
Program is dying as the sub(); is being called with nothing telling me why!
I'm using Strict and Warnings, could anyone offer some insight?
UPDATE!!
The data I was trying to parse looks like this
#CC:'queue-01':ps|2|-5|1.0|1.0:tp'
#CC:'queue-02':ps|2|-5|1.0|1.0:tp'
#CC:'queue-03':ps|2|-5|1.0|1.0:tp'
#CC:'queue-04':ps|2|-5|1.0|1.0:tp'
#CC:'queue-05':ps|2|-5|1.0|1.0:tp'
#CC:'queue-06':ps|2|-5|1.0|1.0:tp'
#CC:'queue-07':ps|2|-5|1.0|1.0:tp'
#CC:'queue-08':ps|2|-5|1.0|1.0:tp'
#CC:'queue-09':ps|2|-5|1.0|1.0:tp'
I have changed my code and now I can execute which printer needs to be deleted from the file successfully, while maintaining a dedicated backup of the file before it was changed :).
sub cleanthefile {
my @filehandles;
# Keep varibles in same order as they were passed to the sub-routi
+ne
my ( $removeq, $sourcefile, $tempfile, $backup ) = @_;
# Keep a dedicated backup of CC_print.printers
if (-e "$_[3]" ) {
system ( "cp -R $_[1] $_[3]" );
# create a temporary file
if ( !-e "$_[1]") {
system("touch $_[2]");
}
# extract lines from $sourcefile which do NOT have the string to
+ remove
# note that the \' inserts ticks so that strings with spaces can
+ work, too
if ( -e "$_[1]") {
# system ("cat $sourcefile \| grep -v $removeq $sourcefile > $
+tempfile");
system ( "grep -v $_[0] $_[1] > $_[2]" );
sleep 1;
print "Waiting to unlink..\n";
# delete source file, then rename working file
unlink($_[1]);
rename( $_[2], $_[1] );
system("rm -rf $_[2]");
}
}
else {
system("touch $_[3]");
system("cp -R $_[1] $_[3]");
}
}
Command that was run
mkpq -r queue-03
As you can see, the queue-03 is gone!
#CC:'queue-01':ps|2|-5|1.0|1.0:tp'
#CC:'queue-02':ps|2|-5|1.0|1.0:tp'
#CC:'queue-04':ps|2|-5|1.0|1.0:tp'
#CC:'queue-05':ps|2|-5|1.0|1.0:tp'
#CC:'queue-06':ps|2|-5|1.0|1.0:tp'
#CC:'queue-07':ps|2|-5|1.0|1.0:tp'
#CC:'queue-08':ps|2|-5|1.0|1.0:tp'
#CC:'queue-09':ps|2|-5|1.0|1.0:tp'
Thanks again everyone!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.