Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Using Perl 5.8.8 on Linux, need the output of a perl 'system' command to be hidden. The command in my code is:

system("wget", "$url", "-Omy_folder/$date-$target.html", "--user-agent=$useragent");

I've tried using > /dev/null 2>&1 in different places in the system command, like this:

system("wget", "$url", "-Omy_folder/$date-$target.html", "--user-agent=$useragent","> /dev/null 2>&1");

Can anyone help me with where the redirection to /dev/null should be?

share|improve this question
    
what about capture or capturex? –  osirisgothra Sep 25 at 23:14

1 Answer 1

When you call system() with more than one arg, you are telling perl to pass it directly to one of the exec*() calls, so it will not be called with a shell. The shell is the thing that understands file redirection. Try this:

system("wget $url -Omy_folder/$date-$target.html --user-agent=$useragent >/dev/null 2>&1");

Note that this is technically less secure than passing it straight to exec.

For quoting use String::ShellQuote, or just implement the logic yourself:

sub sq {
  my $str = shift;

  if (!$str) {
    $str = "''";
  } else {
    $str =~ s|'|'\\''|g;
    $str = "'$str'";
    $str =~ s|^''||;
    $str =~ s|''$||;
  }

  return($str);
}

$useragent = sq($useragent);
system("wget $url -Omy_folder/$date-$target.html --user-agent=$useragent >/dev/null 2>&1");

Also, to make this a little more sys-admin-y and a little less programmer-y, have you considered using the -q option to wget instead of jumping through the shell/file redirection hoops?

share|improve this answer
    
that would work, except the $useragent string contains '(' characters, I need to escape those somehow ... –  Chris Apr 1 '10 at 20:17
    
thanks, I'll try that! –  Chris Apr 1 '10 at 20:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.