Sunday, January 28, 2018

[Shell script] Find user in system


The first way:

#!/bin/bash

if [ $# -eq 0 ]
then
 echo "Usage: $0 lawrence"
else
  grep "\<$1\>" /etc/passwd > /dev/null
  RETVAL=$?
  if [ $RETVAL -eq 0 ]
  then
   echo "Username $1 was found in the system"
  else
   echo "Cannot find username $1"
 fi
fi

The second way:

[ $# -eq 0 ] &&  { echo "Usage: $0 lawrence" ; exit 1; }

grep "\<$1\>" /etc/passwd > /dev/null
RETVAL=$?

[ $RETVAL -eq 0 ] && echo "Username $1 was found in the system" || echo "Cannot find username $1"

No comments: