title: Bash test of function return not working
body: <p>I am trying to write a test to check if a user exists.  I know there is the simple <code>getent</code>, which I discovered later while googling my problem (and which I will probably wind up using) but, I am now interested in solving this as an academic exercise.</p>
  
  <p>I have a script, loading a second script via the <code>source</code> command and then executing a function from the loaded script.  The goal was to run the check without spawning a subshell.</p>
  
  <p>The problem I am having, is checking the return status within that called function (it, in turn, calls a second method).  </p>
  
  <p>My code.  Initial tst.sh script:</p>
  
  <pre><code>#!/bin/sh
  userName="$1"
  source userExists.sh
  exitIfUserExist "${userName}"
  exitIfUserNotExist "${userName}"
  exit 0
  </code></pre>
  
  <p>userExists.sh:</p>
  
  <pre><code>#!/bin/sh
  
  #check if user exists already
  checkIfUserExists()
  {
      userName=$1
      echo "userName: $userName"
  
      while IFS= read -r line
      do
          # First, whack off everything after the username
          userNameTmp=${line%:*:*:*:*:*:*}
          echo "userNameTmp: $userNameTmp"
  
          # Then compare the username to that entered by the user.
          [[ $userNameTmp = "${userName}" ]] &amp;&amp; return 0
      done &lt;/etc/passwd
      echo "Not found"
  
      return 2
  }
  
  exitIfUserExist()
  {
      if checkIfUserExists $1 == 0
      then
        echo "Error: User ${userName} already exists"
        exit 1
      fi
      echo "end exitIfUserExist"
  }
  
  exitIfUserNotExist()
  {
      checkIfUserExists $1;echo $?
      if checkIfUserExists $1 &gt; 0
      then
          echo "Error: User ${userName} does not exist"
          exit 1
      fi
      echo "end exitIfUserNotExist"
  }
  </code></pre>
  
  <p>Output from two tests - first with a username that exists, the second with one that does not:</p>
  
  <pre><code>[root@local_host ~]# sh t.sh bob
  userName: bob
  userNameTmp: root
  userNameTmp: bin
  userNameTmp: daemon
  ...
  userNameTmp: rpc
  userNameTmp: rpcuser
  userNameTmp: nfsnobody
  userNameTmp: bob
  Error: User bob already exists
  
  
  [root@local_host ~]# sh t.sh bobNOTTHERE
  userName: bobNOTTHERE
  userNameTmp: root
  userNameTmp: bin
  userNameTmp: daemon
  ...
  userNameTmp: rpc
  userNameTmp: rpcuser
  userNameTmp: nfsnobody
  userNameTmp: bob
  Not found
  end exitIfUserExist
  userName: bobNOTTHERE
  userNameTmp: root
  userNameTmp: bin
  userNameTmp: daemon
  ...
  userNameTmp: rpc
  userNameTmp: rpcuser
  userNameTmp: nfsnobody
  userNameTmp: bob
  Not found
  2
  end exitIfUserNotExist
  </code></pre>
  
  <p>As can be seen the first test works, but, given the result of the second, I don't believe the if .. then test is working as I expect it to.  How do I fix the tests in the "exitIf*" functions to work properly?</p>