comp.unix.shell FAQ - Answers to Frequently Asked Questions Appendix A and B

comp.unix.shell FAQ - Answers to Frequently Asked Questions Appendix A and B

Post by j.p. » Wed, 29 Jun 2005 11:47:50


rchive-name: unix-faq/shell/sh
Posting-Frequency: monthly
Version: $Id: cus-faq-appendix.html,v 1.2 2005/05/21 19:15:57 jhalpin Exp $
Maintainer: Joe Halpin

Please read the introduction in the first section of this document.
This section assumes you have read that introduction.

======================================================================

Appendix A: Examples

Web sites:
-------------------------------------------

Heiner Steven's Shelldorado site has quite a few example scripts,
tutorials, and links to other such places.

http://www.shelldorado.com/

Arbitrary Date Arithmetic
-------------------------------------------

From: Tapani Tarvainen ( XXXX@XXXXX.COM )
Subject: Re: yesterday's date under the shell
View: Complete Thread (8 articles)
Original Format
Newsgroups: comp.unix.shell
Date: 2002-02-12 07:45:05 PST

"Jean-No?l" < XXXX@XXXXX.COM > writes:

> To determine the yesterday date i do it so:
> TZ=PST+24 date +%d
> it work well but my question is:
> does this work on all systems and all shells
> or should i do it otherwise ???

No, it does not work on all systems at all times.
In some it will work practically always,
on others never, on most it works sometimes
and sometimes not. I would recommend against it.

Unfortunately there is no short and sweet portable
solution. If you have or can install Gnu date it
will do it cleanly, otherwise you can find a number
of solutions posted in this group in the past.
For a general solution you could try the following,
which should work with POSIXy shells (I've only tested
it with HP's which is essentially ksh88, though):

#! /usr/bin/sh

# Date calculations using POSIX shell
# Tapani Tarvainen July 1998, February 2001 (POSIXified)
# This code is in the public domain.

# Julian Day Number from calendar date
date2julian() # day month year
{
day=$1; month=$2; year=$3
tmpmonth=$((12 * year + month - 3))
tmpyear=$((tmpmonth / 12))
print $(( (734 * tmpmonth + 15) / 24 - 2 * tmpyear + \
tmpyear/4 - tmpyear/100 + tmpyear/400 + day + 1721119 ))
}

# Calendar date from Julian Day Number
julian2date() # julianday
{
tmpday=$(($1 - 1721119))
centuries=$(( (4 * tmpday - 1) / 146097))
tmpday=$((tmpday + centuries - centuries/4))
year=$(( (4 * tmpday - 1) / 1461))
tmpday=$((tmpday - (1461 * year) / 4))
month=$(( (10 * tmpday - 5) / 306))
day=$((tmpday - (306 * month + 5) / 10))
month=$((month + 2))
year=$((year + month/12))
month=$((month % 12 + 1))
print $day $month $year
}

# Day of week, Monday=1...Sunday=7
dow() # day month year
{
print $(( $(date2julian $1 $2 $3) % 7 + 1))
}

##################### The End ########################

Those allow rather arbitrary date computations.
For example, yesterday's date can be computed like this:

julian2date $(( $(date2julian $(date +"%d %m %Y") ) - 1 ))

--
Tapani Tarvainen

======================================================================

Appendix B: Refe
 
 
 

1. comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)

2. comp.lang.c Answers to Frequently Asked Questions (FAQ List)


Hi Steve,

It's been five years since the C FAQ was last updated. Although it has
served and is serving its purpose pretty well, there are several places
where its age is showing; e.g. in the future-tense reference to the
'forthcoming' C9X standard, and the mentioning of particular sofware
packages that are out-of-date. Also, the Postscript and HTML versions
that you make available are out-of-sync with the most recent (text) version.


Could you share your thoughts on the desirability of improving this
situation? If you agree that improvements would be desirable, could you
propose a way forward of implementing improvements (e.g., by submitting
patches, or another way)?

I'd appreciate to hear from you. I've also cross-posted to comp.lang.c
since this concerns the entire newsgroup.

Best regards,

Sidney Cadot

3. comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)

4. comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)

5. comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)

6. [OT] comp.lang.c Answers to Frequently Asked Questions (FAQ List)

7. comp.os.ms-windows.programmer.vxd Frequently Asked Questions (FAQ)

8. comp.lang.eiffel Frequently Asked Questions (FAQ)

9. pointer to comp.software.testing Frequently Asked Questions (FAQ)

10. comp.arch.bus.vmebus Frequently Asked Questions (FAQ)

11. Updated comp.software.testing Frequently Asked Questions (FAQ)?

12. [FAQ] comp.sys.amiga.games Frequently Asked Questions (v4.03)