Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Can't exec "command": No such file or directory error

by nysus (Parson)
on Dec 14, 2022 at 21:50 UTC ( #11148867=perlquestion: print w/replies, xml ) Need Help??

nysus has asked for the wisdom of the Perl Monks concerning the following question:

On macos running 5.36 with zsh, this works:

#!/usr/bin/env perl use strict; use warnings; system 'command task list';

It lists the tasks in Taskwarrior. However, the same code on a docker container running also running zsh and 5.36 throws this at me:

Can't exec "command": No such file or directory at ./test.pl line 6.

zsh version on alpine is 5.9 (x86_64-alpine-linux-musl). macos is zsh 5.8.1 (x86_64-apple-darwin22.0). which command reports: "command: shell built-in command" on both mac and on docker alpine.

Any ideas what the problem might be?

$PM = "Perl Monk's";
$MC = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar Parson";
$nysus = $PM . ' ' . $MC;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Can't exec "command": No such file or directory error (updated!)
by haukex (Archbishop) on Dec 14, 2022 at 22:24 UTC

    I don't know much about zsh, but my best guess is that this might be The problem of "the" default shell. You might try system('/bin/zsh','-c','command task list');, but be aware of the security implications of doing this with with anything other than a fixed string.

    Update: What is task? Because if it's a binary then my advice here is wrong - see my reply here.

      In both zsh and bash the "command" shell builtin (and its cousin builtin) are helpers for bypassing shell functions or aliases. When you prefix something with it then the shell explicitly searches for an external thing to run rather than running any shell function or builtin of the same name. The usual usage is for something like having a shell function of the same name wrapping something (to say manipulate arguments passed) whenever in the function you want to actually call the "real" program (or in the case of builtin the shell's actual builtin command of the same name (e.g. you could write a cd shell fn which then actually calls builtin cd blahblah to make the shell change the cwd)). If you didn't then the shell would just recursively call the same function over and over.

      At any rate two closing thoughts: zsh is just awesome and anyone not using it as their interactive or programming shell is missing out; and unless you've got some reaaaaaly wonky default shell environment you're dealing with there's probably not much reason to prefix something you're calling from perl (or anything else "external") with it "command".

      (The command in /usr/bin on OSX is actually a bash script actually which calls builtin which is just weird . . .)

      Edit: made advice about "command" being unneeded more explicit.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        In both zsh and bash the "command" shell builtin (and its cousin builtin) are helpers for bypassing shell functions or aliases.

        Thanks for pointing this out and sorry for not responding sooner. I admit I haven't used command and I should have looked it up first, because of course that changes things - it seems strange to want to call the shell only to tell the shell to not use a builtin or function. As a side note, interestingly, it looks like command is slightly different in the two shells: bash says "Runs command with arguments ignoring any shell function named command. Only shell builtin commands or commands found by searching the PATH are executed." while in zsh: "The simple command argument is taken as an external command instead of a function or builtin and is executed. If the POSIX_BUILTINS option is set, builtins will also be executed but certain special properties of them are suppressed." - another argument for avoiding The problem of "the" default shell.

        nysus: What is task? If it's something like Taskwarrior (where apparently task list is a valid command), then my suggestion above is actually wrong because no shell is needed, and you should be using something like system('/usr/bin/task','list') instead. See also my node Calling External Commands More Safely.

      Beautiful. Worked. And thanks for the link, I'll take a look. My shell foo is weak.

      $PM = "Perl Monk's";
      $MC = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar Parson";
      $nysus = $PM . ' ' . $MC;
      Click here if you love Perl Monks

Re: Can't exec "command": No such file or directory error
by stevieb (Canon) on Dec 14, 2022 at 22:25 UTC
    Any ideas what the problem might be?

    Yes. Only an idea though.

    The PATH environment variable differs between the systems when running system from perl. Perhaps when executing in the Docker container the 'command' binary can't be found because it's not looking in the right place.

    system("echo \$PATH");
      the 'command' binary

      nysus wrote 'which command reports: "command: shell built-in command"' which leads me to think that there may not be a command binary at all.

        Indeed. find command turned up /usr/bin/command on macos. I can not find any such animal on alpine. I'm wondering if it's in a separate alpine package.

        What's weird is if I do command task on alpine directly on the command line, it runs just fine. It's only with perl I seem to have the problem.

        $PM = "Perl Monk's";
        $MC = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar Parson";
        $nysus = $PM . ' ' . $MC;
        Click here if you love Perl Monks

Re: Can't exec "command": No such file or directory error
by shmem (Chancellor) on Dec 15, 2022 at 03:49 UTC
    Any ideas what the problem might be?

    Whenever a command is addressed to me, I rise an eyebrow. Who is commanding me, to what aim, who benefits? If I was a soldier in the army of the Russian Federation, I would of course obey, because the command is issued ultimately by Mother Russia.
    The PATH is important. In chinese it is called TAO.
    Indian philosophies have other names, christians call it Jesus, but don't type that in, you'll get: command not found

    ) nice song, shrewd lyrics, seventies, no wonder: very few in "the west" really know life and death under the rule of Mother Russia.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      I'm going to say nothing, other than I'd personally appreciate peer-reviewed links. Doesn't seem like your normal presentation.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11148867]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2023-05-31 02:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?