Skip to content

Instantly share code, notes, and snippets.

@nordinrahman
Last active October 2, 2024 08:31
Show Gist options
  • Save nordinrahman/a1d23f7bb0dfdf4d7ce1175af549a914 to your computer and use it in GitHub Desktop.
Save nordinrahman/a1d23f7bb0dfdf4d7ce1175af549a914 to your computer and use it in GitHub Desktop.
Script to install zsh on Windows, without WSL
#!/bin/bash
# This script installs or updates Zsh on Windows using Git Bash.
# It requires elevated privileges and the following commands:
# - git
# If Git is not installed, the script will stop.
# The script will:
# 1. Check if it is running with elevated privileges on Windows and stop if it is not.
# 2. Check if Git is installed and stop if it is not.
# 3. Automatically determine the latest version of zstd, download, and install it if it is not available and Zsh needs to be installed or updated.
# 4. Emit help text with usage and requirements if any checks fail.
# 5. Avoid duplicate entries in .bashrc for setting Zsh as the default shell.
# 6. Only install Oh My Zsh if it is not already installed.
# 7. Proceed with the Zsh installation and update process if all checks pass.
# Function to display help text
display_help() {
echo "Usage: ./install_zsh.sh"
echo ""
echo "This script installs or updates Zsh on Windows using Git Bash."
echo "It requires elevated privileges and the following commands:"
echo " - git"
echo ""
echo "Steps to run the script:"
echo "1. Open Git Bash as an administrator."
echo "2. Navigate to the directory where you saved the script."
echo "3. Make the script executable: chmod +x install_zsh.sh"
echo "4. Run the script: ./install_zsh.sh"
exit 1
}
# Function to check if the script is running with elevated privileges on Windows
check_admin() {
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
net session > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "This script must be run as an administrator."
display_help
fi
fi
}
# Function to check if Git is installed
check_git_installed() {
if ! command -v git &> /dev/null; then
echo "Git is not installed or not in the PATH."
display_help
fi
}
# Function to download and install the latest zstd if not available
install_zstd() {
if ! command -v zstd &> /dev/null; then
echo "zstd is not installed. Downloading and installing the latest zstd..."
LATEST_ZSTD_URL=$(curl -s https://api.github.com/repos/facebook/zstd/releases/latest | grep "browser_download_url.*win64.zip" | cut -d '"' -f 4)
curl -L -o zstd.zip $LATEST_ZSTD_URL
unzip zstd.zip -d zstd
cp zstd/zstd.exe "$GIT_INSTALL_DIR/usr/bin/"
rm -rf zstd zstd.zip
fi
}
# Check for elevated privileges
check_admin
# Check if Git is installed
check_git_installed
# Define variables
ZSH_PAGE_URL="https://packages.msys2.org/package/zsh?repo=msys&variant=x86_64"
GIT_INSTALL_DIR=$(find_git_install_dir)
# Download the Zsh package page
echo "Downloading Zsh package page..."
curl -L -o zsh_page.html $ZSH_PAGE_URL
# Extract the latest Zsh version from the page
LATEST_ZSH_VERSION=$(grep -oP 'zsh-\K[0-9]+\.[0-9]+\.[0-9]+' zsh_page.html | head -n 1)
INSTALLED_ZSH_VERSION=$(get_installed_zsh_version)
echo "Installed Zsh version: $INSTALLED_ZSH_VERSION"
echo "Latest Zsh version: $LATEST_ZSH_VERSION"
# Compare versions and download if necessary
if version_greater "$LATEST_ZSH_VERSION" "$INSTALLED_ZSH_VERSION"; then
echo "Updating Zsh to version $LATEST_ZSH_VERSION..."
# Ensure zstd is installed
install_zstd
ZSH_DOWNLOAD_URL=$(grep -oP 'https://mirror.msys2.org/msys/x86_64/zsh-[^"]+\.pkg\.tar\.zst' zsh_page.html | head -n 1)
curl -L -o zsh.pkg.tar.zst $ZSH_DOWNLOAD_URL
# Extract Zsh package using tar
echo "Extracting Zsh package..."
tar -I zstd -xvf zsh.pkg.tar.zst -C "$GIT_INSTALL_DIR"
# Clean up
echo "Cleaning up..."
rm zsh.pkg.tar.zst
else
echo "Zsh is already up to date."
fi
# Clean up the downloaded page
rm zsh_page.html
# Set Zsh as the default shell if not already set
if ! grep -q 'exec zsh' ~/.bashrc; then
echo "Setting Zsh as the default shell..."
echo 'if [ -t 1 ]; then exec zsh; fi' >> ~/.bashrc
else
echo "Zsh is already set as the default shell in .bashrc."
fi
# Install Oh My Zsh (optional)
if [ ! -d "$HOME/.oh-my-zsh" ]; then
echo "Installing Oh My Zsh..."
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
else
echo "Oh My Zsh is already installed."
fi
echo "Zsh installation completed!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment