FAQ
-----BEGIN PGP SIGNED MESSAGE-----

Hello all,
I'm writing a common gateway interface that will call I need to
issue several shell commands.
(cp, cd, and cd .., and maybe others). Any assistance would be
greatly appreciated. How can a call a shell other than bash.

Thank you,
Mark-Nathaniel Weisman A+, MCP, CNA, MOUS MI
Network Systems Administrator
Career Academy
Anchorage, AK

Search Discussions

  • Gary Hawkins at Dec 23, 2001 at 11:22 am
    With tcsh I just use backticks (below the tilde) because it's simple and
    reliable and no worry about resources and they are not repetitive tasks and I
    don't care if Santa brings me anything. So:

    `cp thisfile thatfile`;

    ....bing, done.

    Cron for example, a scheduled task, can start the script and then for some
    ungodly reason doesn't seem to know where it is, probably defaulting back to my
    user root, so I wind up with a first line like:

    `cd /u/ghawk/the/location/of/the/script/you/just/ran/dummy`;

    But there are those who would scold me for it, and probably will be some with a
    fine recommendation or two about the more proper ways to do things.

    Till then, backtick to your heart's content.

    /g
    -----Original Message-----
    From: Mark-Nathaniel Weisman
    Sent: Saturday, December 22, 2001 10:32 PM
    To: beginners@perl.org
    Subject: Calling a shell from a perl script.


    -----BEGIN PGP SIGNED MESSAGE-----

    Hello all,
    I'm writing a common gateway interface that will call I need to
    issue several shell commands.
    (cp, cd, and cd .., and maybe others). Any assistance would be
    greatly appreciated. How can a call a shell other than bash.
  • Peter Cornelius at Dec 23, 2001 at 3:28 pm
    There are several methods for executing external commands from Perl. The 2
    most common would be 'system()' and backticks '``'. You should probably
    read the 'perldoc -f system' for detailed info. The main difference between
    backticks and system is where the commands output goes and where the return
    value goes. With system the output is not redirected for you and the return
    code goes to $!. With backticks the output is returned to the program and
    the return code goes to $?.

    You should probably try 'perldoc -q backtic' to get more info on this whole
    topic. perlfaq8 has this to say.

    What's wrong with using backticks in a void context?

    Strictly speaking, nothing. Stylistically speaking, it's
    not a good way to write maintainable code because
    backticks have a (potentially humongous) return value, and
    you're ignoring it. It's may also not be very efficient,
    because you have to read in all the lines of output,
    allocate memory for them, and then throw it away. Too
    often people are lulled to writing:

    `cp file file.bak`;

    And now they think "Hey, I'll just always use backticks to
    run programs." Bad idea: backticks are for capturing a
    program's output; the system() function is for running
    programs.

    There's more but I wont repost it here.

    All this being said, you should probably look into how perl could do most of
    these things for you without using the shell. The 'cd' shell command is
    built in with 'chdir'. perldoc -f chdir reads

    chdir EXPR
    Changes the working directory to EXPR,

    and the cp stuff is in a module called File::Copy
    This is a standard module that you can get at by putting
    use File::Copy;

    at the top of your script. Then somewhere you can
    copy ("source", "dest") or die "with a useful message: $!\n";

    Guess what I'm going to say next? perldoc File::Find will tell you more.

    Good luck,
    Peter C.

    --SNIP--
    Hello all,
    I'm writing a common gateway interface that will call I need to
    issue several shell commands.
    (cp, cd, and cd .., and maybe others). Any assistance would be
    greatly appreciated. How can a call a shell other than bash.
  • Jim Conner at Dec 23, 2001 at 11:56 pm
    As personal preference I do try hard not to use system() calls in my Perl
    scripts because Perl can do everything itself with built-ins or
    modules. It's my opinion that while easier in terms of coding a lot of the
    time using system() calls or back-tics it is harder, albeit most likely not
    too much, on the host machine to call outside programs to perform certain
    functions. So, I avoid doing so as often as I can.

    Therefore, use the built-ins. To do file manipulations use File::Copy
    which gives the ability to move(), copy() files. Use Cwd.pm to get current
    working directory (getcwd()), and use use chdir() to change directory. You
    can use perldoc File::Copy, perldoc -f chdir, and perldoc Cwd to get
    documentation on these functions.

    So, I agree with Peter in his last couple of paragraphs and put my 2 cents
    in that imho, it is better to use perl built-ins as much as possible.

    - Jim
    At 07:30 12.23.2001 -0800, Peter Cornelius wrote:
    There are several methods for executing external commands from Perl. The 2
    most common would be 'system()' and backticks '``'. You should probably
    read the 'perldoc -f system' for detailed info. The main difference between
    backticks and system is where the commands output goes and where the return
    value goes. With system the output is not redirected for you and the return
    code goes to $!. With backticks the output is returned to the program and
    the return code goes to $?.

    You should probably try 'perldoc -q backtic' to get more info on this whole
    topic. perlfaq8 has this to say.

    What's wrong with using backticks in a void context?

    Strictly speaking, nothing. Stylistically speaking, it's
    not a good way to write maintainable code because
    backticks have a (potentially humongous) return value, and
    you're ignoring it. It's may also not be very efficient,
    because you have to read in all the lines of output,
    allocate memory for them, and then throw it away. Too
    often people are lulled to writing:

    `cp file file.bak`;

    And now they think "Hey, I'll just always use backticks to
    run programs." Bad idea: backticks are for capturing a
    program's output; the system() function is for running
    programs.

    There's more but I wont repost it here.

    All this being said, you should probably look into how perl could do most of
    these things for you without using the shell. The 'cd' shell command is
    built in with 'chdir'. perldoc -f chdir reads

    chdir EXPR
    Changes the working directory to EXPR,

    and the cp stuff is in a module called File::Copy
    This is a standard module that you can get at by putting
    use File::Copy;

    at the top of your script. Then somewhere you can
    copy ("source", "dest") or die "with a useful message: $!\n";

    Guess what I'm going to say next? perldoc File::Find will tell you more.

    Good luck,
    Peter C.

    --SNIP--
    Hello all,
    I'm writing a common gateway interface that will call I need to
    issue several shell commands.
    (cp, cd, and cd .., and maybe others). Any assistance would be
    greatly appreciated. How can a call a shell other than bash.


    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org

    - Jim

    Philosophy is for those who have nothing better to do than wonder
    why philosophy is for those who have nothing better to do than...
  • Michael Fowler at Dec 26, 2001 at 7:05 pm

    On Sun, Dec 23, 2001 at 07:30:21AM -0800, Peter Cornelius wrote:
    With system the output is not redirected for you and the return code goes
    to $!.
    Minor correction: system returns the exit status, and stores that status in
    $?, not $!. $! will contain the reason for a problem if $? or the return
    value is -1.

    use File::Copy;

    at the top of your script. Then somewhere you can
    copy ("source", "dest") or die "with a useful message: $!\n";

    Guess what I'm going to say next? perldoc File::Find will tell you more.
    Or perldoc File::Copy. :)


    Michael
    --
    Administrator www.shoebox.net
    Programmer, System Administrator www.gallanttech.com
    --
  • Lance Prais at Dec 26, 2001 at 8:25 pm
    I am using the following statment to try to print a sub string of a line
    but
    print (substr($line, 42, 7));

    or

    print substr($line, 42, 7);

    but getting the following error:

    syntax error at emailagent line 16, near ");"


    Can anyone tell me what I am doing wrong?

    Thank you
    Lance
  • Michael Fowler at Dec 26, 2001 at 9:55 pm
    On Wed, Dec 26, 2001 at 02:24:16PM -0800, Lance Prais wrote:
    [snip]
    print (substr($line, 42, 7)); [snip]
    print substr($line, 42, 7);

    but getting the following error:

    syntax error at emailagent line 16, near ");"
    The code snippets you've shown us would not result in that error message.
    You have a problem elsewhere in your code; perhaps you forgot a semicolon,
    close quote, or close brace.


    Michael
    --
    Administrator www.shoebox.net
    Programmer, System Administrator www.gallanttech.com
    --
  • Lance Prais at Dec 26, 2001 at 10:17 pm
    Am I understanding this correctly that if I put in the following code the
    result will be it will start reading on row 23?

    For the print output I am getting the following.
    line: (substr(1, 42, 7)

    Which leads me to believe that it is starting on line 1.

    Can anyone see what I am doing wrong?

    for(my $i=0; $i<22; $i++){<EMAILMGR>}; #This will put you at row 23.----
    $_=<EMAILMGR>;
    my $line=$_;
    my $nextline=$line++;
    if (substr($line, 42, 7) eq "Running")
    {
    print STDERR "\nline: (substr($line, 42, 7)";
    }
    Else
    {
    print STDERR "\nline: (substr($line, 42, 7)";
    }
  • John W. Krahn at Dec 26, 2001 at 10:59 pm

    Lance Prais wrote:

    Am I understanding this correctly that if I put in the following code the
    result will be it will start reading on row 23?

    For the print output I am getting the following.
    line: (substr(1, 42, 7)

    Which leads me to believe that it is starting on line 1.

    Can anyone see what I am doing wrong?

    for(my $i=0; $i<22; $i++){<EMAILMGR>}; #This will put you at row 23.----
    $_=<EMAILMGR>;
    my $line=$_;
    my $nextline=$line++;
    $ perl -le' $l = "This is a line of text"; $n = $l++; print "$l : $n" '
    1 : This is a line of text

    if (substr($line, 42, 7) eq "Running")
    {
    print STDERR "\nline: (substr($line, 42, 7)";
    }
    Else
    {
    print STDERR "\nline: (substr($line, 42, 7)";
    }


    John
    --
    use Perl;
    program
    fulfillment
  • Bob Showalter at Dec 26, 2001 at 8:37 pm

    -----Original Message-----
    From: Lance Prais
    Sent: Wednesday, December 26, 2001 5:24 PM
    To: beginners@perl.org
    Subject: Using print


    I am using the following statment to try to print a sub
    string of a line
    but
    print (substr($line, 42, 7));

    or

    print substr($line, 42, 7);

    but getting the following error:

    syntax error at emailagent line 16, near ");"


    Can anyone tell me what I am doing wrong?
    Neither of those lines is incorrect by itself, so you
    need to check the line above or post more of your script.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedDec 23, '01 at 6:43a
activeDec 26, '01 at 10:59p
posts10
users8
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase