help with console user please

tkimpton
Valued Contributor II

hi guys

Was wondering if you can help me.

For my scripts i usually use below for the currently logged in user

ls -l /dev/console | cut -d " " -f4

this returns tkimpton@rufusleonard.hq

Im trying to work out how to strip out @rufusleonard.hq so i am left with tkimpton.

Any ideas?

1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

Do you get a better result with the following?-

/usr/bin/who | awk '/console/{ print $1 }'

If not, you can use your command and send it through another cut like this-

| cut -d@ -f1

That should leave you with everything from before the @ symbol, meaning "tkimpton'

View solution in original post

7 REPLIES 7

mm2270
Legendary Contributor III

Do you get a better result with the following?-

/usr/bin/who | awk '/console/{ print $1 }'

If not, you can use your command and send it through another cut like this-

| cut -d@ -f1

That should leave you with everything from before the @ symbol, meaning "tkimpton'

tkimpton
Valued Contributor II

OMG wow that if friggin awesome.

Thank you very much

went on a bender last night and my brain has b een hitting a brick wall ;)

acdesigntech
Contributor II

following in the "1001 ways to do" X tradition: ```
who | grep console | awk '{print $1}'
```

nessts
Valued Contributor II

/dev/console does seem to be a bit more reliable, sometimes when logging out one user and logging in a new one who on the console can produce less than consistent results in my experience anyway...

gregneagle
Valued Contributor

Yet another way which avoids grep and awk:

stat -f "%Su" /dev/console

tkimpton
Valued Contributor II

Thanks guys :)

jarednichols
Honored Contributor

You can also read the lastUser key from com.apple.loginwindow.plist