[alt.sources.d] how to compare file modification time

lee@sq.sq.com (Liam R. E. Quin) (08/03/90)

fritz@mercury.caltech.edu (Fritz Nordby) writes:
> Am I missing something here, or will the following [work]?
>
>#!/bin/sh
>case $# in 2);;*)echo "usage: ${0:-newer} file1 file2" >&2;exit 2;;esac
>[ ! -f "$2" -o -f "$1" -a "X`/bin/ls -t \"\$1\" \"\$2\"`" = "X$1
>$2"] >/dev/null 2>&1

I have no idea.  It *might* have won the Obfuscated Shell Contest, except
that there isn't one.  At first I thought it was a perl script.
I have had a go at expanding this a little below... although I'd probably
use find(1) instead for this.  [I mised the original article, presumably
there was some reason not to use find apart from ``I don't understand it'']

Lee


#! /bin/sh

# valilidate arguments:
case $# in
2)  ;;
*)  echo "usage: ${0:-newer} file1 file2" >&2
    exit 2
    ;;
esac

if [ ! -f "$2" -o ! -f "$1" ]
then
    # we might want to print an error message here??
    exit 1
fi

LsOuput=`/bin/ls -dt "$1" "$2"`
FirstNewer="$1
$2"

test x"${LsOutput}" = x"${FirstNewer}

exit $?

-- 
Liam R. E. Quin,  lee@sq.com, {utai,utzoo}!sq!lee,  SoftQuad Inc., Toronto
``He left her a copy of his calculations [...]  Since she was a cystologist,
  she might have analysed the equations, but at the moment she was occupied
  with knitting a bootee.''  [John Boyd, Pollinators of Eden, 217]

chrisb@risky.Convergent.COM (Chris Bertin) (08/06/90)

In article <1990Aug3.155651.12006@sq.sq.com>, lee@sq.sq.com (Liam R. E. Quin) writes:
> fritz@mercury.caltech.edu (Fritz Nordby) writes:
> > Am I missing something here, or will the following [work]?
> >
> >#!/bin/sh
> >case $# in 2);;*)echo "usage: ${0:-newer} file1 file2" >&2;exit 2;;esac
> >[ ! -f "$2" -o -f "$1" -a "X`/bin/ls -t \"\$1\" \"\$2\"`" = "X$1
> >$2"] >/dev/null 2>&1
> 
> I have no idea.  It *might* have won the Obfuscated Shell Contest, except
> that there isn't one.  At first I thought it was a perl script.
> I have had a go at expanding this a little below... 
....
> Lee

Expanded doesn't mean better.
> 
> # valilidate arguments:
> case $# in
> 2)  ;;
> *)  echo "usage: ${0:-newer} file1 file2" >&2
>     exit 2
>     ;;
> esac

The comment is useless and 'valilidate' is a new word to me. It is obviously
an argument validation check, since it is checking $# and echo-ing a usage
statement.

> 
> test x"${LsOutput}" = x"${FirstNewer}
> exit $?
> 

The 'exit' statement is useless. 'test' will return the right exit code.

To go back to the original script, it really doesn't need to open /dev/null.
Most people do that when they want to discard output, but why not close
the apropriate file descriptor?
> >[ ! -f "$2" -o -f "$1" -a "X`/bin/ls -t \"\$1\" \"\$2\"`" = "X$1
> >$2"] >/dev/null 2>&1

[ ! -f "$2" -o -f "$1" -a "X`/bin/ls -t \"\$1\" \"\$2\"`" = "X$1 $2"] >&- 2>&-

Chris
-- 
Chris Bertin		|   chrisb@risky.Convergent.COM
Unisys			|		or
(408) 435-3762		| ...!uunet!pyramid!ctnews!risky!chrisb

maart@cs.vu.nl (Maarten Litmaath) (08/07/90)

In article <303@risky.Convergent.COM>,
	chrisb@risky.Convergent.COM (Chris Bertin) writes:
)...
)To go back to the original script, it really doesn't need to open /dev/null.
)Most people do that when they want to discard output, but why not close
)the apropriate file descriptor?

Because in general it's a bad idea _not_ to provide a program with _valid_
stdin, stdout and stderr files.  The program e.g. might abort after
discovering a write() to file descriptor 1 failed...
--
   "UNIX was never designed to keep people from doing stupid things, because
    that policy would also keep them from doing clever things."  (Doug Gwyn)