sumandas has asked for the wisdom of the Perl Monks concerning the following question:

I am running script in linux environment but it is not passing the argument.I am calling the system command from one perl script(script1.pl) to call the second one(script2.pl) by passing argument

my $configfile=$ARGV[0]; system('path of script in linux/perl script2.pl',$configfile);
  • Comment on How to pass arguments in perl in linux system through system command
  • Download Code

Replies are listed 'Best First'.
Re: How to pass arguments in perl in linux system through system command
by McA (Priest) on Mar 14, 2014 at 06:30 UTC

    The way you coded it results in perl looking for a program with the name path of script in linux/perl script2.pl. I'm pretty sure you don't have such a program in the path. What you like is probably:

    system('perl', 'path of script in linux/script2.pl', $configfile);

    McA

Re: How to pass arguments in perl in linux system through system command
by vinoth.ree (Monsignor) on Mar 14, 2014 at 07:26 UTC
    Hi sumandas,

    system() accepts as argument either a scalar or an array. If the argument is a scalar, system() uses a shell to execute the command. If the argument is an array it executes the command directly, considering the first element of the array as the command name and the remaining array elements as arguments to the command to be executed.

    Its highly recommended for efficiency and safety reasons that you use an array to pass arguments to system call.

    Ex:
     system("command", "arg1", "arg2", "arg3");
    So,

    You can call your second perl script script2.pl with arguemnt as,

    system("path of your perl interpreter/perl","Path of/script2.pl","Path of /$configfile");

    If you have given the execution permission and your second script script2.pl has the shabang line ("#!/usr/bin/perl"), so u can simply execute your script as below,

    system("Path of/script2.pl","Path of /$configfile");

    The return value is set in $?. This value is the exit status of the command as returned by the 'wait' call, to get the real exit status of the command you have to shift right by 8 the value of $? ($? >> 8).

    If the value of $? is -1, then the command failed to execute, in that case you may check the value of $! for the reason of the failure.


    All is well
Re: How to pass arguments in perl in linux system through system command
by kcott (Archbishop) on Mar 14, 2014 at 06:30 UTC

    G'day sumandas,

    You posted this in "Perl Monks Discussion" which was the wrong place. I've moved it here, "Seekers of Perl Wisdom", which is the right place. The top of each section has an explanation of what's appropriate to post in that section: please check this before posting. Full details of what to post where can be found in "PerlMonks Sections".

    Regarding your question, here's some things to check:

    • After my $configfile=$ARGV[0];, add print "$configfile\n"; — does this print the value that you expected $ARGV[0] to have?
    • From your command line does 'path of script in linux/perl' script2.pl whatever correctly pass whatever to script2.pl?
    • Assuming you've written "path of script in linux" purely as a placeholder, is "script" just a typo or did you really mean "perl", i.e. "path of perl in linux"?

    -- Ken