简体   繁体   中英

Unable to escape dollar sign in ruby_block chef resource

I'm writing a chef recipe wherein an sql command is run.

ruby_block 'SQL command' do
  block do
    report = open(ReportFile,'a')
    command = %Q( ssh -o StrictHostKeyChecking=no root@#{dbHost2} 'su - #{oraSidUser} -c "#{oracleHome}/bin/sqlplus / as sysdba <<EOF
SELECT FLASHBACK_ON FROM V$DATABASE;
EOF"' )
    commandObj = Mixlib::ShellOut.new(command, :timeout => 60)
    commandObj.run_command()
    report.puts("Command:#{command}\nOutput:#{commandObj.stdout}\nError:#{commandObj.stderr}\nExit code:#{commandObj.exitstatus}")
  end
end

But when I check the log, it does not run the SQL command. It runs,

SQL> SELECT FLASHBACK_ON FROM V
                         *
ERROR at line 1:
ORA-00942: table or view does not exist

Seems it is trying to put the value of $DATABASE. I've tried using backslash to escape $. I've used double dollar sign. I've tried putting the SQL in a variable but it still does not run the correct SQL. Is there a different way to escape the dollar sign?

Edit: With double backslash or triple backslash it fails with,

Error:DATABASE: Undefined variable.

since the command is a bash command, you need to escape the $ sign, which is being evaluated in the shell as a variable.

append double backslash before the $ , that is \\\\$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM