system("pwd"); <- yuck 
Author Message
 system("pwd"); <- yuck

Hi,

I decided I need to write some script that needs to know what the current
path is.  So I opened up the Camel book and Llama book but could not find
any info on obtaining the current path, only $ENV{'PATH'}, but that is not
what I need.

So I used $path = system("pwd");

but when I printe "$path\n"; I get some zero (0) at the end for some
reason, for example:

cobalt:/usr/local/src/www>run-me-first.pl
/usr/local/src/www
0
cobalt:/usr/local/src/www>

any idea why that 0 is there and how I could get rid of it ?  besides
chop($path);....

Thanks !

Otis



Sun, 04 Jan 1998 03:00:00 GMT  
 system("pwd"); <- yuck
I believe you can do


that should work.

sam

--
"It's hard to work in groups when you're omnipotent"

                        - Q



Sun, 04 Jan 1998 03:00:00 GMT  
 system("pwd"); <- yuck

Quote:

>So I used $path = system("pwd");

Whoops...Maybe you should use $path=`pwd`;

Quote:
>but when I printe "$path\n"; I get some zero (0) at the end for some
>reason, for example:

Yup, you should. Pretty good reason too...

Quote:
>cobalt:/usr/local/src/www>run-me-first.pl
>/usr/local/src/www

^^^^^^^^^^^^^^^^^^^
printed to standard out by the system call, your print statement didn't
do it.

Quote:
>0

^^
printed by your print statement.

Quote:
>any idea why that 0 is there and how I could get rid of it ?  besides
>chop($path);....

chop($path); will give path="";

You are slightly confused as to when to use system and when to use ``.
Use the system function when you want to run something in the system (all
output goes to standard out.) and want to return to the script. The return
value is the exit code.
Use `` when you want to run something in the system and stick the output
into a variable instead of standard out.

               --Alex

|-----------------------------------------------------------------------------

| Alex's Home Page: http://www.wwu.edu/~n9244414
| Barefoot Computing Home Page: http://www.wwu.edu/~n9244414/barefoot.html



Sun, 04 Jan 1998 03:00:00 GMT  
 system("pwd"); <- yuck

Quote:

> I decided I need to write some script that needs to know what the current
> path is.  So I opened up the Camel book and Llama book but could not find
> any info on obtaining the current path, only $ENV{'PATH'}, but that is not
> what I need.

Use $ENV{'PWD'} .

Quote:
> So I used $path = system("pwd");
> ...
> any idea why that 0 is there and how I could get rid of it ?

According to the `perlfunc' man page, the return value of `system' is
the exit status of the program.

Daniel Lewart



Sun, 04 Jan 1998 03:00:00 GMT  
 system("pwd"); <- yuck

Quote:

> > I decided I need to write some script that needs to know what the current
> > path is.  So I opened up the Camel book and Llama book but could not find
> > any info on obtaining the current path, only $ENV{'PATH'}, but that is not
> > what I need.

> Use $ENV{'PWD'} .

Well ...  not all shells export a PWD environment variable consisting
of the current working directory, so this is not totally portable.

Quote:
> > So I used $path = system("pwd");
> > ...
> > any idea why that 0 is there and how I could get rid of it ?

> According to the `perlfunc' man page, the return value of `system' is
> the exit status of the program.

If you want to use the pwd program, you must do it like this:

  $path = `pwd`;
  chomp $path;  # or in perl 4:  chop $path

I hope this helps.

--
Lloyd Zusman            01234567 <-- The world famous Indent-o-Meter.

   To get my PGP public key automatically mailed to you, please
   send me email with the following string as the subject or on a
   line by itself in the message (leave off the quotation marks):
                    "mail-request public-key"



Mon, 05 Jan 1998 03:00:00 GMT  
 system("pwd"); <- yuck

Quote:
>Hi,
>I decided I need to write some script that needs to know what the current
>path is.  So I opened up the Camel book and Llama book but could not find
>any info on obtaining the current path, only $ENV{'PATH'}, but that is not
>what I need.
>So I used $path = system("pwd");
>but when I printe "$path\n"; I get some zero (0) at the end for some
>reason, for example:
>cobalt:/usr/local/src/www>run-me-first.pl
>/usr/local/src/www
>0
>cobalt:/usr/local/src/www>
>any idea why that 0 is there and how I could get rid of it ?  besides
>chop($path);....

The system function returns the exit status of the command, which  is
zero becouse there was no error, so $path is set to 0. The current path
is not printed by perl but by pwd itself, you could do

$path=`pwd`;   # These are backquotes.
chop $path;
print "--- $path ---\n";

The ---'s are just to make sure that perl is printing the path and not
pwd itself. There must be some better way of finding the current working
directory but I must admit I don't know what it is.

Hope this clears the confusion, and that someone else knows the right way.

Regards

--
"Tae skapar fullt af vandamlum, | Yngvi Tr Sigurjnsson
umfram tae sem vie turfum      | Directorate of Fisheries, Inglfsstr?ti 1,
ae halda"                        | 150 Reykjavk, Iceland.
   --- Gunnar ?rvarsson          | Tel:(354 1)697900 Fax: 697990



Mon, 05 Jan 1998 03:00:00 GMT  
 system("pwd"); <- yuck

