Re: A couple of scripting queries
From: Tony Lawrence (foo_at_pcunix.com)
Date: 03/24/05
- Previous message: Simon Hobson: "A couple of scripting queries"
- In reply to: Simon Hobson: "A couple of scripting queries"
- Next in thread: Tony Lawrence: "Re: A couple of scripting queries"
- Reply: Tony Lawrence: "Re: A couple of scripting queries"
- Reply: brian_at_aljex.com: "Re: A couple of scripting queries"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 24 Mar 2005 14:11:57 -0500
Simon Hobson wrote:
> I've been writing a script at work, mostly using Informix to extract some
> data from the sales system - and I've come up with a couple of problems that
> are new to me. Yes I'm sure they are bot a doddle in Perl - but I haven't
> learned Perl (yet). This all runs in sh, not ksh, and will be executed
> (indirectly) from cron.
>
>
>
> 1) 'double substitution' of variables
>
> I need to repeat the same operation for the next 11 months, so I calculated
> the start and end dates and stored them in variables S1 to S11 and E1 to E11
> - thinking that I could then do something along the lines of :
>
> for month in 1 2 3 4 5 6 7 8 9 10 11
> do
> echo "select <something> from <something>
> where date_required between \"${Sn}\" and \"${En}\" " \
> | dbaccess data -
> done
>
> But then I realised that ${S${month}} doesn't work !
>
> In the end I did this :
>
> for month in 1 2 3 4 5 6 7 8 9 10 11
> do
> start=`echo 'echo "${S'${month}'}"' | /bin/sh`
> end=`echo 'echo "${E'${month}'}"' | /bin/sh`
> echo "select <something> from <something>
> where date_required between \"${start}\" and \"${end}\" " \
> | dbaccess data -
> done
>
> I'm sure there must be a better way than start=`echo 'echo "${S'${month}'}"'
> | /bin/sh` to do this - any ideas ?
>
>
>
> 2) date calculations
>
> I suspect some of you might have seen this part coming ...
>
> In the general case I need to process a whole pile of stuff for yesterdays
> date (because the script will be run early in the morning). So I need to get
> yesterdays date, and then the start and end dates for that month and the
> following 11 months.
>
> At the moment I've done this with a combination of some rather unpretty
> script and informix scripts (which I'm too embarassed to quite here). Again
> I'm sure there'll be a better way of doing it - any ideas ?
>
>
>
> TIA, Simon
>
You can get yesterday's date as simply as
date --date='1 day ago'
If you want more complicated things, it's harder to do in the shell (and
usually no reason to); see http://aplawrence.com/Unix/yesterday.html and
for an example of doing it in perl see
http://aplawrence.com/Unix/perlnetftp.html
-- Tony Lawrence Unix/Linux/Mac OS X resources: http://aplawrence.com
- Previous message: Simon Hobson: "A couple of scripting queries"
- In reply to: Simon Hobson: "A couple of scripting queries"
- Next in thread: Tony Lawrence: "Re: A couple of scripting queries"
- Reply: Tony Lawrence: "Re: A couple of scripting queries"
- Reply: brian_at_aljex.com: "Re: A couple of scripting queries"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|