Error with EOF when used in function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error with EOF when used in function
# 1  
Old 03-28-2012
Error with EOF when used in function

Hi all

I am inserting record in oracle database using shell script. When the sql part is directly written in the shell script the record is getting inserted. But when the same piece of code is added into a function, I am getting an error while execution "line 45: syntax error: unexpected end of file".

The file is containing only 44 lines of code.

The function which is called from a main function in which the code for insert is written is as follows. When the entire SQL section is removed from the function the program executes properly.

Any suggestions/inputs would be great help. Smilie

TIA

Code:
add () 
{
                    sqlplus -s "custom/custom"<<EOF
insert into name_list values ('$f_name','$l_name');
exit
EOF
 
if [ $? -ne 0 ]; then 
echo "Error in insertion"
add;
             else 
echo -e "\nRecord added successfully... Do you wish to add more(y/n) \c"
read choice
 
if [ $choice == "y" ]; then
add ;
else 
clear
main ;
fi
fi
}


Last edited by methyl; 03-28-2012 at 11:47 AM.. Reason: please use code tags
# 2  
Old 03-28-2012
Misread, sorry, ignore the post.
# 3  
Old 03-28-2012
When you get an error message, please post the error message verbatim.

That error message type usually means that there are extra characters (e.g. space characters) on the same line as the terminating EOF, or if that string EOF is in any way indented.
It can be caused if the script has been edited with a Microsoft editor such as notepad which introduces spurious carriage-return characters.

The script segment as posted is sort of sound but the logic and structure is dubious because the functions can call themselves. We need to see the whole script.
# 4  
Old 03-28-2012
Quote:
Originally Posted by methyl
When you get an error message, please post the error message verbatim.

That error message type usually means that there are extra characters (e.g. space characters) on the same line as the terminating EOF, or if that string EOF is in any way indented.
It can be caused if the script has been edited with a Microsoft editor such as notepad which introduces spurious carriage-return characters.

The script segment as posted is sort of sound but the logic and structure is dubious because the functions can call themselves. We need to see the whole script.
The error message i have written is precise. As i stated the same section of code (sqlplus ... << EOF to EOF) when used as an independent script is inserting values and if this is section is removed from the script the execution is successful.

Following is the entire script

add ()
{
sqlplus -s "custom/custom"<<EOF
insert into name_list values ('$f_name','$l_name');
exit
EOF

if [ $? -ne 0 ]; then
echo "Error in insertion"
add;
else
echo -e "\nRecord added successfully... Do you wish to add more(y/n) \c"
read choice

if [ $choice == "y" ]; then
add ;
else
clear
main ;
fi
fi
}

function main
{
echo -e "\t----------- database"
echo -e "\t****************"
echo -e "\n"
echo -e "1) Add"
echo -e "2) View"
echo -e "3) Delete"
echo -e "4) Exit"
echo -e "\nEnter your choice: \c"
read choice
case $choice in
1) add ;;
*) echo "awaited" ;;
esac
}
main ;
# 5  
Old 03-28-2012
The "EOF" ending your here document should be at the very front of the line. I suspect the shell never finds the end of your here document as a result of it being indented.

P.S. consider using the "select" construct for creating a menu. Also I don't think you are gaining anything by putting main into a function. If your intention is to put it in a while loop eventually to repeat the menu, have a look here it may help you:
https://www.unix.com/unix-dummies-que...#post302606268

Last edited by gary_w; 03-28-2012 at 12:52 PM..
This User Gave Thanks to gary_w For This Post:
# 6  
Old 03-28-2012
Remove the whitespace before 'EOF'.
This User Gave Thanks to CarloM For This Post:
# 7  
Old 03-28-2012
Quote:
Originally Posted by CarloM
Remove the whitespace before 'EOF'.

Thanks... The whitespaces before EOF were the problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to resolve unexpected EOF while looking for matching `"' error

Hi, can someone kindly look into my copy script and figure out why am i getting a "unexpected EOF while looking for matching `"' error message #!/bin/ksh -x cd /home/goldenga/test/flag37 if ; then rm copied.ok cd /home/goldenga/test Upper=`ls -t|grep 'qw*'|cut -d "w" -f 2|head... (4 Replies)
Discussion started by: NDalal007
4 Replies

