On Thu, 25 Sep 2003 16:12:15 GMT
"John" <> wrote:
<snip>
> My shell script does some things and when it finds a particular file
> it calls a perl script [from within the shell script] like this:
> change.pl . myfile.txt << 2 command line parameters, a directory
> [DOT] and a filename
Simple - $ARGV{0] and $ARGV[1]. Let's move on
>
> I wanted . to reflect the working directory of the file that that
> needs to be edited. Since my shell script has already descended into
> the file's directory then there is no need to pass any other
> directory to the perl script. Hence, I wanted to use . [DOT] as I
> don't really want to change the directories at this point.
Huh? Okay, so you're in the directory you want to be in, but you want
to pass a directory to the script - right? There's a phrase that
comes to mind, but since the 'Net is suppose to be 'G' rated, I won't
use it
>
> But my perl script dies due to the following line in its contents:
> chdir '$ARGV[0]' || die "Cannot chdir to: $!\n";
Why? Oh, that's the question you're asking

Okay, we'll get to
that, okay?
>
> On the other hand, if I want to run the perl script on its own, I
> need to be able to give it some sort of a directory - hence the
> $ARGV[0].
Ah. What do you mean "on its own"? Are you running this from cron or
some other utility that runs automated tasks? Still a bit confused.
>
> If I remove the DIE option both scripts run together as expected. Am
> I doing something wrong? Why is the perl script unable to chdir '.'?
Yes, you are doing something wrong

It appears that you have a few
issues. First, use '-w' on the first line of your script. This will
issue warnings. Second, use the strict pragma. If you have duplicate
variables, undefined variables, etc., this will cause the script to
die and issue (a) message(s) describing what went wrong. You may want
to look over what I wrote and see if it's what you wanted.
==untested==
#!/usr/bin/perl -w
#use the strict pragma - prevents you from hanging yourself
use strict;
#use warnings to catch what the '-w' command line option misses
use warnings;
#use diagnostics - print more useful messages if we die
use diagnostics;
#the home environmental variable does not exist for
#some versions of Windows - however, the present
#working directory _should_ exist on either OS
my $chdir = $ENV{HOME} || $ENV{PWD};
#get the first parameter passed to the script and make
#that $chidr if something was passed to the script
#(ie if $ARGV[0] is defined)
$chdir = $ARGV[0] if(defined $ARGV[0]);
#now, try and change the current working directory -
#die if we can't
chdir $chdir || die "Can't chdir to $chdir: $!\n";
#execute the 'pwd' command - not sure of the Windows
#equivlent - this is just "proof of concept" (show we did
#change to the directory)
system("pwd");
==untested==
Now - if the first parameter is the file, -not- the directory, then
the script will die (because you'll be trying to change the working
directory to a file and, well, that doesn't work so well -unless- you
have a file _and_ a directory by the same name). You may want to look
over Getopt::Std or Getopt::Long if you want non-positional parameters
passed to the script.
HTH
--
Jim
Copyright notice: all code written by the author in this post is
released under the GPL.
http://www.gnu.org/licenses/gpl.txt
for more information.
a fortune quote ...
One Page Principle: A specification that will not fit on one
page of 8.5x11 inch paper cannot be understood. -- Mark Ardis