Kyle R. Burton on 19 Feb 2006 20:59:12 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] bash and if


Can you just:

if cmp -s "$f" "$dir2/b"; then
  echo "same"
else
  echo "not same"
fi

?

If one doesn't exist, cmp will return 1 (false).  If they both exist and are the same, then it will return 0 (true).

Kyle

On 2/19/06, Jeff Abrahamson <jeff@purple.com> wrote:
In a bash script I want to compare two files (one or both of which may
not exist) and take a certain action if they *both* exist and are
identical.

Refresher, cmp returns 0 on equality, 1 on inequality, 2 on error such
as one or both files doesn't exist.

The following attempt fails miserably (at the syntax level, for
obvious reasons):

    if [ (cmp -s "$f" "$dir2/$b")  -a $? == 0 ]; then

Anyone know how to combine a subshell invocation with a logical
operation in an if clause?

I'd like to be more elegant than a double if, merely on principle:

    if [ -r "$f" -a -r "$dir2/$b" ]; then
        if ( cmp -s ... ); then ...

--
Jeff

Jeff Abrahamson  <http://www.purple.com/jeff/>    +1 215/837-2287
GPG fingerprint: 1A1A BA95 D082 A558 A276  63C6 16BF 8C4C 0D1D AE4B


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFD+NomFr+MTA0drksRAhD1AJ92LZeXi2wa+w7nNDgOSfM0f8q1tQCdEfMo
e37rkpjAaV69vQdLY0wNPb8=
=ungl
-----END PGP SIGNATURE-----


___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug





--
------------------------------------------------------------------------------
Wisdom and Compassion are inseparable.
        -- Christmas Humphreys
kyle.burton@gmail.com                            http://www.neverlight.com/~mortis
------------------------------------------------------------------------------
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug