NetAdminTools.com
 
SignalQ Sites:
NetAdminTools - Coprolite - NoNIC - SpotBridge - NAW
RoboCoop - AreWeDown - SolarPower - SysAdminTools
Xfig - Gold Loaf - GeekPapa - FixGMC - MCJ - FixRambler
Categories:
GNU/Linux | Homebrew designs | Perl | Administration | Backup/Recovery | Bugs/Fixes | Certification | Database | Email | File/Print | Hardware | Information Grab Bag | Interoperability | GNU/Linux ABCs | Monitoring | Name Resolution | Network Services | Networking | Remote Control | Security | Desktop | Web | BSD | Solaris | GIAGD | REALbasic

Last 30 Days | Last 60 Days | Last 90 Days | All Articles | RSS | Hail Support


Categories:
·GNU/Linux
·Homebrew designs
·Perl
·Administration
·Backup/Recovery
·Bugs/Fixes
·Certification
·Database
·Email
·File/Print
·Hardware
·Information Grab Bag
·Interoperability
·GNU/Linux ABCs
·Monitoring
·Name Resolution
·Network Services
·Networking
·Remote Control
·Security
·Desktop
·Web
·BSD
·Solaris
·GIAGD
·REALbasic
·All Categories


How to Test for a Particular User From Bash
Topic: GNU/Linux   Posted:2005-05-27
Printer Friendly: Print

spacerspacer
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 ~]$ 





Please read our Terms of Use
Microsoft, Windows, Windows XP, Windows 2003, Windows 2000, and NT are either trademarks or registered trademarks of Microsoft Corporation. NetAdminTools.com is not affiliated with Microsoft Corporation. Linux is a registered trademark of Linus Torvalds, and refers to the Linux kernel. The operating system of most distributions that contain the Linux kernel is GNU/Linux. All logos and trademarks in this site are property of their respective owner. Copyright 1997-2008 NetAdminTools.com

Created by:
MCJ
MCJ CMS