如何从 sqlplus 获取 SQL 查询输出到 bash 脚本

how to get SQL query output from sqlplus to bash script

如何使用 sqlplus 作为中间人执行 sql 查询或脚本并将输出插入到 bash 变量?

(至少)3 个好看的 stack-overflow 和其他网站教程对我没有任何用处,所以我发布对我有用的内容,希望它对其他人有用。

代码:

#!/bin/bash
export ORACLE_SID=<DB_SID>
VAL=""
VAL=`sqlplus -s / as sysdba << EOF
whenever sqlerror exit sql.sqlcode;
set echo off
set heading off
@<sql_script_path>
exit;
EOF`
echo $VAL