[ILUG] Bash script misbehaving

paul at clubi.ie paul at clubi.ie
Fri Aug 8 10:47:34 IST 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On Thu, 7 Aug 2008, Walter Faleiro wrote:

> All,
> We have a script to cleanup filesystem.
> It contains the following lines in the code
>
> #!/bin/bash
>
> DAYSPERMONTH=("31" "29" "31" "30" "31" "30" "31" "31" "30" "31" "30" "31" )
> CDSTAMP=`date '+%Y%m%d'`
>
> bash-3.00# date '+%Y%m%d'
> 20080807
>
>
> CURRENTYEAR=`echo $CDSTAMP | awk '{print substr($1,1,4)}'`

If you're using GAWK (almost guaranteed if its a Linux server, and 
GAWK is easily available for most other platforms), the sub-command 
and echo are quite redundant, instead:

 	gawk 'BEGIN {print strftime("%Y%m%d") }'

It should be obvious how you can make it print the various 
sub-strings you want (man strftime).

In order to guarantee you manipulate only one date, you may wish to 
operate on a fixed timestamp:

 	gawk 'BEGIN {ts = systime(); print strftime("%Y%m%d", ts) }'

regards,
- -- 
Paul Jakma	paul at clubi.ie	paul at jakma.org	Key ID: 64A2FF6A
Fortune:
Weiner's Law of Libraries:
 	There are no answers, only cross references.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iHUEARECADUFAkicFjYuGmh0dHA6Ly93d3cuamFrbWEub3JnL35wYXVsL3BncF9w
b2xpY3ktMS4xLnR4dAAKCRDYa/eUZKL/arwHAKCpacgr5fyGDD9dSOGtMvJx+0g0
pgCdFg5xmEDks8RwfKVBf/KhBqNetq0=
=YQbx
-----END PGP SIGNATURE-----



More information about the ILUG mailing list