Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Unsuccessfully passing arguments to a bash command

by zapatafa (Initiate)
on Jun 15, 2013 at 19:55 UTC ( #1039146=perlquestion: print w/ replies, xml ) Need Help??
zapatafa has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to pass arguments to a function defined in .bashrc. I don't have any control over the contents of the .bashrc.

For the sake of argument: In .bashrc there is a function defined along the lines of:

function untouchable { perl ./untouchable.pl $* } export -f untouchable
where the script untouchable.pl is also not something I have any control over:
#!/usr/bin/perl use strict 'vars'; my @people = @ARGV; print "Staring...\n"; foreach my $person ( @people ){ print "Hello $person\n"; } print "done!\n";

I'm trying to make a system call to this function from within a perl script as follows:

#!/usr/bin/perl system ("bash -ic untouchable john mike bill");
But my arguments john mike bill are completely lost.

I know I could make a call directly to the untouchable.pl in this particular scenario here. But, I'm actually interested in how to make an interactive call to the bash command.

If I run untouchable from the command line, it works as expected. How do I write a perl script to make a system call to untouchable and get this call to also pass my arguments along?

Comment on Unsuccessfully passing arguments to a bash command
Select or Download Code
Re: Unsuccessfully passing arguments to a bash command
by flexvault (Parson) on Jun 15, 2013 at 20:14 UTC

    Welcome zapatafa,

    Most times when you call a shell, the problem is in the special characters of Perl and of the shell. Try doing this:

    my $cmd = qq|[Path]bash -ic ...|; print "$cmd\n"; system($cmd);
    Then look for any differences from your command line call. Replace Path with the full path name to get to 'bash'.

    Update: Happy-the-monk pointed out my error, thanks.

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin

      I've tried this. But even if I write
      #!/usr/bin/perl my $cmd = qq|/bin/bash -ic untouchable john mike bill|; print $cmd; system ($cmd);
      The output is
      /bin/bash -ic untouchable john mike billStarting... done!
      Somehow the arguments john mike bill are getting lost.

        Where john mike bill are what?

Re: Unsuccessfully passing arguments to a bash command
by Happy-the-monk (Monsignor) on Jun 15, 2013 at 20:27 UTC

    I am a bit wary about the calling of a shell/bash function and possible gotchas,
    ...but I also think you should try passing parameters to system as a list or an array.

    See if that gets you there.

    Cheers, Sören

    (hooked on the Perl Programming language)

Re: Unsuccessfully passing arguments to a bash command
by Happy-the-monk (Monsignor) on Jun 15, 2013 at 21:17 UTC

    this does it for me:

    perl -e'print qx(bash -ic "untouchable john mike bill");'

    So it was about the right quoting to protect it from the shell.

    Cheers, Sören

    (hooked on the Perl Programming language)

      Works perfectly. Thank you.
Re: Unsuccessfully passing arguments to a bash command
by poj (Priest) on Jun 15, 2013 at 21:24 UTC
    Try ;
    #!/usr/bin/perl my @args = ("bash","-ic","untouchable john mike bill"); system(@args);
    poj
Re: Unsuccessfully passing arguments to a bash command
by ikegami (Pope) on Jun 16, 2013 at 05:02 UTC
    $* reflects the args passed to the function, but you didn't pass any! You merely passed some to bash as you executed the bash command untouchable. You want to execute the bash command untouchable john mike bill, which you can do using
    system(q{bash -ic 'untouchable john mike bill'});
    or better yet,
    system('bash', '-ic', 'untouchable john mike bill');

    The latter launches bash directly instead of launching a sh to launch bash.

    If the names are variable, you want:

    use String::ShellQuote qw( shell_quote ); system('bash', '-ic', shell_quote('untouchable', @names));
    or
    system('bash', '-ic', 'untouchable "$@"', '-', @names);

    If I run untouchable from the command line, it works as expected.

    But running the command you passed to system wouldn't have.

Re: Unsuccessfully passing arguments to a bash command
by choroba (Abbot) on Jun 16, 2013 at 09:00 UTC
    Bash comment:
    It is better to use
    perl ./untouchalbe.pl "$@"

    That way, you can correctly process arguments containing whitespace.

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      "$*" in this case.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1039146]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2014-10-31 22:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (225 votes), past polls