Search code examples
perlbashsolaris

Bash Command In Perl Script


How can I get the following Bash command to execute correctly in a Perl Script?

su - oracle -c  "echo \"select database_role from v\\\$database;\" | sqlplus -s / as sysdba";

When placed in a bash script, the command executes correctly. But when in a perl script using the following:

my @roleQuery = `su - oracle -c  "echo \"select database_role from v\\\$database;\" | sqlplus -s / as sysdba"`;

Returns:

sh:  | sqlplus -s / as sysdba: not found
Oracle Corporation      SunOS 5.10      Generic Patch   January 2005
 You have mail.
 select

I am executing both the bash and perl script as the same user.

Bash Output (Which is my desired return)

Oracle Corporation      SunOS 5.10      Generic Patch   January 2005
You have mail.

DATABASE_ROLE
----------------
PRIMARY

Solution

  • qx (backticks) follows the same rules as qq (double-quotes). Escape the \ and $ characters. (And @ if you had any.)

    su - oracle -c  "echo \"select database_role from v\\\$database;\" | sqlplus -s / as sysdba";
    

    is executed by

    `su - oracle -c  "echo \\"select database_role from v\\\\\\\$database;\\" | sqlplus -s / as sysdba";`