Skip to content

Instantly share code, notes, and snippets.

@RandallFlagg
Created March 24, 2025 13:36
Show Gist options
  • Save RandallFlagg/d5a439d1647a321695f655e812d3db71 to your computer and use it in GitHub Desktop.
Save RandallFlagg/d5a439d1647a321695f655e812d3db71 to your computer and use it in GitHub Desktop.
Linux Scripts
#!/usr/bin/env fish
# Set variables
set TMP_DIR (mktemp -d) # Create a temporary folder
set DOWNLOAD_URL "https://code.visualstudio.com/sha/download?build=stable&os=linux-x64"
set HARDCODED_TARGET_DIR "/path/to/" # Change this to your desired folder
set DIR_TO_BACKUP "/path/to/VSCode-linux-x64/" # Change this to your desired folder
# Use HARDCODED_TARGET_DIR as the target directory
set TARGET_DIR (string replace -r '/+$' '' $HARDCODED_TARGET_DIR) # Normalize HARDCODED_TARGET_DIR to remove trailing slashes
# Use DIR_TO_BACKUP for backup operations
set DIR_TO_BACKUP (string replace -r '/+$' '' $DIR_TO_BACKUP) # Normalize DIR_TO_BACKUP to remove trailing slashes
# Debugging to verify normalized values
echo "Normalized TARGET_DIR: '$TARGET_DIR'"
echo "Directory to back up: '$DIR_TO_BACKUP'"
# Step 1: Download file to temporary folder
echo "Downloading VSCode to temporary folder: $TMP_DIR..."
wget -O "$TMP_DIR/vscode.tar.gz" "$DOWNLOAD_URL"
# Step 2: Kill the process named 'code'
echo "Killing VSCode process..."
pkill -f code; or echo "VSCode process not running."
# Step 3: Rename existing destination directory if it exists
if test -d "$DIR_TO_BACKUP"
set BACKUP_DIR "$DIR_TO_BACKUP"_backup_(date +%s | string trim) # Create a backup with a timestamp
echo "Renaming existing directory $DIR_TO_BACKUP to $BACKUP_DIR..."
mv "$DIR_TO_BACKUP" "$BACKUP_DIR"
end
# Step 4: Extract the downloaded file to the target directory
echo "Extracting VSCode to: $TARGET_DIR..."
mkdir -p "$TARGET_DIR" # Ensure the target directory exists
tar -xzf "$TMP_DIR/vscode.tar.gz" -C "$TARGET_DIR"
# Step 5: Verify the extraction was successful and delete the backup directory
if test $status -eq 0
echo "Extraction completed successfully."
if test -d "$BACKUP_DIR"
echo "Deleting old backup directory $BACKUP_DIR..."
rm -rf "$BACKUP_DIR"
end
else
echo "Extraction failed. Restoring the old directory..."
if test -d "$BACKUP_DIR"
mv "$BACKUP_DIR" "$DIR_TO_BACKUP"
end
echo "Update aborted."
rm -rf "$TMP_DIR" # Clean up temporary files
exit 1
end
# Step 6: Clean up temporary files
echo "Cleaning up temporary files..."
rm -rf "$TMP_DIR"
echo "VSCode setup completed successfully!"
#!/bin/bash
# Set variables
TMP_DIR=$(mktemp -d) # Create a temporary folder
DOWNLOAD_URL="https://code.visualstudio.com/sha/download?build=stable&os=linux-x64"
HARDCODED_TARGET_DIR="/path/to/" # Change this to your desired folder
DIR_TO_BACKUP="/path/to/VSCode-linux-x64/" # Change this to your desired folder
# Normalize TARGET_DIR by removing trailing slashes
TARGET_DIR=$(echo "$HARDCODED_TARGET_DIR" | sed 's:/*$::')
DIR_TO_BACKUP=$(echo "$DIR_TO_BACKUP" | sed 's:/*$::')
# Debugging to verify normalized values
echo "Normalized TARGET_DIR: '$TARGET_DIR'"
echo "Directory to back up: '$DIR_TO_BACKUP'"
# Step 1: Download file to temporary folder
echo "Downloading VSCode to temporary folder: $TMP_DIR..."
wget -O "$TMP_DIR/vscode.tar.gz" "$DOWNLOAD_URL"
# Step 2: Kill the process named 'code'
echo "Killing VSCode process..."
pkill -f code || echo "VSCode process not running."
# Step 3: Rename existing destination directory if it exists
if [ -d "$DIR_TO_BACKUP" ]; then
BACKUP_DIR="${DIR_TO_BACKUP}_backup_$(date +%s)"
echo "Renaming existing directory $DIR_TO_BACKUP to $BACKUP_DIR..."
mv "$DIR_TO_BACKUP" "$BACKUP_DIR"
fi
# Step 4: Extract the downloaded file to the target directory
echo "Extracting VSCode to: $TARGET_DIR..."
mkdir -p "$TARGET_DIR" # Ensure the target directory exists
tar -xzf "$TMP_DIR/vscode.tar.gz" -C "$TARGET_DIR"
# Step 5: Verify the extraction was successful and delete the backup directory
if [ $? -eq 0 ]; then
echo "Extraction completed successfully."
if [ -d "$BACKUP_DIR" ]; then
echo "Deleting old backup directory $BACKUP_DIR..."
rm -rf "$BACKUP_DIR"
fi
else
echo "Extraction failed. Restoring the old directory..."
if [ -d "$BACKUP_DIR" ]; then
mv "$BACKUP_DIR" "$DIR_TO_BACKUP"
fi
echo "Update aborted."
rm -rf "$TMP_DIR" # Clean up temporary files
exit 1
fi
# Step 6: Clean up temporary files
echo "Cleaning up temporary files..."
rm -rf "$TMP_DIR"
echo "VSCode setup completed successfully!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment