The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - General (http://hintsforums.macworld.com/forumdisplay.php?f=16)
-   -   Terminal: PS results different from Activity Monitor? (http://hintsforums.macworld.com/showthread.php?t=91484)

baf 07-05-2008 10:40 AM

Quote:

Originally Posted by biovizier (Post 480456)
For me, checking the owner of '/dev/console' has been fairly reliable (at least in 10.4 and 10.5) in finding the current active user (root if at the login window), eg.
Code:

stat -f "%Su" /dev/console


That's so easy that it borders on cheating. :D

cwtnospam 07-05-2008 10:57 AM

Quote:

Originally Posted by baf (Post 480467)
Reason: All the interesting stuff is on one line so it's hard to use awk or grep and I'm not sure if they always come in the same order.

Well, this seems to work. ;)
Code:

ioreg -w0 -Sn Root | grep 'kCGSSessionOnConsoleKey"=Yes' | perl -pe 's/kCGSSessionOnConsoleKey"=Yes/\nkCGSSessionOnConsoleKey"=Yes/g' | grep 'kCGSSessionOnConsoleKey"=Yes' | perl -pe 's/kCGSessionLongUserNameKey"="/\n/g' | tail -n 1 | perl -pe 's/"/\n/g' | head -n 1
I'm sure it could be made shorter, but it'll never get as short as biovizier's solution.

baf 07-05-2008 11:06 AM

Yes but I'm always careful when data comes on a long line because sometimes order is not guaranteed. With my script order doesn't matter at all.

cwtnospam 07-05-2008 11:11 AM

Quote:

Originally Posted by baf (Post 480479)
With my script order doesn't matter at all.

Neither does mine, I think. ;) I tried to keep it all on one line and then eliminate extraneous data to pluck the user name from that line.

baf 07-05-2008 11:27 AM

Example of what I mean:
assume that sometimes kCGSessionLongUserNameKey=... comes before kCGSSessionOnConsoleKey=... your script would then fail.

I'm not saying it does but as I can only check on one machine and with it's installed os I can't be sure. Is it perhaps sorted in username order or what? I just prefer to do a real parse if possible unless I have "proof" e.g. a man page that states the order.

baf 07-05-2008 11:44 AM

And if we're talking perl then use it the whole way.
Code:

ioreg -w0 -Sn Root |perl -ne '/.*{(.*?kCGSSessionOnConsoleKey"=Yes.*?)}.*/ && $1=~/"kCGSessionLongUserNameKey"="(.*?)"/ && print $1'
This (I think) should be order safe, doesn't depend on any commands except ioreg and raw perl. It seems to be the fastest one (except bio:s ) checked with time ....

hayne 07-05-2008 11:54 AM

An alternative is to use the 'scutil' command, asking it to show the data for the key "State:/Users/ConsoleUser".

Here's a Perl script that does this (it shows the short username):
Code:

#!/usr/bin/perl

# getConsoleUsername:
# This script echos the name of the currently active user
# (this is useful when using Fast User Switching)
#
# This script also serves as an illustration of using the 'scutil' command.
# Cameron Hayne (macdev@hayne.net)  July 2008

use strict;
use warnings;

my $scutil = "/usr/sbin/scutil";
my $consoleUserKey = "State:/Users/ConsoleUser";
my $results = `echo "show $consoleUserKey" | $scutil`;
#print $results;
my ($uid) = $results =~ m/^\s*UID\s*:\s*(\d+)\s*$/m;
my ($username) = $results =~ m/^\s*Name\s*:\s*(\S+)\s*$/m;
#print "$uid\n";
print "$username\n";


cwtnospam 07-05-2008 11:56 AM

Quote:

Originally Posted by baf (Post 480482)
Example of what I mean:
assume that sometimes kCGSessionLongUserNameKey=... comes before kCGSSessionOnConsoleKey=... your script would then fail.

I see.
Quote:

Originally Posted by baf (Post 480486)
And if we're talking perl then use it the whole way.
Code:

ioreg -w0 -Sn Root |perl -ne '/.*{(.*?kCGSSessionOnConsoleKey"=Yes.*?)}.*/ && $1=~/"kCGSessionLongUserNameKey"="(.*?)"/ && print $1'
This (I think) should be order safe, doesn't depend on any commands except ioreg and raw perl. It seems to be the fastest one (except bio:s ) checked with time ....

I've got to get better with perl. I only started using it when I had a problem with sed and escaped characters like \n and \t.

hayne 07-05-2008 12:11 PM

Quote:

Originally Posted by baf (Post 480486)
It seems to be the fastest one (except bio:s ) checked with time ....

Code:

% time ioreg -w0 -Sn Root |perl -ne '/.*{(.*?kCGSSessionOnConsoleKey"=Yes.*?)}.*/ && $1=~/"kCGSessionLongUserNameKey"="(.*?)"/ && print $1'
Fred Smith
real        0m0.048s
user        0m0.013s
sys        0m0.039s

% time ./getConsoleUsername
fred

real        0m0.021s
user        0m0.010s
sys        0m0.010s

% time stat -f "%Su" /dev/console
fred

real        0m0.004s
user        0m0.001s
sys        0m0.002s

One problem with biovizier's solution is that the permissions on /dev/console would seem to prevent a script running under some other user account from doing 'stat' on it. Hence a 'sudo' would be needed.

baf 07-05-2008 01:12 PM

Nice one hayne. Do you have any good place to read up on scutil? The man page is rather short.

And on which os doesn't stat work? On tiger it's ok for me at least.

hayne 07-05-2008 04:54 PM

Quote:

Originally Posted by baf (Post 480505)
And on which os doesn't stat work? On tiger it's ok for me at least.

I don't know that it doesn't work - I just looked at the permissions and saw that they are:
crw-------
so only the owner has read permission. And so I thought that 'stat' would fail for anyone other than owner. But I realize now that the relevant permissions are those of the enclosing folder - so all is well.

I don't have any better reference on 'scutil' - I agree that the man page is inadequate.
Best to just run it interactively and try things: especially 'list' and 'help'

baf 07-05-2008 04:56 PM

:D Ahh even you can be mistaken.;)


All times are GMT -5. The time now is 05:46 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.