Shells come with a magic variable $0 for performing various operations. We can use these to perform certain functions. In its simplest incantation we can just echo out $0 to get the path a script is in from within the script: echo $0 We can also just get the directory a script is in. For example, if we want to see if it’s being executed from within an app bundle, temp, or download directory. This is also helpful if we’ve created files in a folder we created and need to delete them all at the end of a larger atomic operation (e.g. rm -r …/<the directory name>. To do that…
-
-
Add Commands To The Shell Built-in Commands
Some shell commands are internal built-in commands. This allows them to change shell process states and when they’re internal they can be really fast as they don’t have to load another program. Some machines are dedicated to running a couple of scripts. Adding a command to the built-ins for those can make them all the faster. To enable a bult-in, use the enable command followed by a -f, the path/uri to the binary, and then the name you want it to be called as. Let’s say we want to call /usr/local/devopsmagic as devopsmagicb, that would be run as follows: enable -f /usr/local/devopsmagic devopsmagicb I’m not a big fan of doing…
-
Get Telemetry on App and System Extensions in macOS
Application extensions allow developers to import common SDKs into projects so they can build increasingly interesting apps without developing a lot of code for those things vendors expose. The Apple extensions typically allow a developer to bring in various Apple libraries and then call them in their code. For example, com.apple.quicklook.thumbnail is used to produce thumbnails in quicklook; therefore Apple apps like iBooks and Shortcuts and any 3rd party developer like MindNode that wants to use Quicklook can provide a known and so somewhat seamless user experience. Click on the Privacy & Security System Preferences and then Extensions and then Quick Look to see the non-Apple apps that use the…
-
One Liner to Loop Through a Directory for Last Used Date of Apps on a Mac
Here we load in a list of Apps in the /Applications directory and then echo them along with the kMDItemLastUsedDate from Spotlight via mdls: ls /Applications | while read APP;do echo "$APP" ; echo `/usr/bin/mdls /Applications/"$APP" | /usr/bin/grep -w kMDItemLastUsedDate`; done The output per item would then look something like this: Apple Configurator.app kMDItemLastUsedDate = 2022-09-27 18:25:21 +0000 awk can get the $3 if that’s all that’s needed or other filtering tools can limit the output. Or get more output, like a bundle ID (kMDItemCFBundleIdentifier) or an Apple Store ID (kMDItemAppStoreAdamID) for parsing through other tools. Further no need to echo the string of the name of the app according…
-
Inverse Case Insensitive Multiple Matching for grep
The apropos command is used to show man pages that exist on Linux, Mac, and Unix. It’s useful to, let’s say, look for a command that includes a given word in the description. It can also be used to show all commands that have man pages (and some that don’t). To see all commands for a machine, simply run apropos with a wildcard (*): apropos * This will take a while. Not all commands can actually be used. Constrain output (which takes no less runtime) by piping the output to a grep command. The -i option can be used to make grep parsing case insensitive. This means that if we…
-
Debugging Bash Scripts with bashdb
For years I’ve been adding a line in bash scripts to echo out a variable here and there. It’s funny how you just accept a workflow like that as “the way.” But when using an IDE for objected oriented languages I’ve always just expected breakpoints and the ability to watch variables when running code from within the IDE. But it never even occurred to me to do that with bash scripts. Then IsaacH at the office told me about an IDE that does that. Looking inside the IDE, it’s piping commands to bashdb. Soooo, bashdb is kinda’ like gdb. You can set breakpoints and print the content of variables when…
-
Scripting the Temp and Cache Directories on a Mac
Ever see a weird folder like /var/folders/g0/lr10g_wx4t75s2hd5129qhkc0000gn/T/ and wonder why it’s there? Those are usually Darwin or shell temporary or cache directories. They are where those weird temp directories you need to access are written to and read from. Apple uses these to host volatile sql databases and a number of scripts use them to house a file and protobuffs prior to processing. You too can access them in your scripts when needed. The most common is a built-in shell temp that you can easily access just by echoing out the contents of $TMPDIR: echo $TMPDIR Darwin has some as well; most notably in DARWIN_USER_CACHE_DIR and DARWIN_USER_TEMP_DIR where each can…
-
Programmatically Generate qrcodes on macOS
I recently needed to make device URLs easy for a service desk team to access. To do so I just piped the URLs into a little app called qrencode. First, I needed to install qrencode, which is pretty easy to do using brew: brew install qrencode Once installed, it’s trivial to pipe the quoted URL into the app with an output (or -o) into a png file. For example: echo "http://thehistoryofcomputing.libsyn.com" | qrencode -o ~/Desktop/History.png Once generated, you can use the camera on an iOS device to automatically open URLs by pointing them at the code and then tapping the notification that appears.
-
What Applications Invoke Persistent Processes On A Mac?
I’m increasingly concerned about applications that act as no applications should need to in the modern era of Apple device management. As such, a simple (albeit computationally expensive) way of performing this type of operation is to simply grep a directory with applications that contain those strings for further inspection: grep -r -l "LaunchDaemons" /Applications grep -r -l "LaunchAgents" /Applications grep -r -l ".kext" /Applications grep -r -l ".pkg" /Applications You can also use find: find /Applications -type f -exec grep -l 'LaunchDaemons' {} \; The nice part of using find here is that you can -exec a lot of options without the same type of piping. You can also…
-
Differences Between zsh and bash
Supposedly, macOS 10.15 Catalina is slated to replace the default /bin/bash shell with zsh, or /bin/zsh. Before we talk about the differences let’s just say that bash is still here and if your script is called as bash then it will still work just fine. To quickly see which you’re using (e.g. when testing a new release), use $0: echo $0 Z Shell or zsh for short was written by Princeton University student Paul Falstad in 1990. Most shells are just extensions of the Bourne shell (including bash) and work similarly but there are minor differences here and there. Yes, Z Shell comes with a control-R reverse incremental search, but…