PDA

View Full Version : Perl all command-line args (argv)



Felson
October 30th, 2008, 05:01 PM
I am trying to find a way to get "all" the arguments that were typed on the command-line when my script was executed, and I need them to be "exactly" as they were typed. That is, I want the single and double quotes, and the escaping exactly as it was. Anyone know of a way to get this?

snova
October 30th, 2008, 05:27 PM
It can't be done in any language. The shell performs the escaping, not the program. By the time it reaches Perl it's too late.

By the time it reaches exec() it's too late...

Felson
October 30th, 2008, 05:39 PM
Damn. I was afraid that was the answer... That makes writing a warper program a bit harder... :)

snova
October 30th, 2008, 05:57 PM
What's that?

Felson
October 30th, 2008, 06:04 PM
Needed to write a program to wrap tar. It's being executed on a webserver where I have no ssh/telnet access (GoDaddy). Couldn't use tar directly because it timesout to fast. Here is what I ended up doing instead.


#!/usr/bin/perl

$Test = 0;
$pid = fork();

if ($pid == 0)
{
if(!$Test)
{
open STDIN, ">>", "/dev/null";
open STDOUT, ">>", "/dev/null";
open STDERR, ">>", "/dev/null";
}

$args = "";
for($c = 0; $c < @ARGV; $c++)
{
$tmpStr = $ARGV[$c];
$tmpStr =~ s/"/\\"/g;
$args .= qq| "$tmpStr"|;
}

$command = qq~tar $args > /dev/null 2> /dev/null~; if($Test) {print "$command<br>\n";} else {`$command`;}
if(!$Test) {exit(0);}
}


Kinda simplistic, but it "should" work for anything I will though at it.