Search code examples
bashperlbackticks

sh: 1: Syntax error: redirection unexpected when using backticks perl


my $mysql_cmd = 'debconf-set-selections <<< \'mysql-server mysql-server/root_password password 6ygr\' ;debconf-set-selections <<< \'mysql-server mysql-server/root_password_again password 6ygr\'; '.our $install_cmd." mysql-server";
my $mysql_stat = `$mysql_cmd`;

I'm using the above piece of code to install mysql from my perl script. But I'm getting this error

sh: 1: Syntax error: redirection unexpected

When I printed $mysql_cmd I got debconf-set-selections <<< 'mysql-server mysql-server/root_password password 6ygr' ;debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password 6ygr'; apt-get -y install mysql-server

which seems to correct and manually executing this command in shell gives desired output. But when execute the perl script it gives error. Any idea?


Solution

  • You should use IPC::Run for this kind of jobs

    use IPC::Run qw(run);
    my @options = map "mysql-server mysql-server/$_ password 6ygr",
        qw(root_password root_password_again);
    my $mysql_stat;
    run [qw(debconf-set-selections)], '<', \$_, '>>', \$mysql_stat, '2>>', \$mysql_stat
        or die "debconf-set-selections: $?"
        for @options;
    run [ $install_cmd, 'mysql-server' ], '>>', \$mysql_stat, '2>>', \$mysql_stat
        or die "$install_cmd: $?";
    

    The main reason is you don't like to deal with quoting issues and incompatibilities and so on.