For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > September 2005 > bourne shell script









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author bourne shell script
Greg

2005-09-27, 3:57 am

is there a way to test that something work ie if I diff 2 files is there any
way in the script to tell if the files were the same


Eric Enright

2005-09-27, 3:57 am

Greg wrote:
> is there a way to test that something work ie if I diff 2 files is there any
> way in the script to tell if the files were the same


Thats what program return codes are for; diff has the following
return codes:

0 No differences were found.
1 Differences were found.
>1 An error occurred.


After executing a program, it's return code is stored in the $?
variable, so:

#!/bin/sh

diff a b > /dev/null
if [ "$?" -ne "0" ]; then
echo "files differ"
fi

HTH,
Eric

SM Ryan

2005-09-27, 3:57 am

Eric Enright <eric.enright@gmail.com> wrote:
# Greg wrote:
# > is there a way to test that something work ie if I diff 2 files is there any
# > way in the script to tell if the files were the same
#
# Thats what program return codes are for; diff has the following
# return codes:
#
# 0 No differences were found.
# 1 Differences were found.
# >1 An error occurred.
#
# After executing a program, it's return code is stored in the $?
# variable, so:

If all you want is same or different, cmp might run faster than diff.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Leave it to the Catholics to destroy existence.
Rich Teer

2005-09-29, 9:57 pm

On Tue, 27 Sep 2005, it was written:

> is there a way to test that something work ie if I diff 2 files is there any
> way in the script to tell if the files were the same


Check the return code from diff.

--
Rich Teer, SCNA, SCSA, OpenSolaris CAB member

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich
Bit Twister

2005-09-29, 9:57 pm

On Tue, 27 Sep 2005 13:50:06 +1000, Greg wrote:
> is there a way to test that something work ie if I diff 2 files is there any
> way in the script to tell if the files were the same


if you just want a indicator, use cmp and test return code.

man cmp
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com