If you change
   CPcmd QUERY $userID
into
   CPcmd QUERY USER $userID

it will be better yet.

2010/3/16 Michael MacIsaac <[email protected]>

>
> Ah, peer review is a wonderful thing! Within *minutes* of me posting a
> proposed approach, I got an e-mail off-list,
> from a person who should remain anonymous, that included:
>
> > it is not exact and sloppy and based on assumptions rather than the
> official API
> > Looks to me your code would also confirm that the users TIME and USERID
> are logged as well
>
> Sure enough TIME and USERID are incorrectly reported as being user IDs:
> # ./checkuser.sh userid
> Error: userid is logged on
> # ./checkuser.sh time
> Error: time is logged on
>
> That made me think of other arguments will also produce the wrong results.
> # ./checkuser.sh 123
> Error: unexpected rc from CP QUERY 123 - 40
>
> So I modified the code to use the LINK <USERID> approach suggested.
>
> # ./checkuser.sh time
> Error: time does not exist
> # ./checkuser.sh userid
> Error: userid does not exist
> # ./checkuser.sh 123
> Error: 123 does not exist
> # ./checkuser.sh foo
> Error: foo does not exist
> # ./checkuser.sh maint
> # ./checkuser.sh linux191
> Error: linux191 is logged on
>
> Here's the modified code:
> # cat checkuser.sh
> #!/bin/sh
>
> #+--------------------------------------------------------------------------+
> function CPcmd()
> # Run a CP command and invoke it via the vmcp module/command
> #   Arg1-n: the command to issue
> #   Return: the command's return code
>
> #+--------------------------------------------------------------------------+
>  {
>   if [ "$verbose" = 2 ]; then  // echo extra output
>     echo "Invoking CP command: $@"
>   fi
> # parse output to get return code: awk -F# splits line at '#' with rc at
> end
>   output=$(vmcp $@ 2>&1)
>   if [ ${#output} != 0 -a "$verbose" != 0 ]; then # echo the output
>     echo "$output"
>   fi
>   retVal=0
>   retVal=$(echo $output | grep "Error: non-zero CP" | awk -F# '{print $2}')
>   return $retVal
>  }
>
>
> #+--------------------------------------------------------------------------+
> function checkID()
> # Verify user ID exists and is logged off
> # Arg 1: user ID to check
>
> #+--------------------------------------------------------------------------+
>  {
>   userID=$1
>   verbose=0
>   CPcmd LINK $userID  # first check if the user ID exists
>   rc=$?
>   case $rc in
>    22)  # user ID exists - fall through
>       ;;
>    53)  # user ID does not exist
>       echo "Error: $userID does not exist"
>       return 2
>       ;;
>    *) # unexpected
>       echo "Error: unexpected rc from CP LINK $userID - $rc"
>       return 3
>   esac
>   CPcmd QUERY $userID # verify that the user ID is logged off
>   rc=$?
>   case $rc in
>     0)  # user ID is logged on or disconnected
>       echo "Error: $userID is logged on"
>       return 1
>       ;;
>    45) # user ID is logged off - this is correct
>       ;;
>    *) # unexpected
>       echo "Error: unexpected rc from CP QUERY $userID - $rc"
>       return 3
>   esac
>   verbose=1
>   return 0
>  } # checkID()
>
> So Rob, does this look better?  :))
>
> "Mike MacIsaac" <[email protected]>   (845) 433-7061




-- 
Kris Buelens,
IBM Belgium, VM customer support

Reply via email to