Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Need help running shell commands

Reply
Thread Tools

Need help running shell commands

 
 
jm
Guest
Posts: n/a
 
      06-07-2004
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.
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      06-07-2004

Quoth http://www.velocityreviews.com/forums/(E-Mail Removed) (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. (E-Mail Removed)
 
Reply With Quote
 
 
 
 
Bob Walton
Guest
Posts: n/a
 
      06-07-2004
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

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      06-07-2004

Quoth (E-Mail Removed)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 (E-Mail Removed)
 
Reply With Quote
 
Gregory Toomey
Guest
Posts: n/a
 
      06-08-2004
Ben Morrow wrote:

>
> Quoth (E-Mail Removed) (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
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      06-08-2004
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


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      06-08-2004
jm <(E-Mail Removed)> 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
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
jm
Guest
Posts: n/a
 
      06-08-2004
Ben Morrow <(E-Mail Removed)> wrote in message news:<ca2nvn$a9f$(E-Mail Removed)>...
> Quoth (E-Mail Removed) (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.
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      06-08-2004

Quoth (E-Mail Removed) (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 ** (E-Mail Removed)
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      06-08-2004
Purl Gurl <(E-Mail Removed)> 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
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell commands via ASP George Lake ASP .Net 4 05-09-2006 07:14 PM
Shell Commands - Accounting (ACS 3.1) Aijaz Wani Cisco 0 10-03-2004 10:08 AM
Need Help Differentiating Bad Commands From Incomplete Commands Tim Stanka Python 1 08-02-2004 02:08 AM
Shell commands in ASP.NET? Allan Rojas ASP .Net 3 07-03-2003 05:29 PM
Re: man pages for C commands (GCC commands) Ben Pfaff C Programming 4 06-28-2003 06:21 PM



Advertisments