Search code examples
perlzshalpine-linux

Getting `Can't exec "command"` error on zsh on alpine linux from inside a perl script


I've got a perl wrapper script for taskwarrior's task command that runs perfectly fine on macos.

I've ported it to a docker container running alpine. When I run the script, I get this weird error:

> # bin/task_wrapper.pl task list
Wrapper command: command task   list
Can't exec "command": No such file or directory at bin/task_wrapper.pl line 61.

On my mac, it works just fine, no error.

which command reports: command: shell built-in command on both mac and on docker alpine.

I can run command task list directly from the command line in docker container and works fine.

Here's the whole script:


#! /usr/bin/env perl

use strict;
use warnings;

my $context;
my $runsub = shift @ARGV;

my @show_cmds = qw( done delete add modify );

{ no strict 'refs';
    &${runsub}(@ARGV) if $runsub;
}

# my @tw_cmds = qw ( add annotate append calc config context count delete denotate done duplicate edit execute export help import log logo modify prepend purge start stop synchronize undo version );

my @descriptors = qw ( due: dep: depends: attribute: status: priority: pri: due: after: start: before: end: );

sub _parse {
    my @bits =  @_;
    # find the first element that contains a command
    my $count = 0;
    my @ids;
    my @rest = @bits;
    foreach my $b (@bits) {
        if ( $b =~ /([[a-f][0-9]]{8}),*/ ) {
            push @ids, $1;
            shift @rest;
            next;
        }
        if ( $b =~ /(\d[\d\-]*),*/ ) {
            push @ids, $1;
            shift @rest;
            next;
        }
        last;
    }

    return \@ids, \@rest;
}

sub task {
    my $args = $_[0] || '';
    my $filter = '';
    my $subcmd = '';
    if (ref $args) {
        $filter = %$args{'filter'} || '';
        $subcmd = %$args{'subcmd'} || '';
        shift @_;
    }
    my @args = @_;
    my @a = qw ( command task );
    $context = $ENV{FLEXIBLE_CONTEXT} if !$context;
    if ($context && $args ne 'sync') {
        push @a, 'rc.context=' . $context;
    }
    if ($args =~ /sync/) {
        exec 'command task sync';
    } else {
        print "Wrapper command: @a $filter $subcmd @args \n";
        ################ ERROR ON LINE BELOW
        system("@a $filter $subcmd @args");
        ################
    }

    # show updated list of tasks
    my $show;
    $show = grep { $subcmd eq $_ } @show_cmds if $subcmd;
    if ($show) {
        my @sub_args;
        push @sub_args, 'limit:3' if !$context;
        push (@sub_args, '+st') if $context && $context !~ /\+sn|\+st/;
        task ({}, @sub_args);
    }
    #print @a;
    #print $ENV{FLEXIBLE_CONTEXT};
    return;
}

sub ta {
    task ({subcmd => 'add' }, @_ );
}

sub tm {
    my ($ids, $rest) = _parse(@_);
    task ({subcmd => 'modify', filter => "@$ids"}, @$rest);
}

# delete task
sub tdel {
    my ($ids, $rest) = _parse(@_);
    task ({subcmd => 'delete', filter => "@$ids"}, @$rest);
}

# done task
sub td {
    task ('done', @_);
}
sub tl {
    task ('next', "\\($ENV{'PU'} or +qst\\)", "-BLOCKED", @_);
}

sub tai {
    task ('add', $ENV{'PU'}, 'due:1h', @_);
}

Solution

  • You say you're using zsh, and zsh does have a builtin named command, but you're not using zsh. You're using /bin/sh since

    system( SHELL_CMD )
    

    is effectively short for

    system( '/bin/sh', '-c', SHELL_CMD )
    

    (It's technically the value returned by perl -V:sh, not /bin/sh. But that value is /bin/sh.)

    If you want to issue a zsh command, you will need to run zsh instead of /bin/sh.

    system( 'zsh', '-c', SHELL_CMD )
    

    Note that "@a $filter $subcmd @args" is not the proper way to build a shell command. It suffers from code injection bugs. You should use String::ShellQuote's shell_quote.