Checking if a user account exists?

Hi everyone,

I'm trying to write a script that checks if there's a user named "test" ... but this returns "AppleScript Error: 0":

set result to do shell script "grep -ic \"test\" /etc/passwd"
if result equals "0" then ...

the grep ... part should return 0 if test isn't found, which I want to go into "result" ... but it doesn't look like it's working? Any ideas would be verrrry appreciated!!

Cassie

Mac OS X (10.4.5)

Posted on Mar 15, 2006 9:13 AM

Reply
5 replies

Mar 15, 2006 10:12 AM in response to cassie21ts

There are two flaws in your script.

First of all, if you man grep you'll eventually find that:

Normally, exit status is 0 if selected lines are found and 1 otherwise.


What you're seeing is that grep is returning exit status 1 (no lines found) and AppleScript interprets any exit status other than 0 as an error from the shell command.

In theory, grep's '-q' switch should change this behavior, but it doesn't appear to, so the alternative is to use a try block to catch the error:

set isFound to 0
try
set isFound to do shell script "grep -i -c \"test\" /etc/passwd"
end try
if isFound ≠ 0 then
...
end if


However, the second error is that this is likely to return 0 anyway because user accounts are not stored in /etc/passwd as they are on most other Unix systems. Even if they were there, your script would match any user with 'test' in their account entry, including a 'real name' of 'Testing account'.

If your machine is standalone, you're better off querying NetInfo for the username. Either:

do shell script "nicl . -list /users | grep -i \"test$\""


(with the same caveats as above when dealing with a non-0 exit status) or, even better, query the user account directly:

set userAccount to "Invalid User"
try
set userAccount to do shell script "nicl . -read /users/test"
end try
if userAccount ≠ "Invalid User" then
-- user exists
end if

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Checking if a user account exists?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.