开发者

How can I obtain unmodified command line arguments to "wrap" another command line tool?

开发者 https://www.devze.com 2022-12-12 03:50 出处:网络
I want to write a script foo which simply calls bar with the exact same arguments it was called with, using Bash or Perl.

I want to write a script foo which simply calls bar with the exact same arguments it was called with, using Bash or Perl.

Now, a simply way to do this in perl would be

#!/bin/perl

my $args=join(' ', @ARGV);
开发者_如何学运维`bar $args`;

However, the values in ARGV have already been processed by the shell, thus if I call

 foo "I wonder, \"does this work\""

bar would get called like this

 bar I wonder "does this work"

How can I obtain the original command line so that I can simply pass it on verbatim?


You can't get the arguments to the perl executable before they were processed by the shell. You can, however, ensure that the shell doesn't process them again:

system('bar', @ARGV);

vs.

system("bar @ARGV");

See perldoc -f system:

If there is only one scalar argument, the argument is checked for shell metacharacters, and if there are any, the entire argument is passed to the system's command shell for parsing (this is "/bin/sh -c" on Unix platforms, but varies on other platforms). If there are no shell metacharacters in the argument, it is split into words and passed directly to "execvp", which is more efficient.

If you need to read the output of the program, use a process handle and pass the arguments in the same way (without interpolation):

open my $barhandle, '-|', 'bar', @ARGV or die "Can't call bar: $!";
while (my $line = <$barhandle>)
{
    # do something with $line...
}
close $barhandle;

There are lots of ways you can call out to a program in Perl; see What's the difference between Perl's backticks, system, and exec? for a good overview of the major options.


It's extremely simple. Check out the system(LIST) variant of system calls.

system('bar', @ARGV);

perldoc -f system


In bash:

exec bar "$@"

This preserves the spacing in the arguments. I/O redirection, though, is not an argument within the meaning of the term (but the I/O redirection done for the original invocation would persist to the invoked 'bar' command unless you change it).

0

精彩评论

暂无评论...
验证码 换一张
取 消