Need help running shell commands

Discussion in 'Perl Misc' started by jm, Jun 7, 2004.

  1. jm

    jm Guest

    I have my perl file set up as 4711 and am running it as root.

    I simply want to change the path via the perl script:

    I have tried system('/mypath'); and system "/mypath";



    When I set the path in the script, is it only set for the script
    itself (just for the script's usage)? and thus it wouldn't change the
    path of the person running the script? Thank you.
     
    jm, Jun 7, 2004
    #1
    1. Advertising

  2. jm

    Ben Morrow Guest

    Quoth (jm):
    > I have my perl file set up as 4711 and am running it as root.


    Why? It sounds like you don't know Perl anywhere near well enough yet to
    be writing scripts to run setid.

    > I simply want to change the path via the perl script:
    >
    > I have tried system('/mypath'); and system "/mypath";
    >
    > When I set the path in the script, is it only set for the script
    > itself (just for the script's usage)? and thus it wouldn't change the
    > path of the person running the script? Thank you.


    Please explain yourself a little better.

    Are you attempting to set $PATH in the invoking user's shell? This is a
    faq: perldoc -q environment

    If you're trying to do something else, then I'm afraid I don't
    understand you at all. Try posting a short complete program, and
    explaining what it does that you don't want it to do (or what it doesn't
    do that you do want).

    Ben

    --
    Razors pain you / Rivers are damp
    Acids stain you / And drugs cause cramp. [Dorothy Parker]
    Guns aren't lawful / Nooses give
    Gas smells awful / You might as well live.
     
    Ben Morrow, Jun 7, 2004
    #2
    1. Advertising

  3. jm

    Bob Walton Guest

    jm wrote:

    > I have my perl file set up as 4711 and am running it as root.
    >
    > I simply want to change the path via the perl script:
    >
    > I have tried system('/mypath'); and system "/mypath";
    >
    >
    >
    > When I set the path in the script, is it only set for the script
    > itself



    Nope. Not even for the script itself (by using system()). Only for the
    process kicked off by system() -- which, incidently, won't do much other
    than error out if it is as shown, unless you happen to have an
    executable program or script in your root directory named 'mypath'.
    Maybe you meant system('cd /mypath'); ?

    > (just for the script's usage)? and thus it wouldn't change the
    > path of the person running the script? Thank you.


    Even if you changed the directory in the process in

    which your Perl interpreter is running your script
    (which can be done using a module whose name I can't recall
    -- maybe File::chdir ?), you *still* wouldn't change the
    directory for the user, script, or whatever that fired
    off Perl's process. Read a Unix primer -- a subprocess
    is pretty well insulated from its parent process -- the
    subprocess cannot make any change to the parent's
    environment. That's a feature, not a bug. See:

    perldoc -q 'changed directory'


    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Jun 7, 2004
    #3
  4. jm

    Ben Morrow Guest

    Quoth d:
    >
    > Even if you changed the directory in the process in
    > which your Perl interpreter is running your script
    > (which can be done using a module whose name I can't recall
    > -- maybe File::chdir ?)


    err, try perldoc -f chdir

    Ben

    --
    "The Earth is degenerating these days. Bribery and corruption abound.
    Children no longer mind their parents, every man wants to write a book,
    and it is evident that the end of the world is fast approaching."
    -Assyrian stone tablet, c.2800 BC
     
    Ben Morrow, Jun 7, 2004
    #4
  5. Ben Morrow wrote:

    >
    > Quoth (jm):
    >> I have my perl file set up as 4711 and am running it as root.

    >


    > Please explain yourself a little better.


    I dont have a clue what he wants. 4711 is a brand of aftershave.
    http://www.fragrancewholesale.com/471127ozedc1.html

    gtoomey
     
    Gregory Toomey, Jun 8, 2004
    #5
  6. jm wrote:
    > I have my perl file set up as 4711 and am running it as root.
    >
    > I simply want to change the path via the perl script:


    You are not very clear about you are trying to do at all.
    But I am guessing you are not talking about the path at all but you are
    talking about the current working directory aka CWD.

    > I have tried system('/mypath'); and system "/mypath";


    Assuming your shell has such a shortcut command, that if a directory name is
    entered instead of a command then it will change the CWD to that directory.
    Then still you are changing the CWD for the forked system process only, not
    for the process that is running the Perl script.

    > When I set the path in the script, is it only set for the script
    > itself (just for the script's usage)?


    Using system() you are not changing anything for the script itself, but at
    best for the process that is being forked to execute the command(s) listed
    in the system() call.
    If you want to chagne the cwd of the Perl process then use the chdir()
    command.

    > and thus it wouldn't change the
    > path of the person running the script?


    Child process cannot modify the environment (including the CWD) of the
    parent process. Please see "perldoc -q environment".

    jue
     
    Jürgen Exner, Jun 8, 2004
    #6
  7. jm <> wrote:

    > When I set the path in the script, is it only set for the script
    > itself (just for the script's usage)? and thus it wouldn't change the
    > path of the person running the script?



    Right.

    That is how it is _supposed_ to be.

    A child process (perl) cannot affect the environment of its parent (sh).


    See this Perl FAQ:

    I {changed directory, modified my environment} in a perl script.
    How come the change disappeared when I exited the script?
    How do I get my changes to be visible?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 8, 2004
    #7
  8. jm

    jm Guest

    Ben Morrow <> wrote in message news:<ca2nvn$a9f$>...
    > Quoth (jm):
    > > I have my perl file set up as 4711 and am running it as root.

    >
    > Why? It sounds like you don't know Perl anywhere near well enough yet to
    > be writing scripts to run setid.
    >
    > > I simply want to change the path via the perl script:
    > >
    > > I have tried system('/mypath'); and system "/mypath";
    > >
    > > When I set the path in the script, is it only set for the script
    > > itself (just for the script's usage)? and thus it wouldn't change the
    > > path of the person running the script? Thank you.

    >
    > Please explain yourself a little better.
    >
    > Are you attempting to set $PATH in the invoking user's shell? This is a
    > faq: perldoc -q environment
    >
    > If you're trying to do something else, then I'm afraid I don't
    > understand you at all. Try posting a short complete program, and
    > explaining what it does that you don't want it to do (or what it doesn't
    > do that you do want).
    >
    > Ben



    Sorry, I was in a hurry. What I meant was:

    system ('export PATH=/mypath:$PATH');
    system ('. /mypath/myenvironment some_argc');

    The problem is that I am using DBI and I have to be in a certain
    environment to use it. I can't just call DBI. I have to set several
    variables in the system (export myvar=myvalue) etc. and finally be in
    a production environment before I can rund the DBI stuff in the Perl
    script.

    I apologize for the crazy code above.
     
    jm, Jun 8, 2004
    #8
  9. jm

    Ben Morrow Guest

    Quoth (jm):
    >
    > Sorry, I was in a hurry.


    When in a hurry explaining yourself clearly will *definitely* result in
    quicker and more useful answers (if any) than being vague.

    > What I meant was:
    >
    > system ('export PATH=/mypath:$PATH');
    > system ('. /mypath/myenvironment some_argc');
    >
    > The problem is that I am using DBI and I have to be in a certain
    > environment to use it. I can't just call DBI. I have to set several
    > variables in the system (export myvar=myvalue) etc. and finally be in
    > a production environment before I can rund the DBI stuff in the Perl
    > script.


    Right, that can't be done, as it stands. system forks a shell; this
    shell cannot affect the working environment of the perl process.

    You can change your environment variables yourself using the %ENV hash,
    so for instance you can do

    $ENV{PATH} = "/mypath:$ENV{PATH}";

    .. You may find if this is for a DBD module that you need to do this in a
    BEGIN block: certainly, I've found that with DBD::Sybase before.

    Alternatively, if it is for some reason inconvenient to re-write the
    shell script setting up the environment in Perl, you can wrap the perl
    script in a shell script:

    Move 'script' to 'script.pl'
    Create a new file 'script' like

    #!/bin/sh

    export PATH="/mypath:$PATH"
    .. /mypath/myenvironment some_argc

    perl /path/to/script.pl

    A third way would be to do something like

    /(.*?)=(.*)$/ and $ENV{$1} = $2 for qx{
    export PATH=...
    . /mypath/...
    env
    };

    if you can be sure nothing in your environment has a newline in it.

    Ben

    --
    don't get my sympathy hanging out the 15th floor. you've changed the locks 3
    times, he still comes reeling though the door, and soon he'll get to you, teach
    you how to get to purest hell. you do it to yourself and that's what really
    hurts is you do it to yourself just you, you and noone else **
     
    Ben Morrow, Jun 8, 2004
    #9
  10. Purl Gurl <> wrote:

    > Changing directories, clearly, is not an environment variable
    > as you have indicated, not an environment variable in a
    > classic sense.



    There are components of the "environment" that are NOT "variables".

    The cwd is one such.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 8, 2004
    #10
  11. jm <> wrote:


    > Sorry, I was in a hurry.



    So you had hundreds or thousands of people trying to sort it all out?

    That is an inefficient distribution of labor, please try not to do
    that to us anymore.


    > What I meant was:
    >
    > system ('export PATH=/mypath:$PATH');



    $ENV{PATH} = "/mypath:$ENV{PATH}";


    > system ('. /mypath/myenvironment some_argc');



    "myenvironment" is in a child process, so it inherits the environment
    from its parent (perl), so it will have the modified path.


    > The problem is that I am using DBI and I have to be in a certain
    > environment to use it.



    That is not a problem.


    > I have to set several
    > variables in the system



    see the %ENV hash in:

    perldoc perlvar


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 8, 2004
    #11
  12. jm

    Joe Smith Guest

    Gregory Toomey wrote:

    > Ben Morrow wrote:
    >>Quoth (jm):
    >>>I have my perl file set up as 4711 and am running it as root.

    >>Please explain yourself a little better.

    >
    > I dont have a clue what he wants. 4711 is a brand of aftershave.


    You cut out the part where Ben shows that he knows exactly what it
    means:
    >> It sounds like you don't know Perl anywhere near well enough yet to
    >> be writing scripts to run setid.


    A script is made setUID=root via the unix command "chmod 4771 script.pl".
    It's a dangerous and ill-advised thing to do, especially for newbies.
    -JOe
     
    Joe Smith, Jun 8, 2004
    #12
  13. Gregory Toomey wrote:
    > Ben Morrow wrote:
    >
    >
    >>Quoth (jm):
    >>
    >>>I have my perl file set up as 4711 and am running it as root.


    > I dont have a clue what he wants. 4711 is a brand of aftershave.


    Not only an aftershave but a whole series of fragrances, body lotions,
    aftershaves etc.
    Named after the number of the company's house in the Glockengasse in
    Cologne (Germany). A specific product of 4711 is often referred to as
    "Eau de Cologne" (Water of Cologne), mainly used by old spinsters B-{).
    (No, I do not work for 4711).

    Besides: Setting any the permission triples of an interpreted script to
    1 is pretty senseless: the interpreter will be unable to read the
    script's code:
    Can't open perl script "./x.pl": Permission denied

    Josef
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Jun 8, 2004
    #13
  14. Purl Gurl <> wrote:
    > Tad McClellan wrote:
    >> Purl Gurl wrote:



    > You have forgotten you have me in your killfile.



    I was slumming.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 8, 2004
    #14
  15. jm

    Brad Baxter Guest

    On Tue, 8 Jun 2004, Purl Gurl wrote:

    > Tad McClellan wrote:
    >
    > > Purl Gurl wrote:
    > > > Tad McClellan wrote:
    > > >> Purl Gurl wrote:

    >
    > > > You have forgotten you have me in your killfile.

    >
    > > I was slumming.

    >
    > An activity much in keeping with the quality of your character.


    Now, now, Ms. Gurl, you know it takes one to know one.

    Regards,

    Brad
     
    Brad Baxter, Jun 9, 2004
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Allan Rojas

    Shell commands in ASP.NET?

    Allan Rojas, Jul 3, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    3,768
    dave wanta
    Jul 3, 2003
  2. George Lake

    shell commands via ASP

    George Lake, May 9, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    840
    George Lake
    May 9, 2006
  3. George Lake

    Re: shell commands via ASP

    George Lake, May 10, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,845
    Swanand Mokashi
    May 11, 2006
  4. Ben Pfaff

    Re: man pages for C commands (GCC commands)

    Ben Pfaff, Jun 24, 2003, in forum: C Programming
    Replies:
    4
    Views:
    4,009
    Thomas Stegen
    Jun 28, 2003
  5. Tim Stanka
    Replies:
    1
    Views:
    836
    Jeff Epler
    Aug 2, 2004
Loading...

Share This Page