Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
632 views
in Technique[技术] by (71.8m points)

variables - in bash, heredoc inside function returns syntax error

I 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
# 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: line 10: `)'

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...