Re: $ARGV[0] breaks the script



On 12/14/2006 06:24 AM, Gregory Machin wrote:
hi
the script will not work if I use $ARGV[0] but works 100% if I hard
code the $input variable;
what have i missed ?

#!/usr/bin/perl

# if your prgram has the string "grep" in the name or in the path
# this program won't work.
use strict;
use warnings;

my $line;
my $input;

$input=$ARGV[0]; #$input="httpd";
chomp $input;
open PROS, "ps -ef|grep $input |";

while ($line = <PROS>){
unless ($line =~ m/grep/){
print "$input is running\n";
exit;
}
}

print "$input isn't running\n";
exec "/etc/init.d/$input restart &";

Many thanks


Perhaps it would be easier to keep the grepping outside of the ps command:

my $input = $ARGV[0];
my @cmds = grep /\Q$input\E/, qx{ ps -eo '%c' };
if (@cmds < 1) {
... do something ...
}



.



Relevant Pages

  • $ARGV[0] breaks the script
    ... the script will not work if I use $ARGVbut works 100% if I hard ... chomp $input; ... Gregory Machin ...
    (perl.beginners)
  • Re: How to find a word in wordlist
    ... > use strict; ... > exit 0; ... With your script, if i look for a word "z***" so the script ...
    (comp.lang.perl.misc)
  • Re: script does not always work the same each time.
    ... I have written a simple script to test code build and test run on a ... $lineterminationChar {append output $expect_out; ... exit 1;} ...
    (comp.lang.tcl)
  • Re: Changing a users password non-interactively?
    ... You need expect and a setpass.expect script which ill add ... exit with a nasty warning. ... # Be careful of the COMMAND and UNDOCMD - they are dependant on your ...
    (comp.unix.aix)
  • Re: Why must I chomp a variable set in a script?
    ... If they are of zero size, ... it is supposed to trigger the email program to notify the users. ... have to "chomp" the variable to get this to work? ... like the value was supplied from the command line, it is set in the script. ...
    (perl.beginners)