5 Replies Latest reply: Jul 23, 2012 8:07 PM by Catch-22 RSS

    Using return status with bash test builtin

    Catch-22
      Hello,

      Following shell script (bash):
      f_init() {
         # Output: 0 = shell, 1 = other
         [ -z "$USER" ] && return 0 || return 1
      }
      
      USER=
      f_init
      RETVAL=$?
      
      [ "$RETVAL" = 0 -a $(id -u) = 0 ] && echo left || echo right
      I was wondering if there is a way to get rid of the RETVAL variable and use the return status of the f_init function inside the test statement directly. Something similar like - which doesn't work, e.g:
      [ "$(f_init)" = 0 -a $(id -u) = 0 ] && echo left || echo right
      
      if f_init -a [ $(id -u) = 0 ]; then
         echo left
      else
         echo right
      fi
      Any ideas? Thanks!