Skip to content

Instantly share code, notes, and snippets.

@alexedwards
Created November 22, 2023 14:53
Show Gist options
  • Save alexedwards/0c1539836d8c254cd331ab87bd0414f1 to your computer and use it in GitHub Desktop.
Save alexedwards/0c1539836d8c254cd331ab87bd0414f1 to your computer and use it in GitHub Desktop.
Bash script for updating Go
#!/bin/bash
# Check if the version argument is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 <go_version>"
exit 1
fi
# Version number provided as an argument
version="$1"
# URL of the file to download
url="https://go.dev/dl/go$version.linux-amd64.tar.gz"
# Directory to store the downloaded file
download_dir="/tmp"
# Ensure the download directory exists
mkdir -p $download_dir
# Download the file
echo "Downloading $url..."
if ! curl -o "$download_dir/go$version.linux-amd64.tar.gz" -L "$url"; then
echo "Failed to download the file. Exiting."
exit 1
fi
# Remove the existing Go installation
echo "Removing existing Go installation..."
sudo rm -rf /usr/local/go
# Extract the downloaded archive to /usr/local
echo "Extracting the downloaded file to /usr/local..."
sudo tar -C /usr/local -xzf "$download_dir/go$version.linux-amd64.tar.gz"
# Check if the extraction was successful
if [ $? -eq 0 ]; then
echo "Go $version has been successfully installed."
else
echo "Failed to install Go. Please check the download and extraction process."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment