如何在 SQLPLUS 中使用 Bash 中的变量?
How to use a variable from Bash into SQLPLUS?
我正在尝试编写一个脚本,该脚本将使用 BASH(使用基本读取命令)接受客户 ID,然后我想在我的 SQLPLUS 查询中使用该 BASH 变量。我怎样才能做到这一点 ?我正在尝试使用以下格式,但它不起作用。
echo "Enter Customer ID :- ";
read Customer
sqlplus username\password@host
select first_name from customer where customer_id = $Customer;
quit
exit
通常,您会这样做:
echo "select first_name from customer where customer_id = $Customer;" | sqlplus username\password@host
如果要运行多个查询,通常使用heredoc:
cat << EOF | sqlplus username\password@host
select first_name from customer where customer_id = $Customer;
select first_name from customer where customer_id = $Customer;
EOF
针对评论中的查询进行了编辑:
要将任何命令的结果存储在变量中,您可以使用进程替换。 var=$( cmd )
。在 heredoc 的情况下,语法是:
var=$( cat << EOF | sql...
query
query
EOF
)
我正在尝试编写一个脚本,该脚本将使用 BASH(使用基本读取命令)接受客户 ID,然后我想在我的 SQLPLUS 查询中使用该 BASH 变量。我怎样才能做到这一点 ?我正在尝试使用以下格式,但它不起作用。
echo "Enter Customer ID :- ";
read Customer
sqlplus username\password@host
select first_name from customer where customer_id = $Customer;
quit
exit
通常,您会这样做:
echo "select first_name from customer where customer_id = $Customer;" | sqlplus username\password@host
如果要运行多个查询,通常使用heredoc:
cat << EOF | sqlplus username\password@host
select first_name from customer where customer_id = $Customer;
select first_name from customer where customer_id = $Customer;
EOF
针对评论中的查询进行了编辑:
要将任何命令的结果存储在变量中,您可以使用进程替换。 var=$( cmd )
。在 heredoc 的情况下,语法是:
var=$( cat << EOF | sql...
query
query
EOF
)