在bash,里面函数返回定界符语法错误 [英] in bash, heredoc inside function returns syntax error
问题描述
我有以下功能:
#!/斌/庆典get_instance {
DBNAME = $(的sqlplus -s / as sysdba已与LT;< EOF
0设定页面
设置反馈关闭
选择从V \\ $数据库名;
出口;
EOF)回声$ DBNAME}get_instance
这似乎工作。在错误信息的中间,我得到我的DBNAME,但仍然会返回一个语法错误。
甲骨文@ testdb01:DB01:/家庭/ ORACLE /
./ test.sh
./test.sh:3号线:get_instance {:命令未找到
DB01
./test.sh:第11行:附近意外的标记语法错误}
}
./test.sh:第11行:
块引用>如果我完全删除函数调用,我得到没有错误的结果:
DBNAME = $(的sqlplus -s / as sysdba已与LT;< EOF
0设定页面
设置反馈关闭
选择从V \\ $数据库名;
出口;
EOF)
回声$ DBNAME甲骨文@ testdb01:DB01:/家庭/甲骨文
> ./test.sh
DB01什么我需要做的就是这一个功能工作?
编辑:
以下建议放置支架EOF标记之后,添加功能关键字:
六test.sh
test.sh12行,160字
!/斌/庆典
块引用>功能get_instance {
DBNAME = $(的sqlplus -s / as sysdba已与LT;< EOF
0设定页面
设置反馈关闭
选择从V \\ $数据库名;
出口;
EOF
)
回声$ DBNAME
}get_instance甲骨文@ testdb01:DB01:/家庭/甲骨文
> ./test.sh
./test.sh:第10行:附近意外的标记语法错误'DBNAME = $(的sqlplus -s / as sysdba已与LT;< EOF
0设定页面
设置反馈关闭
选择从V \\ $数据库名;
出口;
EOF
)./ test.sh:第10行:')
解决方案您函数的声明是错误的:
get_instance {
应该是一个
功能get_instance {
get_instance(){把接近支架在另一条线路:
DBNAME = $(的sqlplus -s / as sysdba已与LT;< EOF
...
EOF
)在定界符的终止词应该是上线的唯一字符(使用时除外标签
<< -
)。演示:$ X = $(猫<< END
>一
>二
>结束)
庆典:警告:在5号线在此文档由档案结尾分隔(想'END')
$回声$ X
一
二所以它的工作,不慎。更好的做法是:
$ Y = $(猫<< END
> 1
> 2
>结束
> )
$回声$ Y
1
2I have the following function:
#!/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
It seems to work. In the middle of the error message, I get my dbname, but still returns a syntax error.
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:
}'If I remove the function call completely, I get the result with no errors:
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
What do I need to do to get this to work in a function?
EDIT:
Following suggestion to place bracket after EOF tag and add function keyword:
vi test.sh "test.sh" 12 lines, 160 characters
!/bin/bash
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: line 10: `)'
解决方案Your function declaration is wrong:
get_instance{
should be one of
function get_instance { get_instance() {
Put the close bracket on a different line:
dbname=$(sqlplus -s / as sysdba<<EOF ... EOF )
The terminating word of the heredoc should be the only characters on the line (except tabs when using
<<-
). Demo:$ x=$(cat <<END > one > two > END) bash: warning: here-document at line 5 delimited by end-of-file (wanted `END') $ echo "$x" one two
So it worked, accidentally. Better practice is:
$ y=$(cat <<END > 1 > 2 > END > ) $ echo "$y" 1 2
这篇关于在bash,里面函数返回定界符语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!