Skip to content

Instantly share code, notes, and snippets.

@robeeeert
Created March 19, 2022 19:59
Show Gist options
  • Save robeeeert/e078fe9baa0bb18d9f886a945f2da7a0 to your computer and use it in GitHub Desktop.
Save robeeeert/e078fe9baa0bb18d9f886a945f2da7a0 to your computer and use it in GitHub Desktop.
Install or update VS code on any Linux systemand backup the previously installed version
#!/bin/bash
if [ $# -gt 0 ]; then
if [ "$1" == "help" ]; then
echo "Usage: $0 [help | <installation_destination_path>]"
exit 0
else
destination="$1"
fi
fi
pwd=${PWD}
tmp_file="vscode.tar.gz"
url="https://code.visualstudio.com/sha/download?build=stable&os=linux-x64"
binary="/usr/local/bin/code"
if [ -z "$destination" ]; then
destination="/opt/vscode"
fi
echo "Updating VSCode"
echo "Download URL: $url"
echo "Install destination: $destination"
echo "Binary destination: $binary"
echo "Temporary download destination: $tmp_file"
cd /tmp
echo "Downloading new version..."
curl --location "https://code.visualstudio.com/sha/download?build=stable&os=linux-x64" > $tmp_file
echo "Extracting vscode..."
vscode_folder=$(tar tf "$tmp_file" | cut -d "/" -f 1 | sort -u)
tar xf "$tmp_file"
if [ -d "$destination" ]; then
if [ -d "$destination"_bak ]; then sudo rm -rf "$destination"_bak; fi
echo "Backing up previous version..."
sudo mv "$destination" "$destination"_bak
fi
echo "Moving new version..."
sudo mv "$vscode_folder" "$destination"
if [ ! -f "$binary" ]; then
echo "Registering executable..."
sudo ln -s "$destination"/code "$binary"
fi
echo "Cleaning up..."
rm -f "$tmp_file"
echo "Update successful"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment