Book Update – Moving to zsh v2

I have pushed an update for the “Moving to zsh” book.

The book is barely two weeks out but I had a few more things to add. The nice thing about self-published digital books is that they can updated quickly and often. I expect more updates over time as I continue to learn more about the subject.

As usual, the update is free when you already own the book.

If you have already purchased the book, you can go to Apple Books application on your Mac and choose ‘Check for available Downloads…’ from the ‘Store’ menu. I have seen the Mac Books app be really slow (or even completely blind) in picking up updates, you can accelerate the process by removing the local download and re-downloading the book. In iOS tap on your iCloud account icon next to ‘Reading Now’ and then choose ‘Updates.’

If you have not yet purchased the book, go get it on Apple Books!

The changes are listed here, but you can also them in the ‘Version History’ section in the book. There it links to the relevant section of the book, so you can find the changes quickly.

  • Learnt about is-at-least and updated ‘Sharing across macOS and zsh versions,’ accordingly
  • Added a section on zmv
  • Added a note to keep changes to the PATH variable up-to-date across multiple shells
  • More feedback from proof readers. (Thank you!)

If yo have read and enjoyed the book, please leave a review on the Apple Books store!

Weekly News Summary for Admins — 2019-11-29

Happy day after Thanksgiving to the US readers! Happy “week where we can take a breath from tech news” for everyone else!

It is tradition to reflect on things you can be grateful for. There are many standard replies, such as family, friends, and—more particular to this newsletter—the amazing MacAdmins community. But, this year I wanted to say that I am grateful to be working in a field that is always changing.

I started out using and managing Macs in the nineties. Back then Apple had a strong market in the print and publishing business and education, but nearly nowhere else. It was obvious that the Macintosh operating system needed a complete overhaul to remain relevant, but Apple’s attempts to build a new system failed. Apple was considered stagnant and irrelevant. The change came in 1996 when Apple bought NeXT and Steve Jobs returned to the company. The iMac, iBook, and Mac OS X saved Apple.

Even though, in hindsight, it is obvious that Mac OS X was an important part of that transition, there were many back then who rejected the new system. There were many reasons for being conservative: the iMac, iBook, and even the blue PowerMac towers looked like “toys,” third party applications were slow to adopt Mac OS X, the new OS was not yet ready for some critical workflows, Windows and Linux were tempting alternatives.

In the beginning, it was mainly new users who would embrace the new Mac OS. Java and web developers, as well as scientists, who realized there was now a Unix(-like) platform with a nice UI and MS Office (one of the first major commercial app suites to have a Mac OS X version). It always felt like the “traditional” Mac users had to be dragged along.

Apple has changed multiple times since then: online and retail stores, the iPod, the iTunes Store, the iPhone, the App Store, the iPad, the Apple Watch, and a focus on services.

There were also failures, dead ends, and missteps along the way: the hockey puck mouse, the Cube, the various incarnations of iTools/dotMac/Mobile me/iCloud, server hardware and software, the butterfly keyboards… In the MacAdmin space we had WebObjects, NetInfo, MCX, Workgroup Manager, NetBoot, NetRestore, Profile Manager, Mac OS (X) Server, bash3 and Python2…

We often complain about the pace of change in the technology field. I agree the entire field would be well-served with more consideration, rather than rushing features at all cost to fulfill an arbitrary, self-imposed deadline. But the absence of change is stagnation and irrelevancy.

I remember when Apple was considered stagnant and irrelevant. I really don’t miss those times.

Happy Thanksgiving!

Talking about change: My new book “Moving to zsh” is now available in the Apple Books Store.

Howard Oakley from the Eclectic Light Company calls it: “[T]imely, invaluable, and very clearly written for anyone who ever uses the command line.”

Packaging for Apple Administrators” and “Property List, Preferences, and Profiles for Apple Admnistrators” are on sale until Dec 3. I also permanently dropped the price for “macOS Installation.”

If you would rather get the weekly newsletter by email, you can subscribe to the Scripting OS X Weekly Newsletter here!! (Same content, delivered to your Inbox once a week.)

On Scripting OS X

News and Opinion

MacAdmins on Twitter

  • Rich Trouton: “Need a quick way to check Apple’s system status boards? System status: isappleup.com Developer system status: developer.isappleup.com
  • Victor (groob): “Adding async command information to @micromdm_io Admins will be able to check the status of each command in the queue and see the raw request/response at any time.” (video)

Bugs and Security

Support and HowTos

Scripting and Automation

Updates and Releases

To Watch

To Listen

Support

There are no ads on my webpage or this newsletter. If you are enjoying what you are reading here, please spread the word and recommend it to another Mac Admin!

