shell script help
Posted in 1999
A poster's Bourne shell script, which loops reading values and pipes an INSERT to dbaccess via a here-document, failed with "syntax error at line 21: end of file unexpected". Replies identified the cause: the terminating EOF of the here-document was indented, so it must start in column 1 (or use <<- EOF, which strips leading tabs only, not spaces). Other fixes suggested: use test $flag -eq 1 and [ "$answer" = y ] instead of ==, quote char values as '$name', and end the SQL with a semicolon. Art Kagel also suggested Jonathan Leffler's sqlcmd to avoid reconnecting per insert.
Auto-generated by DrWatson from the posts below — may be imperfect; read the full thread.
Topics: Server Administration
I wrote a small shell script(insert.sh) to insert record to a table
(named mytable). mytable has 3 char type columns : name,birth,addr.
mydb is the database name.
insert.sh
******************************************
1 flag=1
2 while test $flag==1
3 do
4 echo type in name:
5 read name
6 echo type in birth:
7 read birth
8 echo type in addr:
9 read addr
10 dbaccess $1 << EOF
11 INSERT INTO mytable
12 VALUES($name,$birth,$addr)
13 EOF
14 echo continue(y/n)?
15 read answer
16 if $answer=='y'
17 then flag=1
18 else flag=0
19 fi
20 done
*****************************************
$ insert.sh mydbinsert.sh: syntax error at line 21: `end of file' unexpected
Any hints and opinior will be appreciated!
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
carina@371.net wrote:
> I wrote a small shell script(insert.sh) to insert record to a table
> (named mytable). mytable has 3 char type columns : name,birth,addr.
> mydb is the database name.
>
> insert.sh
> ******************************************
I think you have some syntax errors.
I'll assume you're using the Bourne shell.
Let me try and correct your text ...
>
> 1 flag=1
> 2 while test $flag -eq 1
# Use '-eq' instead of '==' if using Bourne shell
>
>
> 3 do
> 4 echo type in name:
> 5 read name
> 6 echo type in birth:
> 7 read birth
> 8 echo type in addr:
> 9 read addr
>
> 10 dbaccess $1 <<EOF
# No space between the '<<' and 'EOF'
>
> 11 INSERT INTO mytable
>
> 12 VALUES('$name','$birth','$addr')
# Informix SQL syntax requires literal char string values to be enclosed
# in single quotes.
>
> 13 EOF
# The 'EOF' should not be indented.
> 14 echo continue(y/n)?
> 15 read answer
> 16 if [$answer='y']
# Incorrect syntax for 'if'
> 17 then
> flag=1
> 18 else
> flag=0
> 19 fi
> 20 done
> *****************************************
>
> $ insert.sh mydb> insert.sh: syntax error at line 21: `end of file' unexpected
>
> Any hints and opinior will be appreciated!
>
> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.
HTH,
Avi.
PS Perhaps a look at the 'sh' manual page will help?
carina@371.net wrote:
> I wrote a small shell script(insert.sh) to insert record to a table
> (named mytable). mytable has 3 char type columns : name,birth,addr.
> mydb is the database name.
>
> insert.sh
> ******************************************
> 1 flag=1
> 2 while test $flag==1
> 3 do
> 4 echo type in name:
> 5 read name
> 6 echo type in birth:
> 7 read birth
> 8 echo type in addr:
> 9 read addr
> 10 dbaccess $1 << EOF
> 11 INSERT INTO mytable
> 12 VALUES($name,$birth,$addr)
> 13 EOF
EOF must start in col 1
>
> 14 echo continue(y/n)?
> 15 read answer
> 16 if $answer=='y'
> 17 then flag=1
> 18 else flag=0
> 19 fi
> 20 done
> *****************************************
>
> $ insert.sh mydb> insert.sh: syntax error at line 21: `end of file' unexpected
>
> Any hints and opinior will be appreciated!
>
> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.
--
Helmut Leininger
Bull AG / Vienna
Unix Support
Email: h.leininger@bull.at
helmut.leininger@bull.net
This opinion is mine and not necessarily that of my employer.
No guarantees whatsoever.
On Tue, 08 Jun 1999 09:39:03 +0200, Helmut Leininger
<h.leininger@bull.at> wrote:
>carina@371.net wrote:
>
>> I wrote a small shell script(insert.sh) to insert record to a table
>> (named mytable). mytable has 3 char type columns : name,birth,addr.
>> mydb is the database name.
>>
>> insert.sh
>> ******************************************
>> 1 flag=1
>> 2 while test $flag==1
>> 3 do
>> 4 echo type in name:
>> 5 read name
>> 6 echo type in birth:
>> 7 read birth
>> 8 echo type in addr:
>> 9 read addr
>> 10 dbaccess $1 << EOF
>> 11 INSERT INTO mytable
>> 12 VALUES($name,$birth,$addr)
>> 13 EOF
>
>EOF must start in col 1
Alternatively, if you want to keep the indent, use:
dbaccess <<- EOF
The hyphen makes the here document ignore
leading white space.
Richard.
Hi,
Probably you made an error in the inline editing syntax. You use the EOF
keyword to
determine the start en end postition of the SQL command. The end
position should always be at the beginning of a new line.
See the example:
ls -l
...
while ....
do
....
dbaccess $1 <<EOF
SELECT * FROM customerEOF
....
done
Add a semi-colon to the end of line 11.
Also you may want to get Jonathan Leffler's sqlcmd which can be started
in server mode outside the loop and reads from a FIFO which will be
written to by sqlcmd in client mode. This saves great chunks of time
and will make your script more responsive as only the server process
actually connects to the database while the client only writes the
commands to the FIFO for processing by the server. In this way your
script users will not have to wait while dbaccess connects to the
server and opens the database just for one insert. FWIW.
Art S. Kagel
carina@371.net wrote:
>
> I wrote a small shell script(insert.sh) to insert record to a table
> (named mytable). mytable has 3 char type columns : name,birth,addr.
> mydb is the database name.
>
> insert.sh
> ******************************************
> 1 flag=1
> 2 while test $flag==1
> 3 do
> 4 echo type in name:
> 5 read name
> 6 echo type in birth:
> 7 read birth
> 8 echo type in addr:
> 9 read addr
> 10 dbaccess $1 << EOF
> 11 INSERT INTO mytable
> 12 VALUES($name,$birth,$addr)
> 13 EOF
> 14 echo continue(y/n)?
> 15 read answer
> 16 if $answer=='y'
> 17 then flag=1
> 18 else flag=0
> 19 fi
> 20 done
> *****************************************
>
> $ insert.sh mydb> insert.sh: syntax error at line 21: `end of file' unexpected
>
> Any hints and opinior will be appreciated!
>
> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.
Richard Thomas wrote:
>
> On Tue, 08 Jun 1999 09:39:03 +0200, Helmut Leininger
> <h.leininger@bull.at> wrote:
>
> >carina@371.net wrote:
> >
> >> I wrote a small shell script(insert.sh) to insert record to a table
> >> (named mytable). mytable has 3 char type columns : name,birth,addr.
[SNIP]
> >> 10 dbaccess $1 << EOF
> >> 11 INSERT INTO mytable
> >> 12 VALUES($name,$birth,$addr)
> >> 13 EOF
> >
> >EOF must start in col 1
>
> Alternatively, if you want to keep the indent, use:
>
> dbaccess <<- EOF
Ouch I missed that.
Art S. Kagel
On Tue, 08 Jun 1999 17:43:31 -0400, "Art S. Kagel"
<kagel@bloomberg.net> wrote:
>Richard Thomas wrote:
<snip>
>> >EOF must start in col 1
>>
>> Alternatively, if you want to keep the indent, use:
>>
>> dbaccess <<- EOF
>
>Ouch I missed that.
But I didn't touch him, honest, Mrs Kagel!! ;-)
Richard.
In article <375cf577.12149924@nflintra>,
richard@ starjump.org (Richard Thomas) wrote:
[...]
> Alternatively, if you want to keep the indent, use:
>
> dbaccess <<- EOF
>
> The hyphen makes the here document ignore
> leading white space.
Richard,
it could be very nice feature. If it worked :(. I just tested it with sh
and ksh - doesn't work. Which shell are you using?
Alex.
--
Before the accident, I even could not spell UNIX
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
Alex Barilo wrote:
>
> In article <375cf577.12149924@nflintra>,
> richard@ starjump.org (Richard Thomas) wrote:
> [...]
> > Alternatively, if you want to keep the indent, use:
> >
> > dbaccess <<- EOF
> >
> > The hyphen makes the here document ignore
> > leading white space.
>
> Richard,
>
> it could be very nice feature. If it worked :(. I just tested it with sh
> and ksh - doesn't work. Which shell are you using?
Always works for me in ksh if the indent is tabs not spaces. Try the
following:
PS1>cat <<-EOF
PS2><tab>HELLO DOLLY
PS2>,tab>Well Hello Dolly
PS2><tab><tab><tab>EOF
HELLO DOLLY
Well Hello Dolly
PS1>
Art S. Kagel
On Thu, 10 Jun 1999 13:33:37 GMT, Alex Barilo <alex.barilo@phila.gov>
wrote:
>In article <375cf577.12149924@nflintra>,
> richard@ starjump.org (Richard Thomas) wrote:
>[...]
>> Alternatively, if you want to keep the indent, use:
>>
>> dbaccess <<- EOF
>>
>> The hyphen makes the here document ignore
>> leading white space.
>
>Richard,
>
>it could be very nice feature. If it worked :(. I just tested it with sh
>and ksh - doesn't work. Which shell are you using?
>
Alex,
/bin/sh on SCO OpenServer 5.0.4 - been using this feature for
a couple of years, and just tested it to make sure :-)
From the man page for sh:
<<[ - ]word
Read the shell input up to a line that is the same as word,
or to an
end-of-file. The resulting document becomes the standard
input. If
any character of word is quoted, no interpretation is placed
upon
the characters of the document; otherwise, parameter and
command
substitution occurs, (unescaped) \\newline is ignored, and
``\\''
must be used to quote the characters \\, $, `, and the first
character of word. If ``-'' is appended to <<, all leading
tabs are
stripped from word and from the document.
Richard.
On Thu, 10 Jun 1999 16:07:19 -0400, "Art S. Kagel"
<kagel@bloomberg.net> wrote:
>
>
>Alex Barilo wrote:
>>
>> In article <375cf577.12149924@nflintra>,
>> richard@ starjump.org (Richard Thomas) wrote:
>> [...]
>> > Alternatively, if you want to keep the indent, use:
>> >
>> > dbaccess <<- EOF
>> >
>> > The hyphen makes the here document ignore
>> > leading white space.
>>
>> Richard,
>>
>> it could be very nice feature. If it worked :(. I just tested it with sh
>> and ksh - doesn't work. Which shell are you using?
>
>Always works for me in ksh if the indent is tabs not spaces. Try the
>following:
>
>PS1>cat <<-EOF
>PS2><tab>HELLO DOLLY
>PS2>,tab>Well Hello Dolly
>PS2><tab><tab><tab>EOF
>HELLO DOLLY
>Well Hello Dolly
>PS1>
>
I must admit I've probably always used tab characters to indent -
maybe that's why Alex couldn't get it to work?
Yep, just test it with spaces, and it failed :-(
Richard.