Creating Alias on User Desktop at Login

New Contributor II

Hello! I am trying to create an alias for 3 folders that live in /Users/Shared on every User's Desktop when they log in.

I am using this...


#Gets Current Logged In User and Creates Alias 
ln -s /Users/Shared/Folder 1 /Users/$(who | awk '/console/{ print $1 }')/Desktop/Folder 1 
ln -s /Users/Shared/Folder 2 /Users/$(who | awk '/console/{ print $1 }')/Desktop/Folder 2 
ln -s /Users/Shared/Folder 3 /Users/$(who | awk '/console/{ print $1 }')/Desktop/Folder 3

I have the policy trigger to run at login and once per user.

I am getting a failed message...

Script result: ln: /Users//Desktop/Folder 1: No such file or directory ln: /Users//Desktop/Folder 2: No such file or directory ln: /Users//Desktop/Folder 3: No such file or directory

It looks like it is not finding the User. Can I some direction on where my script is wrong? When I run this manually on a computer it is successful.

Thanks bunch!

Lisa Rosbrook


Contributor II

I tried running it just from a jamf -event call and it looks like it works fine. Judging from how you're getting the user using the "who" command, I'm guessing that it's calling the script before the console session gets fully established, because it's returning a value of null when you're trying to set that as the current console user's folder path.

Maybe try throwing in a "sleep 5" before you try running the symlink commands and see if that does the trick?

Legendary Contributor II

@NoahRJ is likely correct. The telltale sign is the line in the error stating:

No such file or directory ln: /Users//Desktop/Folder 2

Notice the missing account name between /Users/ and /Desktop/ Its not getting the username.

Also, I would avoid using who | awk '/console{print $1}' to get the user. its not the most reliable way to do it.
Try swapping it out with:

$(stat -f%Su /dev/console)

Honored Contributor II
Honored Contributor II

If you are running it at login you can use the standard Casper variable $3 so /Users/$3/Desktop/Folder 1

Valued Contributor

+1 for all of the above. Unless you create a LaunchAgent, you can't be sure that /dev/console is running before your script runs.

As @davidacland suggested, $3 will work at login, Casper is doing that work for you, but I'd avoid a sleep command as this could vary on success.

You could also add to User Template.