If you want to support me and this website even further, then consider buying one (or all) of my books. It’s like a subscription fee, but you also get a useful book or two extra!

Comparing Version strings in zsh

Another excerpt from the book “Moving to zsh.” I found this one so useful, I thought I’d like to share it.

You can get the version of zsh with the ZSH_VERSION variable:

% echo $ZSH_VERSION
5.7.1

And you can get the version of macOS with the sw_vers command:

% sw_vers -productVersion
10.15.1

Comparing version strings is usually fraught with potential errors. Strings are compared by the character code for each character.‘2’ is alphabetically greater than ‘10’ when compared as strings, because the character code for 2 is greater than the character code for 1. So, a string comparison of macOS version numbers will return that 10.9.5 is greater than 10.15.1.

Zsh, however, provides a function is-at-least which helps with version string comparisons.

With a single argument, is-at-least will return if the current zsh version matches or is higher than a given number:

if ! is-at-least 2.6-17; then
  echo "is-at-least is not available"
fi

When you provide two arguments to is-at-least, then the second argument is compared (using version string rules) with the first and needs to match or be higher:

autoload is-at-least
if is-at-least 10.9 $(sw_vers -productVersion); then
  echo "can run Catalina installer"
else
  echo "cannot run Catalina installer"
fi

Note: when used in a script, you will probably have to autoload is-at-least before using it. In an interactive shell, it is often already loaded, because many other autoloader functions will have already loaded it.

Black Friday/Cyber Monday Sale

It is Thanksgiving week in the US, which means that all real-world and online retailers are luring buyers with all kinds of crazy sales.

Here at Scripting OS X, I keep the book prices low all year round. My latest book “Moving to zsh” is US$9.99 all the time.

I also just permanently lowered the price on “macOS Installation.”

Nevertheless, starting today and up to Dec 3, I will also put “Packaging for Apple Administrators” and “Property Lists, Preferences, and Profiles for Apple Administrators” on sale!

And not just for US readers, but on all regions where the books are available in the Apple Books store!

Happy Thanksgiving!

Weekly News Summary for Admins — 2019-11-22

My new book, “Moving to zsh,” is now available in the Apple Books Store!

If you have been reading this newsletter for a while, you know what this book is about, otherwise you can read all the details in my blog post. This is the first of my books that is not targeted specifically at Mac Admins, but is useful to anyone who use the Terminal on the Mac.

Please, spread the word to friends, co-workers, fellow admins, developers, and power-users who you think would benefit from this book. Thank you all for your support.

If you would rather get the weekly newsletter by email, you can subscribe to the Scripting OS X Weekly Newsletter here!! (Same content, delivered to your Inbox once a week.)

On Scripting OS X

News and Opinion

MacAdmins on Twitter

  • Timo Perfitt: “New MacBook Pro 16” in the house!… ” (Thread)
  • Tech Girl: “It’s weird jamf sells jamf protect and treats patch like their child they’re pretending doesn’t exist. How many #macadmin pay for jamf also need jamjar/autopkgr/Munki to properly patch & notify users?”
  • Victor (groob): “Is there someone at Apple who understands that security updates are not optional and that an enterprise might want to enforce a deadline? Lack of MDM options to make this possible suggests otherwise.” (Thread)
  • Carl Ashley: “Things your postinstall scripts are doing that are 100% bad 100% of the time: cp /tmp/foo.app /Applications/foo.app; chmod 777 /Applications/Foo.app; chown root:admin /Applications/Foo.app” (Thread)

Bugs and Security

Support and HowTos

Scripting and Automation

Apple Support

Updates and Releases

To Watch

To Listen

Support

There are no ads on my webpage or this newsletter. If you are enjoying what you are reading here, please spread the word and recommend it to another Mac Admin!

If you want to support me and this website even further, then consider buying one (or all) of my books. It’s like a subscription fee, but you also get a useful book or two extra!

Install shellcheck binary on macOS (updated)

A few months back I wrote a post on how to compile and build an installer for the shellcheck binary for macOS.

Just a few weeks later, the shellcheck project added a download for a pre-compiled binary for macOS. You can now download the binary with this link:

https://shellcheck.storage.googleapis.com/shellcheck-latest.darwin.x86_64.tar.xz

Ironically, macOS can unarchive xz archives when you double click them in the Finder, but there is no command line tool on macOS to unarchive them. In the previous post, I ran into the same problem, and there you can find instructions on how to install the xz tools on macOS.

Update 2020-03-26: I was unnecessarily complicating this. You can use tar to unarchive this:

