$ ./mactechbuds.sh
Starting MacTechBuds...
Guide loaded
Engine active
mactechbuds.com >
Back to Articles

Master macOS SysAdmin Shell Scripting Guide

1. Overview & Zsh Standard on macOS

Since macOS Catalina, Apple adopted /bin/zsh as the default system shell. System administrators script automated workflows to enforce compliance, collect inventory telemetry, and perform automated system maintenance.

Writing reliable shell scripts for macOS requires handling macOS-specific command line utilities like defaults, dscl, fdesetup, and system_profiler cleanly.

2. Script 1: Automated System Cleanup & Health Check

zsh
#!/bin/zsh
# Name: mac_sysadmin_cleanup.sh
# Purpose: Purge stale caches, verify FileVault, and output health telemetry

echo "=== macOS Health Check started at $(date) ==="

# 1. Verify FileVault Encryption Status
FV_STATUS=$(fdesetup status)
echo "[+] FileVault Status: $FV_STATUS"

# 2. Clear System Log Caches (> 30 days old)
echo "[+] Purging old temporary items..."
sudo rm -rf /Library/Caches/* ~/Library/Caches/* 2>/dev/null

# 3. Rebuild LaunchServices Database to fix broken app associations
echo "[+] Rebuilding LaunchServices..."
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister \
  -kill -r -domain local -domain system -domain user

echo "=== Cleanup completed successfully ==="

3. Script 2: Programmatic Preference Configuration via defaults

zsh
# Disable automatic spelling correction system-wide
defaults write NSGlobalDomain NSAutomaticSpellingCorrectionEnabled -bool false

# Show hidden files and extension names in Finder
defaults write com.apple.finder AppleShowAllFiles -bool true
defaults write NSGlobalDomain AppleShowAllExtensions -bool true

# Restart Finder to apply changes
killall Finder

4. Script 3: FileVault Key Validation & Battery Health

zsh
# Check Battery Cycle Count and Condition on MacBooks
system_profiler SPPowerDataType | grep -E "(Cycle Count|Condition)"

# Check if current user possesses a FileVault Secure Token
sysadminctl -secureTokenStatus $(stat -f "%Su" /dev/console)

5. Frequently Asked Questions (FAQ)

Q: Why use zsh instead of bash on macOS?

A: macOS ships with an outdated Bash 3.2 version (due to GPLv3 licensing constraints). Zsh on macOS is modern, actively supported, and default on Apple hardware.