Passing a directory into $ARGV[0] from a shell script

J

John

Hi,

One other Q for tonite. I just realised a little nuisance...

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

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.

But my perl script dies due to the following line in its contents:
chdir '$ARGV[0]' || die "Cannot chdir to: $!\n";

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].

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 '.'?

Thank you.
 
D

Darren Dunham

John said:
Hi,

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

Windows or other OS?
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.
But my perl script dies due to the following line in its contents:
chdir '$ARGV[0]' || die "Cannot chdir to: $!\n";

1) '$ARGV[0]' is single quotes and will not be interpolated.
2) Did you check the value of ARGV[0] as sane before chdiring?
3) I see no point in your post where you show the output of that line,
including the valued of $!. The message would likely help understand
why the chdir is failing.
 
J

John

Darren Dunham said:
John said:
Hi,

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

Windows or other OS?
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.
But my perl script dies due to the following line in its contents:
chdir '$ARGV[0]' || die "Cannot chdir to: $!\n";

1) '$ARGV[0]' is single quotes and will not be interpolated.
2) Did you check the value of ARGV[0] as sane before chdiring?
3) I see no point in your post where you show the output of that line,
including the valued of $!. The message would likely help understand
why the chdir is failing.

--
Darren Dunham (e-mail address removed)
Unix System Administrator Taos - The SysAdmin Company
Got some Dr Pepper? San Francisco, CA bay area
< This line left intentionally blank to confuse you. >

Darren,

OS = Solaris
1) Of course. This gives me no error, the script runs and does 85% of the
work. :)
Where it's failing is that it still cannot change the permissions of the
directory that the edited file is in UNLESS I remove the DIE extension.
I'll have a look at it tomorrow - a printout may help.
2) Value seems OK as passed from the shell script. ie.: $ARGV[0] holds .
[DOT]
3) Hmmm...I guess so :(

Thanks
J
 
D

Darren Dunham

I'm a bit confused, so let me review your response here..


John said:
Darren Dunham said:
But my perl script dies due to the following line in its contents:
chdir '$ARGV[0]' || die "Cannot chdir to: $!\n";

1) '$ARGV[0]' is single quotes and will not be interpolated.

1) Of course. This gives me no error, the script runs and does 85% of the
work. :)

What does that mean, and what does that have to do with the fact that
you almost certainly want $ARGV[0] instead of '$ARGV[0]'.
Where it's failing is that it still cannot change the permissions of the
directory that the edited file is in UNLESS I remove the DIE extension.
I'll have a look at it tomorrow - a printout may help.

Change permissions? That has nothing to do with the code you've shown.

the '$ARGV[0]' is *wrong*. Remove the single quotes. It is unnecessary
to quote a single scalar variable, and in your case it's causing you
problems.

chdir $ARGV[0] ....
 
J

James Willmore

On Thu, 25 Sep 2003 16:12:15 GMT
John said:
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
 
J

John

Purl Gurl said:
John said:
Darren said:
John wrote:
(snipped)
But my perl script dies due to the following line in its contents:
chdir '$ARGV[0]' || die "Cannot chdir to: $!\n";
1) '$ARGV[0]' is single quotes and will not be interpolated.
1) Of course. This gives me no error, the script runs


chdir ('$ARGV[0]') or die "Directory: $!\n";

Directory: No such file or directory

Where it's failing is that it still cannot change the permissions of the
directory that the edited file is in UNLESS I remove the DIE extension.

You make no mention of this in your original article. You have
suddenly changed parameters which is a very common tactic of
a typical troll.


Purl Gurl

The original post said:
"If I remove the DIE option both scripts run together as expected."

Is it not the same as:
"Where it's failing is that it still cannot change the permissions of the
directory that the edited file is in UNLESS I remove the DIE extension."?

Seems the same to me. Can you elaborate as to which parameters I have
suddenly changed?

Regards,
Not a typical troll
 
J

John

*snip*
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

Guys,
It looks like I may have confused some ppl here with a not-too-exhausting
description of what I'm actually trying to accomplish.
Sorry for that. Will try to elaborate:

I have a shell script and I have a Perl script.
Each can be run on its own or they can be run in sequence [shell first].

Now, the key to all of this is a file located is some directory that I'd
like to edit/change/delete/whatever.

I can run the Perl script on its own like this:
$ change.pl /dir/test/august myfile.txt

or the Perl script can be called from within the shell script like this:
...
change . myfile.txt
...

The reason for the above . [DOT] is that at this point the shell script has
already descended to the directory holding myfile.txt so as it calls the
Perl script I need to stay in the same directory. So $ARGV[0] needs to be .
here but on the other hand, it can [but does not have to] be a full
directory path if the Perl script is run on its own.

And so, when the two scripts were run in sequence, the Perl script would die
at:
chdir '$ARGV[0]' || die "Cannot chdir to: $!\n";

The minute I removed the || die option, the Perl script would complete. So
my first thoughts were along the lines of:
"Hmmm, it's not accepting chdir '.' as valid and falls over. Why is it so?"

Am I making sense? Sorry once again if this post is no improvement on the
previous ones.
I won't be able to adjust the code till Tuesday so unfortunately there's not
much I can offer in terms of further 'constructive' input till then.

Thanks
 
J

John

Purl Gurl said:
extension.


extension."?

You are correct. Both statements by you are identical, word-for-word.



You have again stated your suddenly changed parameters.
Why are you asking me to elaborate on what you already know?

Are you trolling?


Purl Gurl

Never mind. Maybe I am...
 
J

Jürgen Exner

John wrote:
[...
And so, when the two scripts were run in sequence, the Perl script
would die at:
chdir '$ARGV[0]' || die "Cannot chdir to: $!\n";

As has been pointed out before: does a directory with the literal(!!!) name
$ARGV[0] exist?

You are using _single_ quotes, i.e. no stringification will happen, in
particular the variable name $ARGV[0] will not be expanded to the variables
value but it will be taken literally as the dollar sign, followed by an
upper case A, followed by upper R, followed by upper G, etc.

Also, you may want to replace the high-priority "||" with the low priority
"or".
Otherwise your expression will be evaluated as
chdir ("$ARGV[0]" || die "Cannot chdir to: $!\n");
which is not what you want.

jue


jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top