From 6234809794b930ee9bcc8985bb970d1aa2ea6613 Mon Sep 17 00:00:00 2001 From: Zhineng Li Date: Fri, 9 Jan 2026 13:23:41 +0800 Subject: configure computer and host names When configuring identifier names, we use interactive prompts to ask the user for the computer name and hostname. We retrieve the current computer name via `scutil` and dynamically calculate a standardized hostname default. For better UX, we use the `read -i` option to pre-fill these values, allowing the user to simply confirm the defaults or edit them easily. Note: The `-i` option requires *Bash 4.0+*. Although macOS ships with Bash 3.2, this script runs after the Homebrew bundle installation (which includes the up-to-date bash package), ensuring the necessary shell version is available. ``` $ /bin/bash --version GNU bash, version 3.2.57(1)-release (arm64-apple-darwin25) Copyright (C) 2007 Free Software Foundation, Inc. $ /usr/bin/env bash --version GNU bash, version 5.3.9(1)-release (aarch64-apple-darwin25.1.0) Copyright (C) 2025 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. ``` --- .macos | 27 +++++++++++++++++++++++++++ Brewfile | 3 +++ 2 files changed, 30 insertions(+) diff --git a/.macos b/.macos index 5623c2d..a77815f 100755 --- a/.macos +++ b/.macos @@ -1,6 +1,33 @@ #!/usr/bin/env bash set -ex +# Ask for the permissions upfront +sudo -v + +######################################################################### +# General +######################################################################### + +# Configure identifier names +current_name=$(scutil --get ComputerName) +user_computer_name="" +user_hostname="" + +read -e -p "Enter computer name (blank to skip): " -i "$current_name" user_computer_name + +if [ -n "$user_computer_name" ]; then + sudo scutil --set ComputerName "$user_computer_name" + + default_hostname=$(echo "$user_computer_name" | sed -E 's/([a-z0-9])([A-Z])/\1-\2/g; s/[^a-zA-Z0-9]+/-/g; s/^-+|-+$//g' | tr '[:upper:]' '[:lower:]') + read -e -p "Enter hostname (blank to skip): " -i "$default_hostname" user_hostname +fi + +if [ -n "$user_hostname" ]; then + sudo scutil --set LocalHostName "$user_hostname" + sudo scutil --set HostName "$user_hostname" +fi + + ######################################################################### # Trackpad ######################################################################### diff --git a/Brewfile b/Brewfile index c2a6865..5dd8b6e 100644 --- a/Brewfile +++ b/Brewfile @@ -1,3 +1,6 @@ +# Shell +brew "bash" + # Development Tools brew "colima" brew "docker" -- cgit v1.2.3