2. Shell Programming and Scripting

EOF Usage - line 56: syntax error: unexpected end of file

Below is a test script I'm writing in the process of learning to write script. When I try to run it I get an unexpected end of file error on line 56. Thoughts? SCRIPT: #!/bin/bash # system_page - A script to produce a system information HTML file ##### Constants TITLE="My System... (1 Reply)
Discussion started by: mpercy725
1 Replies

3. UNIX for Advanced & Expert Users

tar: write error: unexpected EOF

tar: write error: unexpected EOF I usually encounter this when I'm doing backup to Dat72 tape. Here's the command gzip all files in a directory then tar cvfp /dev/rmt/0n files. Not all files are copied. Any things I should look at? Directory size? ---------- Post updated at 05:20 PM... (1 Reply)
Discussion started by: lhareigh890
1 Replies

4. Shell Programming and Scripting

[Solved] <<EOF error checking

I am trying to check the return status of a command thats encapsulated in an <<EOF #!/bin/ksh set -x xxx() { set -x ls -lt /tmp/jdlkewjdlkewjdlkewjdlewjdlew<<EOT ret=$? EOT echo "ret=$ret" } >>$LOG 2>&1 LOG=/tmp/t2.out rm -rf $LOG echo "Starting process `date... (2 Replies)
Discussion started by: BeefStu
2 Replies

5. Shell Programming and Scripting

help pls getting EOF error

#!/bin/bash while : do clear echo ""***************************************" echo "* - MAIN - MENU - *" echo "***************************************" echo "* 1. Directory Listing *" echo "* 2. Users Currently Logged In *" echo "* 3. Create a... (2 Replies)
Discussion started by: celtic123
2 Replies

6. UNIX for Dummies Questions & Answers

Unclosed EOF (error)

I have a script which throws an error: "here document `EOF' unclosed" The script is: ... main() { RETVAL=`sqlplus -s $ConnectDetails << EOF SET SERVEROUTPUT ON DECLARE errnum NUMBER; errmsg VARCHAR2(1000); BEGIN My_Procedure( errbuf => errmsg, errcode... (2 Replies)
Discussion started by: laiko
2 Replies

7. UNIX for Advanced & Expert Users

tar:read error: unexpected EOF

I need archive data from some tar files. I use the command "tar xvf filename". For about half of the files I get the error message "tar:read error: unexpected EOF". The number of bytes extracted comes very close to the size of the tar file. An example: 972434779 Jan 10 01:01... (4 Replies)
Discussion started by: mikeruth
4 Replies

8. Shell Programming and Scripting

Unexpected eof error

Hi, I am newbie and am just trying to connect to oracle from shell script ,,,but I am getting the following error ./prog.sh: line 20: syntax error: unexpected end of file The scripts is : #!/bin/bash O=$IFS; IFS=","; while read a b c d do echo $c ... (6 Replies)
Discussion started by: thana
6 Replies

9. UNIX for Dummies Questions & Answers

tar: write error: unexpected EOF

I am taring to disk and get this message: tar: write error: unexpected EOF This message is normally associated with tape devices, not disk. Here is the syntax: tar -cf <tarfilename> /dir Have you seen this message on disk tars? (4 Replies)
Discussion started by: jabe
4 Replies

10. Shell Programming and Scripting

tar: write error: unexpected EOF

Hi friends, I am using Sun Solaris 5.9. I want to take backup of 3 folders which are 50 GB in size totally using tar command on tapes. I am having DAT 72 tape. After initiating tar command (tar -cvf /dev/rmt/0n /tmp/dir1/ /tmp/dir2/ /tmp/dir3/), tarring is stopped after 10 hrs (approx) and i am... (7 Replies)
Discussion started by: vijayakumar.pc
7 Replies
Login or Register to Ask a Question