Beginner problem to execute command

Discussion in 'Perl Misc' started by Petterson Mikael, Jun 1, 2004.

  1. $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
     
    Petterson Mikael, Jun 1, 2004
    #1
    1. Advertising

  2. In article <>,
    Petterson Mikael <> 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
     
    Walter Roberson, Jun 1, 2004
    #2
    1. Advertising

  3. 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
     
    Jürgen Exner, Jun 1, 2004
    #3
  4. Petterson Mikael

    Guest

    "Jürgen Exner" <> 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
     
    , Jun 1, 2004
    #4
  5. 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
     
    Jürgen Exner, Jun 1, 2004
    #5
  6. Petterson Mikael

    Ben Morrow Guest

    Quoth "Jürgen Exner" <>:
    > 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::path.

    > 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, *
    it is worth reading when you are sixty." - C.S.Lewis
     
    Ben Morrow, Jun 1, 2004
    #6
  7. 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
     
    Sherm Pendley, Jun 1, 2004
    #7
  8. "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
     
    John W. Krahn, Jun 2, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lucas Cowald
    Replies:
    4
    Views:
    1,101
    Tohid
    Oct 23, 2003
  2. Lucas Cowald

    RUN/execute a Command-Line command from an ASP page

    Lucas Cowald, Oct 22, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    261
    Genich
    Oct 22, 2003
  3. Lucas Cowald

    RUN/execute a Command-Line command from an ASP page.

    Lucas Cowald, Oct 22, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    224
    Oliver
    Oct 22, 2003
  4. Lucas Cowald

    RUN/execute a Command-Line command from an ASP page.

    Lucas Cowald, Oct 22, 2003, in forum: ASP .Net Mobile
    Replies:
    0
    Views:
    191
    Lucas Cowald
    Oct 22, 2003
  5. Lucas Cowald

    RUN/execute a Command-Line command from an ASP page.

    Lucas Cowald, Oct 22, 2003, in forum: ASP .Net Security
    Replies:
    0
    Views:
    248
    Lucas Cowald
    Oct 22, 2003
Loading...

Share This Page