When running scripts via the JSS, you sometimes have a challenge to get the username of the currently logged in user.
If you’re running the script at login or via Self Service, then $3 can be used to grab the username. However, you’ll quickly find that at times you’ll want to find the username whilst running a policy at another time than at login or via Self Service.
A common method used is below & you’ll find it in a number of my scripts.
This has served me well, but the method has issues when multiple users are logged in with Fast User Switching enabled. As it will return the username for all Users.
On seeing an example of the above, Frogor over at the ##OSX-Server IRC mentioned that this method was both a little “odd” & not Apple’s recommended way. Then, being the guy he is.. He not only pointed us in IRC to a link about Apple’s recommended method, but also gave an example of how to call it within bash. That example is below:
You’ll start to see the above soon in my new posts, & if you do use Fast User Switching then this may work better for you where other methods have failed.