Shell scripts are a great way to automate processes that might otherwise have to be
done manually.
They're also very helpful in extending the functionality of Jamf Pro.
Learning what a script is and how to create them can have great benefit when managing
and supporting Macs.
This is Jeremy and I'll introduce you to scripting in macOS.
A script is a collection of commands in a single file to be run in sequence.
They're commonly used to automate a repetitive process or complete a set of manual actions
the same way every time.
There are a number of places to find scripts - a safe example being Jamf Nation.
Select Resources, then Scripts, and more than 100 scripts are available for download.
Let's take a look at this script for updating Adobe Acrobat Reader DC.
The script page provides a download link, quick synopsis, and description, followed
by the full script in the Preview section.
The first line in a script is the command-line interpreter, that specifies which shell to
use when executing commands in the script.
macOS contains a variety of shells with different commands and syntax for access to the operating
system's services.
Most scripts on Jamf Nation are written using the Bourne shell or bash interpreters - bash
being short form for the Bourne again shell.
Z shell is the default login shell for new users starting in macOS Catalina, but the
Bourne shell and bash interpreters still work and bash is still the login shell for users
created prior to upgrading to Catalina.
To change the default shell for a user, check out the Apple support article, "Use zsh as
the default shell on your Mac."
You can change which shell is used in a script by simply modifying the interpreter.
However, it's a good idea to test existing Bourne shell and bash scripts with the Z shell
interpreter before deploying to managed computers in a production environment.
Z shell has most features of bash, but in some cases with a different syntax.
Without getting too technical, let's take a look at the cd command and its syntax in
the manual pages for bash compared to Z shell.
Notice the same uppercase L and P options are available in both, but the lowercase q
and s options only work in Z shell.
With all that said, I'll write a script using the Z shell interpreter since it's been available
in past versions of macOS and is now the default login shell.
A text editor is needed to get started.
There are some available in the command line and a larger selection available in the Mac
App Store with a graphical user interface.
I'm going to use the built-in command-line text editor known as nano.
First, I'll change directory to the Desktop for easy access to the script once it's created.
Type nano and then the name of the script to be created with its file extension.
I'm using .zsh for my file extension, but it has no effect on which interpreter is used
when running the script - it's merely a label.
Press Return and nano creates a new empty file for my script.
A command-line interpreter always begins with a shebang, named for its use of a pound sign
or hash, and exclamation mark, or bang.
Immediately following the shebang is the absolute path to which shell you want the script to
use.
Next, it's a good practice to add some comments to the script.
This helps other administrators, or maybe even you in the future, to understand what
the script is doing.
Comments begin with a pound sign, which tells the interpreter to ignore any text on the
line following the pound sign when the script is run.
I'll add a comment saying the first line in this command will be used to open the Jamf
Support webpage in the default browser, then type that command.
Since it may take a few seconds to open an app and load the webpage, I'll add another
comment that the script will wait three seconds before proceeding and execute this with the
sleep command.
Lastly, one more comment tells readers this script greets users by saying, "Welcome to
Jamf ." The say command does just that - converts text to audible speech - so be sure to turn
up your volume when testing to hear the results.
The reason I chose nano for this demonstration is its ease of use if you're new to text editing
in the command line.
Keyboard shortcuts at the bottom of the window let you know how to perform common tasks.
For example, I'll use the WriteOut keyboard shortcut to save this script.
Press Control-O and I'm prompted for which file name to write.
Make any changes to the file name, or leave it as is and press Return to save changes.
Notice the new Z shell script that appeared on my Desktop.
Then Control-X will exit the nano text editor.
Now the script can be tested by invoking the Z shell by typing zsh and then the path to
the script.
When the script runs, it opens my default web browser to the Jamf Support webpage, waits
three seconds, then audibly greets users.
Try writing a script of your own or testing one from Jamf Nation.
Just know that there are many other places to find scripts that may not be safe.
Never test scripts on a Mac with sensitive information, especially if it requires sudo,
or root privileges, in order to run.
That's all for our introduction to scripting in macOS.
Thanks for watching, and be sure to check out our other videos on the command-line interface
and Terminal.