Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Beginner problem to execute command

Reply
Thread Tools

Beginner problem to execute command

 
 
Petterson Mikael
Guest
Posts: n/a
 
      06-01-2004
$cmd="mkdir -p `dirname $local`";
print("$cmd\n");
system($cmd) || die ("Could not create new directory");

Note: $local is equal to
/vobs/rbs/sw/rbssw1/boam_subsys/.metadata/.plugins/net.sourceforge.eclipseccase/pref_store.ini

I am running this script as ./eclipse.pl

Why do I get "Could not create new directory" when I have no problem to
to do this in a shell.

BR

//Mikael Petterson
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      06-01-2004
In article <(E-Mail Removed)>,
Petterson Mikael <(E-Mail Removed)> wrote:
: $cmd="mkdir -p `dirname $local`";
: print("$cmd\n");
: system($cmd) || die ("Could not create new directory");

:Note: $local is equal to
:/vobs/rbs/sw/rbssw1/boam_subsys/.metadata/.plugins/net.sourceforge.eclipseccase/pref_store.ini

:I am running this script as ./eclipse.pl

:Why do I get "Could not create new directory" when I have no problem to
:to do this in a shell.

perldoc perlops

Apart from the behavior described above, Perl does not
expand multiple levels of interpolation. In particular,
contrary to the expectations of shell programmers, back-
quotes do NOT interpolate within double quotes, nor do
single quotes impede evaluation of variables when used
within double quotes.

Thus, your system() command is submitting something that contains
the backquotes in place, but with $local expanded. That's probably
not what you thought you were doing, and could lead to conflicts
in behaviour between your shell and the '/bin/sh' that is used for system().
--
History is a pile of debris -- Laurie Anderson
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      06-01-2004
Petterson Mikael wrote:
> $cmd="mkdir -p `dirname $local`";
> print("$cmd\n");
> system($cmd) || die ("Could not create new directory");


Is there a specific reason why you are forking a new shell instead of using
Perl's buildin mkdir() function?

> Note: $local is equal to
>

/vobs/rbs/sw/rbssw1/boam_subsys/.metadata/.plugins/net.sourceforge.eclipsecc
ase/pref_store.ini
>
> I am running this script as ./eclipse.pl
>
> Why do I get "Could not create new directory"


Why don't you ask Perl?
system($cmd) or die ("Could not create new directory because $!");

Besides, did you check "perldoc -f system" about the return value of
system()?
It differs from what you might expect.

jue


 
Reply With Quote
 
BreadWithSpam@fractious.net
Guest
Posts: n/a
 
      06-01-2004
"Jürgen Exner" <(E-Mail Removed)> writes:
> Petterson Mikael wrote:
> > $cmd="mkdir -p `dirname $local`";
> > print("$cmd\n");
> > system($cmd) || die ("Could not create new directory");

>
> Is there a specific reason why you are forking a new shell instead of using
> Perl's buildin mkdir() function?


Or, for that matter, the File::Basename module to get
the dirname function as well.

> Why don't you ask Perl?
> system($cmd) or die ("Could not create new directory because $!");


$! is your friend.

--
Plain Bread alone for e-mail, thanks. The rest gets trashed.
No HTML in E-Mail! -- http://www.expita.com/nomime.html
Are you posting responses that are easy for others to follow?
http://www.greenend.org.uk/rjk/2000/06/14/quoting
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      06-01-2004
Jürgen Exner wrote:
> Petterson Mikael wrote:


Some more oddities I noticed:

- you didn't tell us what you expect your code to do. This is essential to
determine if your code has problem or your understanding has a problem.

>> $cmd="mkdir -p `dirname $local`";


Are you certain that you want to create two new folders the first named
"`dirname" and the second whatever the content of $local is plus a trailing
backquote? This looks odd to me.

jue


 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      06-01-2004

Quoth "Jürgen Exner" <(E-Mail Removed)>:
> Petterson Mikael wrote:
> > $cmd="mkdir -p `dirname $local`";
> > print("$cmd\n");
> > system($cmd) || die ("Could not create new directory");

>
> Is there a specific reason why you are forking a new shell instead of using
> Perl's buildin mkdir() function?


Presumably as mkdir doesn't have a -p option?
OP: see File:ath.

> Besides, did you check "perldoc -f system" about the return value of
> system()?


This is the bug.

Ben

--
"If a book is worth reading when you are six, * http://www.velocityreviews.com/forums/(E-Mail Removed)
it is worth reading when you are sixty." - C.S.Lewis
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      06-01-2004
Jürgen Exner wrote:

> Are you certain that you want to create two new folders the first named
> "`dirname" and the second whatever the content of $local is plus a
> trailing backquote? This looks odd to me.


Backticks work the same in a shell as they do in Perl. He might have
intended to run "dirname $local", and use its output as an argument to
"mkdir -p".

Or, he might have been hoping for multiple levels of interpolation in Perl -
which, as a another poster pointed out above, doesn't happen.

At any rate, I agree with the first part of your comment - Since both shell
and Perl use backticks and $ variables, it's impossible to do more than
guess at what level the OP expected things to be expanded.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      06-02-2004
"Jürgen Exner" wrote:
>
> Petterson Mikael wrote:
> > $cmd="mkdir -p `dirname $local`";
> > print("$cmd\n");
> > system($cmd) || die ("Could not create new directory");

>
> Is there a specific reason why you are forking a new shell instead of using
> Perl's buildin mkdir() function?
>
> > Note: $local is equal to
> >

> /vobs/rbs/sw/rbssw1/boam_subsys/.metadata/.plugins/net.sourceforge.eclipsecc
> ase/pref_store.ini
> >
> > I am running this script as ./eclipse.pl
> >
> > Why do I get "Could not create new directory"

>
> Why don't you ask Perl?
> system($cmd) or die ("Could not create new directory because $!");
>
> Besides, did you check "perldoc -f system" about the return value of
> system()?
> It differs from what you might expect.


Yes, which is why "system() or die()" will not do what you apparently
expect it to do and neither will $!. Have you read "perldoc -f system"
lately?


John
--
use Perl;
program
fulfillment
 
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
RUN/execute a Command-Line command from an ASP page. Lucas Cowald ASP .Net 4 10-23-2003 11:09 AM
RUN/execute a Command-Line command from an ASP page Lucas Cowald ASP .Net Building Controls 1 10-22-2003 04:26 PM
RUN/execute a Command-Line command from an ASP page. Lucas Cowald ASP .Net Datagrid Control 1 10-22-2003 12:22 PM
RUN/execute a Command-Line command from an ASP page. Lucas Cowald ASP .Net Security 0 10-22-2003 11:15 AM
RUN/execute a Command-Line command from an ASP page. Lucas Cowald ASP .Net Mobile 0 10-22-2003 11:15 AM



Advertisments