Stephen Gran on 19 Feb 2006 21:32:39 -0000


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

Re: [PLUG] bash and if


On Sun, Feb 19, 2006 at 04:05:23PM -0500, Jeff Abrahamson said:
> On Sun, Feb 19, 2006 at 03:58:45PM -0500, Kyle R. Burton wrote:
> > 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.
> > >
> > Can you just:
> > 
> > if cmp -s "$f" "$dir2/b"; then
> >   echo "same"
> > else
> >   echo "not same"
> > fi
> > 
> If one doesn't exist it returns 2.

That's in your else clause, then.

roughly:

if cmp -s "$f" "$dir2/b"; then
  echo same
  do_stuff_for_same
else
  case "$?" in
    1) echo differ
       do_stuff_for_different
       ;;
    2) echo missing
       do_stuff_for_missing
       ;;
  esac
fi

Probably prettier ways of doing it, but you get the idea - if the if
returns true, then the files differ because cmp exited 0.  Otherwise,
you'll have to examine $? to see what it's value is.  You can't, as far
as I know, do the examination of return status in line as you can in
other languages ( the `if ( ( ret = do_something ) == 0 )' syntax you're
looking for).  If you figure out a way to do it that I'm unaware of,
please pass it on.

HTH,
-- 
 --------------------------------------------------------------------------
|  Stephen Gran                  | BOFH excuse #73:  Daemons did it        |
|  steve@lobefin.net             |                                         |
|  http://www.lobefin.net/~steve |                                         |
 --------------------------------------------------------------------------

Attachment: signature.asc
Description: Digital 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