command_exists function

adapted from http://stackoverflow.com/a/3931779 and the discussion above