Beginner problem to execute command

  • Thread starter Petterson Mikael
  • Start date
P

Petterson Mikael

$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
 
W

Walter Roberson

: $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().
 
J

Jürgen Exner

Petterson said:
$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
 
G

Guest

Jürgen Exner said:
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.
 
J

Jürgen Exner

Jürgen Exner said:
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.

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
 
B

Ben Morrow

Quoth "Jürgen Exner said:
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::path.
Besides, did you check "perldoc -f system" about the return value of
system()?

This is the bug.

Ben
 
S

Sherm Pendley

Jürgen Exner said:
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--
 
J

John W. Krahn

Jürgen Exner said:
Is there a specific reason why you are forking a new shell instead of using
Perl's buildin mkdir() function?


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
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top