Chad Carr wrote:

Shell gurus,

Is there a reliable cross-platform way to write an inverted if statement? Something like bash's:

        if ! /usr/bin/false # with some other command, obviously
        then
                # do something true
        else
                # do something false
        fi

Your assistance is greatly appreciated.

Just shuffle the "then" and "else" code around as appropriate for the sense of the exit condition you're checking for.


If that's not an option (ie: you're only checking one case, and don't want an empty "then"), you can do something like

  if /usr/bin/false ; [ $? -ne 0 ] ; then
    ...

or more typically:

  /usr/bin/false
  if [ $? -ne 0 ] ; then
    ...

or even:

  /usr/bin/false
  retcode=$?
  ...
  if [ $retcode -ne 0 ] ; then
    ...

If you're just trying to test for error codes or something, the following might be handy:

  abort() { ... }

  # Call an abort procedure if command1 fails
  /usr/bin/command1 || abort

  # Run several commands if command2 fails
  /usr/bin/command2 || { echo "ERROR!!!" ; exit ; }

HTH

--
Charles Steinkuehler
[EMAIL PROTECTED]


-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 - digital self defense, top technical experts, no vendor pitches, unmatched networking opportunities. Visit www.blackhat.com


_______________________________________________
leaf-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/leaf-devel

Reply via email to