how to compare file modification time in bourne shell script

Alex Hanselmann alex at impch.imp.com
Thu Aug 2 23:30:03 AEST 1990


In article <1990Jul23.233044.2729 at silma.com> aab at silma.UUCP () writes:
>I need to compare the modification times of two files in a bourne shell
>script. I would like to do this without writing C code.
>Machine is Sparcstation 1 running SunOS 4.03c
>
>Thus I need a function:
>
>newer file1 file2
>
>that returns 0 if file1 is newer than file2 else returns 1
>
>Can it be done?

yes here's the script for newer. It's work with the modification time.

--
#!/bin/sh
#newer file1 file2 (mode)
# returns 0 if file 1 is newer then file2 refered by mode (A/M/C (=> man ls)
# returns 1 if file 2 is newer then file1 refered by mode ...
# returns 2 on error


if [ $# -lt 2 -o $# -gt 3 ] ;then
	echo "error in usage: incorrect argc" 
	exit 2
fi

NMODE=''

if [ $# -eq 3 ] ; then
	case $3 in
	[Aa]) NMODE='u' ;;
	[Cc]) NMODE='c' ;;
	esac
fi


[ `ls -t$NMODE $1 $2 | line` = $1 ] && exit 0
exit 1
---

alex



More information about the Comp.unix.questions mailing list