http://www.perlmonks.org?node_id=1004931

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Shell script with PERL
by tobyink (Canon) on Nov 21, 2012 at 14:45 UTC

    That's quite a vague question. Perhaps you're looking for the system function.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Shell script with PERL
by blue_cowdawg (Monsignor) on Nov 21, 2012 at 15:11 UTC
        can any one suggest that how to use shell script with perl?

    My first reaction as a Perl instructor is "don't." Unless you are pressed for time and the script is overly complex (bad idea in the first place) you should try translating the script's functionality to Perl.

    What you've failed to communicate is what kind of script it is. Is it a Winbloze ".BAT" file? Is it a *nix shell script? (I've heard winbloze admins refer to BAT files as shell scripts.)

    If you are talking about *nix shell scripts then most of the functionality you can obtain within a Bash, KSH, CSH or whatever script is available within Perl plus some that isn't.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Shell script with PERL
by blackle (Beadle) on Nov 21, 2012 at 15:08 UTC

    That really depends on what you mean by using a shell script with perl. If you want to launch a perl script from a shell script, simply use the syntax perl path/to/script.pl arg1 arg2 arg3 etc. If you want to launch a shell script within perl, there are a few options. The first one to look at is system as tobyink mentioned. This function returns the exit status of the shell script, which is useful if all you need to know is if the script failed or not.

    If you instead want to capture the standard output of the script (i.e. the ouput one would see when running it in a terminal) you want to use backticks or qx//. The syntax for this is as follows:

    my $stdout_one = `sh shellscript.sh`; my $stdout_two = qx/sh shellscript.sh/; print "This never happens" if ($stdout_one ne $stdout_two); print "This always happens" if ($stdout_one eq $stdout_two);

    Be advised that running shell commands directly introduces a security risk. Suppose you want to run your script with an argument from a variable, you might write my $out = `sh shellscript.sh $param1`. If for any reason $param1 = "1; rm -rf *" your perl script will start deleting everything it can.

Re: Shell script with PERL
by Yates (Beadle) on Nov 21, 2012 at 14:49 UTC
    You can use the system function or backticks (`STRING`). Using backticks will capture the output, whereas output from a system call will go to STDOUT/STDERR.
Re: Shell script with PERL
by mbethke (Hermit) on Nov 21, 2012 at 15:02 UTC
    Look for "backticks" in perlop.
Re: Shell script with PERL
by space_monk (Chaplain) on Nov 21, 2012 at 17:10 UTC

    In order to use a shell script with perl, you can either use backticks to issue the command, or use the system command.

    However, the first question would be 'why?', as normally Perl is capable of anything that can be done in a normal shell script.

    A Monk aims to give answers to those who have none, and to learn from those who know more.