1

There are two ways this script can be run: When the user opens the pkg file and goes through the normal GUI setup, or when an admin (or savvy user) runs sudo installer -pkg /path/to/Installer.pkg -target /. For the second one, I want to know when the script was run in this mode so I can make more admin-friendly decisions like not opening another GUI. How do I know when my pkg is installed via the command line?

I'm hoping for some environment variable or something similar.

Ky -
  • 30,724
  • 51
  • 192
  • 308
  • This should be tagged with the operating system or distro for which the relevant packaging format and toolchain is in use. (As the pkg-file tag says, more than one OS uses that extension). – Charles Duffy Aug 15 '17 at 15:57
  • @CharlesDuffy done – Ky - Aug 15 '17 at 16:01

2 Answers2

1

Running the script via sudo will change the values of certain variables and add others in. Your script could check for the existence of those variables (or their values) to make a determination as to whether the installer was run via sudo.

Values that would get updated:

  • HOME
  • LOGNAME
  • MAIL

Values that would get set:

  • SUDO_COMMAND -- the command that was run via sudo
  • SUDO_GID -- the GID of the user that ran sudo
  • SUDO_UID -- the UID of the user that ran sudo
  • SUDO_USER -- the username of the user that ran sudo

My recommendation would be to check for the existence of the SUDO_COMMAND environment variable; it is unlikely to be set for a non-sudo installation, and would be set for a sudo-based installation.

Reference: sudo 1.8.20 manual - ENVIRONMENT section

Jeff Schaller
  • 2,352
  • 5
  • 23
  • 38
  • That's true *if* the OS-provided tools don't sanitize the environment before running pre- and post-scripts. That's definitely not always true -- RPM, for instance, tries to prevent package pre- and post-scripts from having their behavior modified by the local environment. – Charles Duffy Aug 15 '17 at 15:58
1

Since this was being run by the installer command, the COMMAND_LINE_INSTALL environment variable is set to 1. When opening the pkg normally, this variable is not set at all.

So:

if [ $COMMAND_LINE_INSTALL ]; then 
    # Do stuff for CLI land
else
    # Do stuff for GUI land
fi
Ky -
  • 30,724
  • 51
  • 192
  • 308