HELP! HELP! The Shell Script is not working when install a package.

shermanpan
New Contributor III

As the blow picture ,when i package a app , i add a shell script after finished the instaling. The app is installed successful, but script copy the /var/somefile to /Users/someuser/Desktop is not successful , please help me solve the problem
deb1ab5e816f4f3881db9ea99b0c60b1
4c2a6b805e86406fbd1ca466e246f8f7

I just want put a file to the computer all user include the local user. please tell me how to do it .

10 REPLIES 10

rwinfie
Contributor

Change /Users/someuser/Desktop to
/Users/$3/Desktop

So ifs ran at login logout or self service it will install under the current login users desktop

dsavageED
Contributor III

First, I'm not sure this is a great idea... Sticking a file randomly on the users desktop isn't necessarily that helpful. That being said, you need to keep in mind that the scripts run from within a package will run as root. So at the very least you need to fix the permissions on the file. Keep in mind that if the computer has multiple users only the one who installed the package will get the info file...

#!/bin/sh

# Get the username
User_Name=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`

cp -f /var/Macshortcut.docx /Users/${User_Name}/Desktop
chown "${User_Name}" /Users/${User_Name}/Desktop/Macshortcut.docx

exit 0;

What's actually in the file and what are you trying to achieve?

chris_kemp
Contributor III

Agreed, it seems pushy to start sticking things on user's Desktops. You could install it in the Applications folder - if you renamed it "Omnigraffle Shortcuts.docx" (I assume that's what this doc is?) then it would show up next to the app itself - plus, if you have multiple users then everyone would be able to access it.

shermanpan
New Contributor III

@ rwinfie can you tell me what is the mean of parameter $3 !

shermanpan
New Contributor III

when i run the command it's report a error, i just put a file to a computer all user include local user!

#!/bin/sh

User_Name=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`


#this command is report a error , why not use user_name=`whoami`

shermanpan
New Contributor III

db7479fc64d244a6b8f29249fb31fcc5

the error

dsavageED
Contributor III

@shermanpan , whoami doesn't handle fast user switching and is generally not reliable enough, especially within a package, where there is a good chance the response will be root. The older method of getting the logged in user was:

User_Name=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')

This method still doesn't work sensibly when Fast User Switching is enabled. As it will return the username for all Users. The python on the other-hand returns only the active user at the point it is run. See: https://macmule.com/2014/11/19/how-to-get-the-currently-logged-in-user-in-a-more-apple-approved-way/

I've used the command on Macs running 10.10 to 10.14. you might need to specify the path to python, so:

User_Name=`/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`

More generally however, you should reconsider the way you are doing this, as said previously dumping a file on the users desktop isn't that helpful or a good user experience...

awginger
Contributor

Can you not just use ~/Desktop to use the current user's Desktop folder?

cp /var/Macshortcut.docx ~/Desktop

dsavageED
Contributor III

I don't think "~/Desktop" will work when run from within the postinstall of a package as the scripts are run as root, so the path will be /var/root/Desktop...

rwinfie
Contributor

@shermanpan the $3 is a jamf built in variable for current logged in user.