For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2005 > system ("cd ..")









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author system ("cd ..")
Eliyah Kilada

2005-08-29, 9:55 pm

Hi,
It seems that the following code gives unexpected results:

system ("cd $dir_name");
system ("pwd");
---> this prints the old directory name not the new one. In other words,
the previous (cd) command hasn't taken its effect!
Do anyone know why?

Thanks And Best Regards,
Eliyah





Bob Showalter

2005-08-29, 9:55 pm

Eliyah Kilada wrote:
> Hi,
> It seems that the following code gives unexpected results:
>
> system ("cd $dir_name");
> system ("pwd");
> ---> this prints the old directory name not the new one. In other
> words, the previous (cd) command hasn't taken its effect!
> Do anyone know why?


Each call to system() creates a separate process in which the command is
executed. The cd command only affects that separate process.

You need to use Perl's chdir() function to change the working directory for
the current process.
Chris Devers

2005-08-30, 6:56 pm

On Mon, 29 Aug 2005, Eliyah Kilada wrote:

> - I agree with [you] that a shell script is more appropriate, but how
> can I write regular expressions in this way ? ;-)


By typing them, naturally :-)

In shell scripts, grep, sed & awk are the common regex using tools;
depending on what you're doing, one or another might be the most useful
one to make use of in a script.

grep does basic regex pattern matching

sed is a Stream EDitor, and can do regex substitutions etc

awk is more complex, almost like a baby version of Perl, but weirder

For my needs, grep & sed are usually enough for most simple shell
scripts, and if I need to make it complex enough that I'm considering
using awk, I just write the whole thing in Perl instead. On the other
hand, a lot of people, eveen now, use awk all the time.

In any case, there are several useful ways to get regexes into your
shell scripts, should you have the need to do so.



--
Chris Devers
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com