Skip to main content
Question

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


Forum|alt.badge.img+3

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

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

10 replies

Forum|alt.badge.img+9
  • Contributor
  • 37 replies
  • October 29, 2018

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
Forum|alt.badge.img+8
  • New Contributor
  • 168 replies
  • October 29, 2018

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?


Forum|alt.badge.img+20
  • Jamf Heroes
  • 339 replies
  • October 29, 2018

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.


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 8 replies
  • October 30, 2018

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


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 8 replies
  • October 30, 2018

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`

Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 8 replies
  • October 30, 2018

the error


dsavageED
Forum|alt.badge.img+8
  • New Contributor
  • 168 replies
  • October 30, 2018

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


Forum|alt.badge.img+7
  • Contributor
  • 44 replies
  • October 30, 2018

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

cp /var/Macshortcut.docx ~/Desktop

dsavageED
Forum|alt.badge.img+8
  • New Contributor
  • 168 replies
  • October 30, 2018

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


Forum|alt.badge.img+9
  • Contributor
  • 37 replies
  • October 30, 2018

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings