IF $USER is not in this list of users, then do this

I need to add to a BASH script if ${USER} is not in a list of users (smitha, brownd, adamsp) then do something... what is the best shortest way to accomplish an if statement with a list like this? Also the list of users should be in the script, not an external file.

Thanks!


echo 'smitha, lucyb, johnk' | grep -q ${USER} 
if [ [ $? -eq 0 ] ; then
  echo "ok"
else
  echo "not ok"
fi

What does $? indicate? What is this the varible of?

$? gives you the exit status of the latest command executed. In this case if this

echo 'smitha, lucyb, johnk' | grep -q ${USER}

is successfully done, then $? will be 0, else it will be 1 (or any other value between 1 and 255). Then you use that value to check if the users was found or not.

Perfect, thanks so much! clever solution.

---------- Post updated at 03:54 PM ---------- Previous update was at 03:53 PM ----------

if [ [ $? -eq 0 ] ; then

Why the double brackets after if, and only on one side?

The one sided double brackets is a typo.
You should also use the -w flag if your grep supports it.

echo 'smitha, lucyb, johnk' | grep -qw ${USER}

This way user smith is not errornously recogized as valid.