HELP! HELP! The Shell Script is not working when install a package.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on
10-29-2018
05:03 AM
- last edited on
03-04-2025
06:38 AM
by
kh-richa_mig
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 .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 10-29-2018 05:12 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 10-29-2018 05:16 AM
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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 10-29-2018 07:09 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 10-29-2018 07:29 PM
@ rwinfie can you tell me what is the mean of parameter $3 !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 10-29-2018 08:53 PM
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`
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 10-29-2018 08:54 PM
the error

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 10-30-2018 02:58 AM
@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...

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 10-30-2018 03:33 AM
Can you not just use ~/Desktop to use the current user's Desktop folder?
cp /var/Macshortcut.docx ~/Desktop

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 10-30-2018 04:06 AM
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 10-30-2018 07:53 AM
@shermanpan the $3 is a jamf built in variable for current logged in user.