tar -xf shellcheck-latest.darwin.x86_64.tar.xz

After downloading and un-archiving, you can manually move the shellcheck binary to a suitable directory. The standard location is /usr/local/bin.

For manual installations, this is it! Much simpler than before. Thank you!

Note: if you want the man page as well, you still need to build it with pandoc from the source.

Build a pkg for managed deployment

If you are a MacAdmin and want to distribute shellcheck with your management system, you will need to build an installer package (pkg).

Instead of copying the binary to /usr/local/bin, place it in a payload folder in a project folder. Then build the pkg with pkgbuild:

% mkdir -p ShellcheckPkg/payload
% cp ~/Downloads/shellcheck-latest/shellcheck ShellcheckPkg/payload
% pkgbuild --root ShellcheckPkg/payload --identifier com.example.shellcheck --version 0.7.0 --install-location /usr/local/bin shellcheck-0.7.0.pkg

Replace the 0.7.0 with the actual version number.

Automated Package creation with autopkg

And because all of this isn’t really that difficult, I built autopkg recipes for Shellcheck You can find them in my recipe repository or with autopkg search shellcheck. Enjoy!

New Book Release Day: Moving to zsh

My new book: “Moving to zsh” is now available on the Apple Books Store!

The book should be useful for anyone who uses macOS Terminal and is wondering what the change of the default shell in Catalina means and how to best handle the transition. The book describes the motivation for Apple (and the user) to “move to zsh” and how to get the most out of the new shell.

It is based on the series of blog posts that I posted from June through August, but reworked and expanded with more detail and more topics. Some of the information from my MacSysAdmin presentation also made it into the book.

The blog series added up to about 11K words, and the book, in its current form, is more than 22K words. Compared to the series, I have added images, movies, clarifications, more examples, and several new sections and appendices.

This books explains:
– why Apple is changing the shell
– implications for infrequent and expert Terminal users
– how to move from bash to zsh
– configuring zsh to be more productive
– moving scripts from bash to zsh

And this will certainly not be the end for “Moving to zsh.” Like my other books, I plan to update and add to it after release as well, keeping it relevant and useful.

This is my first book that is not targeted mainly at MacAdmins. I believe this book will be useful for any Mac user that uses the Terminal frequently: Developers, web admins, scientists, and other power users. Please help spread the news by sharing this post and the book link with friends, co-workers, and across social media. Thank you!

Go get “Moving to zsh” on the Apple Books Store!

Weekly News Summary for Admins — 2019-11-15

Hard to believe, but Apple software updates this week (except for new beta releases). Instead we got a new MacBook Pro! (And yes, it has a new keyboard—I really hope we can put this behind us now…)

Also Jamf Nation User Conference in Minneapolis! Hope everyone had a great time and safe travels home!

Progress update: my new book “Moving to zsh.” has been sent to the proof readers!

If you would rather get the weekly newsletter by email, you can subscribe to the Scripting OS X Weekly Newsletter here!! (Same content, delivered to your Inbox once a week.)

Headlines

On Scripting OS X

News and Opinion

MacAdmins on Twitter

  • Timo Perfitt: “In prep for the MDS 2 release, I created some new videos and postings for the new MDM service in MDS.” (Thread)
  • Patrick Fergus: “If anyone was using a CLI uninstall of an Adobe product, the path to Setup changed, assumedly with CCDA 5”
  • Rosyna Keller: “Some fun new stuff with altool 4.0 (Xcode 11.x) is now available.” (Thread)

Jamfnation User Conference

Bugs and Security

Support and HowTos

Scripting and Automation

Updates and Releases

To Listen

Support

There are no ads on my webpage or this newsletter. If you are enjoying what you are reading here, please spread the word and recommend it to another Mac Admin!

If you want to support me and this website even further, then consider buying one (or all) of my books. It’s like a subscription fee, but you also get a useful book or two extra!

Associative arrays in zsh

This is an excerpt from my book “Moving to zsh” which is available for order on the Apple Books Store.

One of the advantages of zsh over bash 3 is the support of “associative arrays,” a data structure known as hash tables or dictionaries in other languages.

In associative arrays, you can store a piece of data, or value with an identifying ‘key’. For example, the associative array userinfo has multiple values, each identified with a key:

% echo $userinfo[name]
armin
% echo $userinfo[shell]
bash
% echo $userinfo[website]
scriptingosx.com

Note: bash 4 also added associative arrays, but they are implemented slightly differently.

Creating associative arrays

In zsh, before you can use a variable as an associative array, you have to declare it as one with

declare -A userinfo

