Kyle R. Burton on 20 Feb 2006 01:09:44 -0000


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

Re: [PLUG] bash and if


> 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.


That actually leads to a pretty clean usage of just case:

cmp -s "$1" "$2"

case "$?" in
  0)
    echo "Files both exist and are the same..."
  ;;

  1)
    echo "Files both exist and are the different."
  ;;

  2)
    echo "One or both of the files do not exist!"
  ;;

  *)
    echo "Hrm, the exit code was: $?, no clause to handle htat..."
  ;;

esac

That easily and explicitly covers all the bases.  Thanks for suggesting case, I'll start using that more now that I think of it this way.

Kyle
___________________________________________________________________________
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