Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Passing a directory into $ARGV[0] from a shell script

Reply
Thread Tools

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

 
 
John
Guest
Posts: n/a
 
      09-25-2003
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.


 
Reply With Quote
 
 
 
 
Darren Dunham
Guest
Posts: n/a
 
      09-25-2003
John <> wrote:
> 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
Unix System Administrator Taos - The SysAdmin Company
Got some Dr Pepper? San Francisco, CA bay area
< This line left intentionally blank to confuse you. >
 
Reply With Quote
 
 
 
 
John
Guest
Posts: n/a
 
      09-25-2003

"Darren Dunham" <> wrote in message
news:JHEcb.3003$ m...
> John <> wrote:
> > 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
> 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


 
Reply With Quote
 
Darren Dunham
Guest
Posts: n/a
 
      09-25-2003
I'm a bit confused, so let me review your response here..


John <> wrote:
> "Darren Dunham" <> wrote in message
> news:JHEcb.3003$ m...
>> John <> wrote:


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

--
Darren Dunham
Unix System Administrator Taos - The SysAdmin Company
Got some Dr Pepper? San Francisco, CA bay area
< This line left intentionally blank to confuse you. >
 
Reply With Quote
 
James Willmore
Guest
Posts: n/a
 
      09-27-2003
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
 
Reply With Quote
 
John
Guest
Posts: n/a
 
      09-28-2003

"Purl Gurl" <> wrote in message
news:...
> John wrote:
>
> > Darren Dunham wrote:
> > > 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


 
Reply With Quote
 
John
Guest
Posts: n/a
 
      09-28-2003

*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




 
Reply With Quote
 
John
Guest
Posts: n/a
 
      09-28-2003

"Purl Gurl" <> wrote in message
news:...
> John wrote:
>
> > Purl Gurl wrote:
> > > John wrote:
> > > > Darren Dunham wrote:
> > > > > John wrote:

>
> (snipped)
>
> > > > 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.

>
> > 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."?
>
> You are correct. Both statements by you are identical, word-for-word.
>
>
> > Seems the same to me. Can you elaborate as to which parameters I have
> > suddenly changed?

>
> 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
> --
> Rock Midis! Science Fiction! Amazing Androids!
> http://www.purlgurl.net/~callgirl


Never mind. Maybe I am...


 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      09-29-2003
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


 
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
Re: How to pass shell variable to shell script from python D'Arcy J.M. Cain Python 0 02-27-2008 01:56 PM
Re: How to pass shell variable to shell script from python Gerardo Herzig Python 1 02-27-2008 12:19 PM
Re: How to pass shell variable to shell script from python Christian Heimes Python 0 02-27-2008 10:53 AM
passing arguments to a shell script from a perl script doni Perl Misc 3 12-10-2007 08:55 AM
execute a shell script in a shell script moongeegee Perl Misc 2 12-04-2007 12:18 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57