Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,262
    Rep Power
    602

    Using * within backticks


    I've been away from perl for a while so I am a bit rusty. I cannot understand why this fails:
    Code:
     my $filelist=`ls "$htmlDir$ARGV[0]/$dir/*.jpg"`;
    The error gives me the correct path

    ls: cannot access /var/www/html/Massachusetts/Sudbury/*.jpg: No such file or directory

    and when I use that path from the command line it works. I guess perl is doing something with the * but escaping it does not help. TIA.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,229
    Rep Power
    1296
    Why are you shelling out to the ls command instead of using one of perl's builtin methods for retrieving the file listing?

    glob function
    File::Glob module
    Diamond Operator

    There's also the opendir/readdir functions.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,229
    Rep Power
    1296
    If you want to shell out, which IMO is the wrong choice, the command needs to be more specific. Like this:

    Code:
    chomp(my @filelist = `/bin/bash -c "ls $htmlDir$ARGV[0]/$dir/*.jpg"`);
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,262
    Rep Power
    602
    Specifying the shell produces the same error. Just as an aside the reason to shell out is irreverent to my question which at this point is an academic one. I'm not really using this but discovered the oddity inadvertently. It SHOULD work. Note that if I remove the '*.jpg' it works other than returning all the files rather than just the jpg files.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,229
    Rep Power
    1296
    That's odd, it works for me.

    Code:
    [rkb@iso101 ~]$ cat test.pl
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use Data::Dumper;
    
    chomp(my @filelist = `/bin/bash -c "ls ./*.pl"`);
    print Dumper \@filelist;
    
    [rkb@iso101 ~]$ ./test.pl
    $VAR1 = [
              './chg_request.pl',
              './cpu.pl',
              './rt2.pl',
              './rt.pl',
              './sqlprch.pl',
              './stores.pl',
              './test.pl'
            ];
    Please post a complete test script like I did which demonstrates the problem?
  10. #6
  11. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,024
    Rep Power
    9616
    Originally Posted by gw1500se
    I'm not really using this but discovered the oddity inadvertently. It SHOULD work.
    FishMonger's solution, yes. Your original code, probably not.

    * expansion is done by the shell, not by the program. When you do "ls *" the shell figures out what filenames * matches and then passes each one as a separate argument to ls. At least that's what bash does - whatever shell Perl is using (csh?) does not support that. Thus if you want to do that command then you need to run it through bash.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,262
    Rep Power
    602
    Interesting. I thought perl was its own shell or at least uses the shell from which it was invoked (in my case it is bash). I did not know it uses one of the standard shells. If it uses csh then it makes sense. Is the shell it uses configurable somewhere?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,229
    Rep Power
    1296
    When perl forks a shell process, it uses the borne shell. I would need to run a test to be sure, but I suspect that the sh shell doesn't support the * filename expansion operator (I think it's classified as an operator).
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,262
    Rep Power
    602
    I haven't used 'sh' in eons, although I did back in the bad old days. Is that configurable in perl?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,229
    Rep Power
    1296
    I've never bothered to check, but I doubt it is configurable unless you want to consider making the adjustment to the C source code and compile your own custom version of perl.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,262
    Rep Power
    602
    Yuck! OK, at least now I know. Thanks.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    95
    Rep Power
    42
    According to the docs, Perl uses /bin/sh, which is usually a symlink to a Bourne-compatible shell, such as bash or dash. Either way the Bourne shell does support * expansion and so do any shells that /bin/sh might possibly be symlinked to (unless you change the symlink yourself of course). Note that the documentation specifically states that * will work:

    Shell wildcards, pipes, and redirections will be honored.
    So whatever the problem is with the wildcard, it's not related to which shell is being used (which explains why it doesn't work even if bash is invoked explicitly).
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,229
    Rep Power
    1296
    I ran another test and discovered that the double quotes you're using is what caused it to fail.

    Broken:
    Code:
    [rkb@iso101 ~]$ cat test.pl
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use Data::Dumper;
    
    chomp(my @filelist = `ls "./*.pl"`);
    print Dumper \@filelist;
    
    
    [rkb@iso101 ~]$ ./test.pl
    ls: cannot access ./*.pl: No such file or directory
    $VAR1 = [];
    Fixed:
    Code:
    [rkb@iso101 ~]$ cat test.pl
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use Data::Dumper;
    
    chomp(my @filelist = `ls ./*.pl`);
    print Dumper \@filelist;
    
    
    [rkb@iso101 ~]$ ./test.pl
    $VAR1 = [
              './chg_request.pl',
              './cpu.pl',
              './rt2.pl',
              './rt.pl',
              './sqlprch.pl',
              './stores.pl',
              './test.pl'
            ];

    Comments on this post

    • sepp2k1 agrees
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    95
    Rep Power
    42
    If you still need the quotes because the path may contain spaces, you can wrap the quotes just around the directory part of the path like this:

    Code:
    `ls "$htmlDir$ARGV[0]/$dir/"*.jpg`
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,262
    Rep Power
    602
    I tried it with and without quotes when I first found the problem. Could it be implementation specific? I'm using CentOS 7 and have not tried it on another *nix.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo