Help need with a Shell Function

Help need with a Shell Function

Post by Greg Hine » Wed, 17 Apr 2002 12:46:27



Hi all,

I'm new to writing scripts and I want to create a Shell Function within a
script as follows:-

recno()
{
        SQL << DONE > /dev/null 2>&1
                lines 0
                select count(*) from TablStat
                where TableID=$1
                into $TMPFILE
                /
        DONE
        <$TMPFILE read para1
        para1=`expr $para1`
        /bin/rm/ -f $TMPFILE

Quote:}

The above produces the error "syntax error at line 28 : `{' unmatched" when
the script is run as it does not understand the SQL statements.

If anyone can help with the problem it would be appreciated.

Thanks,
--

Remove NOSPAM when replying

 
 
 

Help need with a Shell Function

Post by David Thompso » Wed, 17 Apr 2002 14:36:57



Quote:> I'm new to writing scripts and I want to create
> a Shell Function within a script as follows:-

> recno()
> {
>         SQL << DONE > /dev/null 2>&1
>                 lines 0
>                 select count(*) from TablStat
>                 where TableID=$1
>                 into $TMPFILE
>                 /
>         DONE
>         <$TMPFILE read para1

          ^^^^^^^^^^^^^^^^^^^^
Woah!  This is cool!  I'd never seen this before.
At first I thought this was an error, but it works!

Quote:>         para1=`expr $para1`
>         /bin/rm/ -f $TMPFILE
> }

When using here documents, put your end marker in
column 1.  Ie,

  recno()
  {
          SQL << DONE > /dev/null 2>&1
          [snip]
  DONE
  }

The shell wants the end marker literally on a line
all by itself, spaces included.  So, if you say,

  cat << "  EOF"
  hello world
    EOF

this works!

Note how I have 2 spaces in front of EOF on the
trailing marker line.  The shell identifies the
trailing marker line based upon a line by line
EXACT comparison with the marker after the <<.

The shell wants an EXACT match; so leading spaces
and trailing spaces are VERY significant on the
line containing your end marker.

Quote:> The above produces the error
> "syntax error at line 28 : `{' unmatched"
> when the script is run as it does not understand
> the SQL statements.

In your example snippet, the end marker is not
found, so as far as shell is concerned the { is
never closed, and thus your error.

--
David Thompson


 
 
 

Help need with a Shell Function

Post by David Thompso » Wed, 17 Apr 2002 16:34:49



[snip]

Sorry, after reading my reply, I realized the
form <<- might be useful to you.  But, beware,
it requires using actual <TAB> characters.

I don't use this form because I use vim's
expandtab option to convert tabs to spaces.

Quote:> recno()
> {
>         SQL << DONE > /dev/null 2>&1

              ^^
There is another way to do this, use <<-, ie,
append - after the <<, this allows tabs to be
used in in front of DONE below.

Quote:>                 lines 0
>                 select count(*) from TablStat
>                 where TableID=$1
>                 into $TMPFILE
>                 /
>         DONE

 ^^^^^^^^^
These must be a tab characters.

Quote:>         <$TMPFILE read para1
>         para1=`expr $para1`
>         /bin/rm/ -f $TMPFILE
> }

man sh
man ksh
man bash

--
David Thompson

 
 
 

Help need with a Shell Function

Post by Greg Hine » Thu, 18 Apr 2002 12:02:15


David,

Thanks very much for your help it was greatly appreciated and very helpful.

Thanks,
Greg
--

Remove NOSPAM when replying


> [snip]

> Sorry, after reading my reply, I realized the
> form <<- might be useful to you.  But, beware,
> it requires using actual <TAB> characters.

> I don't use this form because I use vim's
> expandtab option to convert tabs to spaces.

> > recno()
> > {
> >         SQL << DONE > /dev/null 2>&1
>               ^^
> There is another way to do this, use <<-, ie,
> append - after the <<, this allows tabs to be
> used in in front of DONE below.

> >                 lines 0
> >                 select count(*) from TablStat
> >                 where TableID=$1
> >                 into $TMPFILE
> >                 /
> >         DONE
>  ^^^^^^^^^
> These must be a tab characters.

> >         <$TMPFILE read para1
> >         para1=`expr $para1`
> >         /bin/rm/ -f $TMPFILE
> > }

> man sh
> man ksh
> man bash

> --
> David Thompson


 
 
 

1. Need HELP with functions in SH shell programming!


Try this little script and if it doesn't work mail me back (to save bandwidth)

========= 8<  cut here
#!/bin/sh
name() {
    echo hi
name

======

This,  when  run should echo hi.  If it works,  then take it from there. :)

ttfn,
        Mike

--
_______________________________________________________________________________
 ___                    "Mickey Mouse wears a timex watch."_ ???

 C|S                    University of Toronto Computing Services

2. Openwin/Xview 3.2 on Xfree86 3.1 Linux

3. need a little help with implementing a shell function

4. ld.so 1.3 -- what the heck is it?

5. User chsh shell change function needed for Solaris

6. Help me to install Linux with old BIOS and 850M HD

7. Beginner Shell programmer needs HELP (belonged on comp.unix.shell)

8. Floating point performance of UNIX workstations.

9. Korn shell help with remote shell needed!

10. shell functions don't work the same when opening sub-shells

11. finding/execing a shell with shell functions?

12. Invoking shell function, shell script or alias with only partial match?

13. Functions for JavaScript function are needed