Category filter

Fix common issues while executing custom scripts on Mac

Case 1:

Unable to execute scripts. Argument values are not parsed as expected.

While executing the scripts, the argument values are not parsed to the corresponding variables used in the shell scripts. Hence, the scripts are not executed successfully.

Reason:

Failed to parse the argument value as a single entity as it is enclosed in quotes when passed from the Hexnode portal.

For instance,

The shell script to add a user to the device:

And, if a value “Adam Johns” is passed as $1, it is not considered a single component but two different components, “Adam” and “John”. Hence, the values might not be parsed correctly.

Solution:

Always enclose the argument value in single quotes if it contains two or more words separated by spaces while passing.

Case 2:

Running the Homebrew commands as scripts from the Hexnode portal generates the error “Running Homebrew as root is extremely dangerous and no longer supported. As Homebrew does not drop privileges on installation, you would be giving all build scripts full access to your system.”

Reason:

By default, Hexnode executes the script command at the root level.

Solution:

Homebrew commands need not be executed at the root level. In cases where you do not want specific commands to be run at the root level, you can explicitly execute them at the user level. The following command helps to get the logged-in details of the current user on the device:


currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' )

Include the above command on the script. Then proceeding with the necessary operation as the currentUser helps you run it at the given user level.

For example, the below command changes the default dock orientation of the given user to the value Left.


currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' )


sudo -u "$currentUser" defaults write com.apple.dock orientation left

Case 3:

Homebrew command raises the “Command not found” error.

Reason:

There might be an issue with the Homebrew path variable.

Solution:

Specify the full path (default location of Homebrew) for the path variable in the scripts.

For example:

  • Troubleshooting Guides