Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > $current_path=`echo $cwd` does not work

Reply
Thread Tools

$current_path=`echo $cwd` does not work

 
 
Kuhl
Guest
Posts: n/a
 
      09-27-2008
Hi, all:

Backticks `` are supposed to be able to transfer the result of shell
commands to Perl variable.
But it's very confusing that it does not behave in the way that I
think it should do.

$current_path=`pwd`; This works.
$current_path=`echo $cwd`; This does not work.

But in fact, pwd is aliased to echo $cwd. Details see below.

What's the issue? How to fix it?

Thanks.


# which pwd
pwd: aliased to echo $cwd
# pwd
/home/user/shell
# echo $cwd
/home/user/shell

#!/usr/bin/perl
$current_path=`pwd`;
print "\nCurrent path is $current_path .\n";
$current_path=`echo $cwd`;
print "\nCurrent path is $current_path .\n";
$current_path=`echo \$cwd`;
print "\nCurrent path is $current_path .\n";
$current_path=`"echo $cwd"`;
print "\nCurrent path is $current_path .\n";
exit 0;


But the result of this script is:

Current path is /home/user/shell
..

Current path is
..

Current path is
..
sh: line 1: echo : command not found

Current path is .
#
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      09-27-2008
Kuhl <(E-Mail Removed)> wrote:
>Hi, all:
>
>Backticks `` are supposed to be able to transfer the result of shell
>commands to Perl variable.
>But it's very confusing that it does not behave in the way that I
>think it should do.
>
>$current_path=`pwd`; This works.
>$current_path=`echo $cwd`; This does not work.
>
>But in fact, pwd is aliased to echo $cwd. Details see below.


A guess: those aliases are not valid in your _system_ shell but only in
the shell that you are using as your standard shell from the command
line?

Try calling print `alias` to find out, if those aliases are even
defined.

jue
 
Reply With Quote
 
 
 
 
Ron Bergin
Guest
Posts: n/a
 
      09-27-2008
On Sep 27, 7:10 am, Kuhl <(E-Mail Removed)> wrote:
> Hi, all:
>
> Backticks `` are supposed to be able to transfer the result of shell
> commands to Perl variable.
> But it's very confusing that it does not behave in the way that I
> think it should do.
>
> $current_path=`pwd`; This works.
> $current_path=`echo $cwd`; This does not work.
>

$cwd is being seen as a Perl scalar that is interpolated by Perl
before passing it to the shell...you need to escape it.

$current_path=`echo \$cwd`;
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      09-27-2008
Kuhl <(E-Mail Removed)> wrote:

> Hi, all:
>
> Backticks `` are supposed to be able to transfer the result of shell
> commands to Perl variable.
> But it's very confusing that it does not behave in the way that I
> think it should do.
>
> $current_path=`pwd`; This works.
> $current_path=`echo $cwd`; This does not work.


[..]
> #!/usr/bin/perl


use strict;
use warnings;

and all becomes clear. It does work, you only misunderstand ``.

There is a very good reason why those two lines are recommended daily in
this group.

--
John http://johnbokma.com/ - Hacking & Hiking in Mexico

Perl help in exchange for a gift:
http://johnbokma.com/perl/help-in-ex...or-a-gift.html
 
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
MS work around on text wrapping in a datagrid does not work TB ASP .Net 2 02-22-2006 10:34 PM
Why does post or pre incremenent or decrement does not work inside a sizeof operator? Tarun C Programming 5 07-14-2005 03:58 PM
Why does this (very simple piece of) code does not work? jblazi Python 5 08-16-2004 01:30 PM
Webservice works once and then DOES NOT seem to work even though program does not crash Phi! ASP .Net Web Services 1 04-23-2004 08:42 AM
int('2.1') does not work while int(float('2.1')) does Vineet Jain Python 9 04-16-2004 10:12 AM



Advertisments