Skip to main content
Answer

help with console user please

  • October 31, 2012
  • 7 replies
  • 21 views

Forum|alt.badge.img+21

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?

Best answer by mm2270

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'

7 replies

mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • Answer
  • October 31, 2012

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'


Forum|alt.badge.img+21
  • Author
  • Honored Contributor
  • October 31, 2012

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 ;)


Forum|alt.badge.img+12
  • Contributor
  • October 31, 2012

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


Forum|alt.badge.img+18
  • Valued Contributor
  • October 31, 2012

/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...


Forum|alt.badge.img+10
  • New Contributor
  • October 31, 2012

Yet another way which avoids grep and awk:

stat -f "%Su" /dev/console


Forum|alt.badge.img+21
  • Author
  • Honored Contributor
  • November 1, 2012

Thanks guys :)


Forum|alt.badge.img+24
  • Valued Contributor
  • November 1, 2012

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