Quote:
Otis Gospodnetic writes:
> I decided I need to write some script that needs to know what the current
> path is.  So I opened up the Camel book and Llama book but could not find
> any info on obtaining the current path, only $ENV{'PATH'}, but that is not
> what I need.

> So I used $path = system("pwd");

> but when I printe "$path\n"; I get some zero (0) at the end for some
> reason, for example:

> cobalt:/usr/local/src/www>run-me-first.pl
> /usr/local/src/www
> 0
> cobalt:/usr/local/src/www>

system doesn't return the output of the command, it returns the exit status
of the command.  The output of the command went straight to your screen.

What you want is this:

        $path = `pwd`;

The output of pwd will now be in $path.  Unfortunately, it will include
the trailing newline, so you've got to chop it:

        $path=`pwd`;
        chop $path;

                                -Jack Applin

                                 http://www.cs.colostate.edu/~heckendo/Jack/



Tue, 06 Jan 1998 03:00:00 GMT  
 system("pwd"); <- yuck


Quote:
> Otis Gospodnetic writes:

> > I decided I need to write some script that needs to know what the current
> > path is.  So I opened up the Camel book and Llama book but could not find
> > any info on obtaining the current path, only $ENV{'PATH'}, but that is not
> > what I need.

> > So I used $path = system("pwd");

> > but when I printe "$path\n"; I get some zero (0) at the end for some
> > reason, for example:

> > cobalt:/usr/local/src/www>run-me-first.pl
> > /usr/local/src/www
> > 0
> > cobalt:/usr/local/src/www>

Also, take a look at the library 'pwd.pl', which comes with the standard
installation.  I'm not sure how it works, actually, but I know it's there.


Tue, 06 Jan 1998 03:00:00 GMT  
 system("pwd"); <- yuck

Quote:

>Hi,

>I decided I need to write some script that needs to know what the current
>path is.  So I opened up the Camel book and Llama book but could not find
>any info on obtaining the current path, only $ENV{'PATH'}, but that is not
>what I need.

>So I used $path = system("pwd");

Use:

        chop($path = `pwd`);

Quote:
>but when I printe "$path\n"; I get some zero (0) at the end for some
>reason, for example:

>cobalt:/usr/local/src/www>run-me-first.pl
>/usr/local/src/www
>0
>cobalt:/usr/local/src/www>

>any idea why that 0 is there and how I could get rid of it ?  besides
>chop($path);....

The 0 is there because that is what system returns.  The reason you see
the pwd is because that went to the stdout.  Try it without the "print $path"
and you'll see that it still prints out the path name.

--
        \    /
        ))  ((        Paul-Joseph "Dragon" de Werk
       ((    ))       Software Engineer/QA
        \\^^//        Non-HP Personnel

         \)(/

        / vv \



Tue, 06 Jan 1998 03:00:00 GMT  
 system("pwd"); <- yuck

Quote:

>Hope this clears the confusion, and that someone else knows the right way.

In perl 5:

use POSIX "getcwd";
print (getcwd(), "\n");

It's interesting to note that this just ends up calling the pwd utility
anyway (and since it uses a relative path, it's very fragile).  You
woulda thunk that it would call the real getcwd.

BTW, could someone *please* document the POSIX interfaces?  getcwd is
just one example of a perl interface in the POSIX module that is not
the same as the C interface.  The reference to the 1003.1 spec in
posix(3pm) is not sufficient for these cases.
--
Danny Faught -- Convex -- Operating System Demolitions Specialist
Magister artis ingeniique largitor venter.



Tue, 13 Jan 1998 03:00:00 GMT  
 system("pwd"); <- yuck


Quote:

> >Hope this clears the confusion, and that someone else knows the right way.

> In perl 5:

> use POSIX "getcwd";
> print (getcwd(), "\n");

> It's interesting to note that this just ends up calling the pwd utility
> anyway (and since it uses a relative path, it's very fragile).

Eeek! You're right. That's a {*filter*} surprise.

Quote:
> You woulda thunk that it would call the real getcwd.

Indeed. I wonder what the motivation for this was. Larry?

(Meanwhile, the Cwd::fastcwd() function might suit your needs.)

Quote:
> BTW, could someone *please* document the POSIX interfaces?  getcwd is
> just one example of a perl interface in the POSIX module that is not
> the same as the C interface.  The reference to the 1003.1 spec in
> posix(3pm) is not sufficient for these cases.

Contributions welcome. Much of Perl5 (outside the core) exists simply
because someone was motivated to do it.

Quote:
> Danny Faught -- Convex -- Operating System Demolitions Specialist
> Magister artis ingeniique largitor venter.

Tim.


Fri, 16 Jan 1998 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. system("ed <stuff>")

2. How to get DOS "PWD"?

3. print <<"EOF";

4. print <<"ending_print_tag";

5. <<ENFofFTP "here-documnet" question

6. system(("cp", "-Rf", "/tmp/a/*", "/tmp/b")); doesnt wrk

7. How to "$block_of_text<<"EOT" while "use strict"?

8. Tcl "exec" construct with "<<", in Perl

9. "character class ""bug""

10. Problem whit system("rmdir $ntdir") and system("md $ntdir");

11. One question about useage "<>"

12. problem with <input type="image".....

 

 
Powered by phpBB® Forum Software