【发布时间】:2015-03-14 08:12:36
【问题描述】:
我有以下功能:
#!/bin/bash
get_instance{
dbname=$(sqlplus -s / as sysdba<<EOF
set pages 0
set feedback off
select name from v\$database;
exit;
EOF)
echo $dbname
}
get_instance
它似乎工作。在错误消息的中间,我得到了我的dbname,但仍然返回一个语法错误。
oracle@testdb01:db01:/home/oracle/
> ./test.sh
./test.sh: line 3: get_instance{: command not found
DB01
./test.sh: line 11: syntax error near unexpected token `}'
./test.sh: line 11: `}'
如果我完全删除函数调用,我会得到没有错误的结果:
dbname=$(sqlplus -s / as sysdba<<EOF
set pages 0
set feedback off
select name from v\$database;
exit;
EOF)
echo $dbname
oracle@testdb01:db01:/home/oracle
> ./test.sh
DB01
我需要做什么才能让它在函数中工作?
编辑:
以下建议在 EOF 标记后放置括号并添加函数关键字:
> vi test.sh
"test.sh" 12 lines, 160 characters
#!/bin/bash
# updated file
function get_instance{
dbname=$(sqlplus -s / as sysdba<<EOF
set pages 0
set feedback off
select name from v\$database;
exit;
EOF
)
echo $dbname
}
get_instance
oracle@testdb01:db01:/home/oracle
> ./test.sh
./test.sh: line 10: syntax error near unexpected token `dbname=$(sqlplus -s / as sysdba<<EOF
set pages 0
set feedback off
select name from v\$database;
exit;
EOF
)'
./test.sh: 第 10 行:`)'
【问题讨论】:
-
现在,我可能弄错了,但是 heredoc 不使用三个
<s 吗? -
EOF应该单独一行,在)之前 -
@MightyPork 没有。它是 2
-
@MightyPork 不,那是别的东西(
cmd <<< "abc"可以在 bash 中用作echo "abc" | cmd的替代品) -
是的,我从 PHP 中知道,所以我认为这将是标准语法,显然不是
标签: bash variables sqlplus heredoc solaris-10