This will tell the shell that the userinfo variable is an associative array. You can also use typeset -A as an alternative syntax. You can verify the type of the variable:

% echo ${(t)userinfo}
association

You can then set the key-value pairs of the userinfo associative array individually:

userinfo[name]="armin"
userinfo[shell]=bash
userinfo[website]="scriptingosx.com"

When you set the value for an existing key again, it will overwrite the existing value:

% echo $userinfo[shell]
bash
% userinfo[shell]=zsh
% echo $userinfo[shell]
zsh

Setting the values for each key is useful in some situations, but can be tedious. You can also set the entire associative array at once. There are two syntaxes for this in zsh:

userinfo=( name armin shell zsh website scriptingosx.com )

This format follows the format ( key1 value1 key2 value2 ...). The other syntax is more verbose and expressive:

userinfo=( [name]=armin [shell]=zsh [website]="scriptingosx.com" )

When you set the associative array variable this way, you are overwriting the entire array. For example, if you set the userinfo for ‘armin’ like above and then set it later like this, the website key and value pair will have been overwritten as well:

% userinfo=( [name]=beth [shell]=zsh )
% if [[ -z $userinfo[website] ]]; then echo no value; fi
no value

If you want to partially overwrite an existing associative array, while leaving the other key/value pairs intact, you can use the += operator:

% userinfo+=( [shell]=fish [website]=fishshell.com )
% echo $userinfo[name]                                           
beth
% echo $userinfo[shell]
fish
% echo $userinfo[website]
fishshell.com

To clear an associative array, you can use:

% userinfo=( )

Retrieving data from an associative array

We have already seen you can get the value for a given key with the ‘subscript’ notation:

% echo $userinfo[name]                                           
beth

When you access the $userinfo variable directly, you will get a normal array of the value:

% echo $userinfo
beth fish fishshell.com

You can also get an array of the keys with this syntax:

% echo ${(k)userinfo}
name shell website

or a list of both keys and values:

% echo ${(kv)userinfo} 
website fishshell.com shell fish name beth

You can use this to copy the data from one associative array to another:

% declare -A otherinfo
% otherinfo=( ${(kv)userinfo )
% echo $otherinfo[name]
beth

You can also use this to loop through all the keys and values of an associated array:

for key value in ${(kv)userinfo}; do
    echo "$key -> $value"
done

#output
website -> fishshell.com
shell -> fish
name -> beth

Limitations

Associative arrays have their uses, but are not as powerful as dictionaries in more powerful languages. In zsh, you cannot nest associative arrays in normal arrays, which limits their use for complex data structures.

There is also no functionality to transfer certain file formats, like XML or property lists directly in to associative arrays or back.

Shell scripting was never designed for complex data structures. When you encounter these limitations, you should move “up” to a higher level language, such as Python or Swift.

Weekly News Summary for Admins — 2019-11-08

Another week, … yes, really… another update. iOS 13.2.2 dropped last night. At this rate, I am surprised there is no Catalina 10.15.1 Supplemental update yet. Maybe next week. We did also get new betas for all the next updates.

In other news, Adobe is finally shipping Photoshop for iPad, and announced Lightroom and Illustrator for the future. I will leave the discussion if this is really a “full” Photoshop or not to others. But I will point out, that this is yet another “Pro” application that is now available on iPad. There are still plenty of reasons or excuses to have a Mac over an iPad, but they are dwindling.

Progress update: I got a lot of work done on my new book “Moving to zsh.” I had set the delivery date to Dec 31, because the Books Store interface required me to enter a date, but I am quite confident now that I will beat that.

If you would rather get the weekly newsletter by email, you can subscribe to the Scripting OS X Weekly Newsletter here!! (Same content, delivered to your Inbox once a week.)

#! On Scripting OS X

📰News and Opinion

🐦On Twitter

  • Rene Ritchie: “I’d love both word count and a plain text option in Notes .app.… ”
  • Bryson Tyrrell: “I think I’ve been vastly under-calculating the amount of traffic that Community Patch serves.” (Thread)
  • Arnold: “Xcode, all you ever tell me is WARNINGS and ERRORS, it’s always problems and bad things with you, that’s so toxic would it kill you to say nice things about my code every once in a while?…” (Great Mockups)

🐞Bugs and Security

🔨Support and HowTos

🤖Scripting and Automation

🍏Apple Support

♻️Updates and Releases

🎧To Listen

📚 Support

There are no ads on my webpage or this newsletter. If you are enjoying what you are reading here, please spread the word and recommend it to another Mac Admin!

If you want to support me and this website even further, then consider buying one (or all) of my books. It’s like a subscription fee, but you also get a useful book or two extra!