2
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?

Harikrishnan
  • 9,688
  • 11
  • 84
  • 127

2 Answers2

7

perl uses /bin/sh to execute commands. Try feeding that string to sh and if you get the same error that is the problem.

If you want to use bash to execute that statement, use bash -c "cmd args ..." as the command.

If you only need to pass a single line to a command, it may be a good idea to avoid the non-portable bashism of <<<. Instead, use echo "whatever" | command which works in any shell.

Jens
  • 69,818
  • 15
  • 125
  • 179
-1

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.

Hynek -Pichi- Vychodil
  • 26,174
  • 5
  • 52
  • 73