55102

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: `)'

Answer1:

Your function declaration is wrong:

get_instance{

should be one of

function get_instance { get_instance() { <hr>

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

Recommend

  • ZipList with Scalaz
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • Installing Perl6 and Panda on Ubuntu 15.10. Problems with bootstrap.pl
  • Can my PDF ping my server when it is opened?
  • Switch to popup in python using selenium
  • Do query loads all the data in memory
  • Who propagate bugfixes across branches (corporate development)?
  • Tomcat memory Leak
  • Question about instantiating object
  • PLSQL: Get number of records updated vs inserted when a merge statement is used
  • Web.config system.webserver errors
  • ADO and msqli connections very slow
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • Problems compiling files using JOGL
  • Retrieve list of sent friend requests from friend_request FQL table
  • HTTP/2 streams vs HTTP/1.1 connections
  • Email verification using google app script and google forms
  • Android full screen on only one activity?
  • Python urlparse: small issue
  • NHibernate Validation Localization with S#arp Architecture
  • How can I send an e-mail from a vbs script
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • Accessing IRQ description array within a module and displaying action names
  • Fill an image in a square container while keeping aspect ratio
  • JSON with duplicate key names losing information when parsed
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • Jquery - Jquery Wysiwyg return html as a string
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Getting Messege Twice Using IMvxMessenger
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Reading document lines to the user (python)
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?