[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: help with bash script



On Fri, Jan 22, 1999 at 03:18:40PM +0200, Johann Spies wrote:
> Hallo,
> 
> I am just starting to use small bash scripts on my system and I enjoy it.
> 
> I am using the following bash script to query my postgresql database using
> "van" (surname) as search field:
> 
> time echo "select noemnaam,van,adres1,telefoon,werktelefoon,selfoon,faks
> from lidmate,besoekpunt where lower(van) like lower('$1%') and bpnommer =
> besoekpunt.nommer order by van, noemnaam;" | psql jhspies
> 
> I use it like this:
> 
> telef marais
> 
> When I want to narrow the search I use it in the following way:
> 
> telef marais | grep -i gert
> 
> supplying a name (gert) to look for.
> 
> I want to modify my script so that I can do either
> 
> telef marais 
> or
> telef marais gert
> 
> What I do not know is how to handle the possible second argument on the
> command line.  The script must be able to use the second argument if there
> is one otherwise it should use the query as it is at the moment.

Hi Johann,

I'm no shell expert by perhaps something like this:

#!/bin/sh

ROWS="noemnaam,van,adres1,telefoon,werktelefoon,selfoon,faks"
TABLES="lidmate,besoekpunt"
CONSTRAINTS="lower(van) like lower('$1%') and bpnommer = \
besoekpunt.nommer"

if [ ! -z "$2" ]
then
  CONSTRAINTS="$CONSTRAINTS and firstname=lower('$2%')"
fi

ORDER="van, noemnaam"

time echo "select $ROWS from $TABLES where $CONSTRAINTS \
           order by $ORDER;" | psql $LOGNAME

Not tested of course :)

-- 
Rafael Kitover
rkitover@io.com

Attachment: pgpPQPeVGmDYT.pgp
Description: PGP signature


Reply to: