Check if user exists using id (linux)

I see a lot of examples about check if user exists in *nix system using id

For test my code i've create a simple code and after check and verify work I've copied to develop script

checkuser.sh

#!/usr/bin/env bash
function checkUser() { if id "$USER" >/dev/null 2>&1; then echo "user exists" else echo "user does not exist" fi
}
USER=$4
checkUser

Test code

sudo local/checkuser.sh misitio test 7.3 abkrim /home/abkrim/Sites/
abkrim.EXISTS
user exists

But when copy in my script not work

#!/usr/bin/env bash
if [ $EUID != 0 ]; then sudo "$0" "$@" exit $?
fi
SITE=$1
PHP=$3
USER=$4
NGINX=/etc/nginx/
FPM=/etc/php/${PHP}/fpm/pool.d/
PATH=$5
checkUser

Test code

sudo local/deploy_site.sh misitio test 7.3 abkrim /home/abkrim/Sites/
abkrim
user does not exist
1

1 Answer

PATH is in the environment, it is special. After your script does PATH=$5, it's no longer able to find the id executable; therefore this

id "$USER" >/dev/null 2>&1

silently fails.

You probably do not want to overwrite PATH. You probably want your variables in lower case.

However if you really want to overwrite PATH, then you should later use full path to any executable that shouldn't depend on the new ("custom") PATH. E.g. /usr/bin/id.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like