How to Test for a Particular User From Bash
You can test for a particular user with the id test:
[usr-1@srv-1 ~]$ if [ `id -u` = 0 ]; then echo root; fi
[usr-1@srv-1 ~]$ su
Password:
[root@srv-1 usr-1]# if [ `id -u` = 0 ]; then echo root; fi
root
[root@srv-1 usr-1]#
|
Here, we simply test for root, userid=0, and echo root if the current user is root. You can use any userid, of course. If we want to test for usr-1, then we need to determine the userid:
[usr-1@srv-1 ~]$ tail /etc/passwd
squid:x:23:23::/var/spool/squid:/sbin/nologin
webalizer:x:67:67:Webalizer:/var/www/usage:/sbin/nologin
xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
gdm:x:42:42::/var/gdm:/sbin/nologin
pvm:x:24:24::/usr/share/pvm3:/bin/bash
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
amanda:x:33:6:Amanda user:/var/lib/amanda:/bin/bash
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
usr-1:x:500:500:User One:/home/usr-1:/bin/bash
[usr-1@srv-1 ~]$
[usr-1@srv-1 ~]$ if [ `id -u` = 500 ]; then echo usr1; fi
usr1
[usr-1@srv-1 ~]$ if [ `id -u` = 501 ]; then echo usr1; fi
[usr-1@srv-1 ~]$
|
|
|