Skip to content

Instantly share code, notes, and snippets.

@dayne
Last active January 31, 2024 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dayne/40c45988e7ecfd9b74b954e711470b9c to your computer and use it in GitHub Desktop.
Save dayne/40c45988e7ecfd9b74b954e711470b9c to your computer and use it in GitHub Desktop.
#!/bin/bash
# Script to download and then install latest .deb Discord client.
# Created 2024-01-29 by Dayne Broderson
#
# Script Overview
# 1. Sends a request to the Discord URL to get the headers and extract the
# location header, which contains the final download URL.
# 2. It extracts the desired filename from the final download URL and cleans
# up any trailing whitespace or carriage return characters.
# 3. It checks if the file with the extracted filename already exists in the
# current directory. If it exists, it skips the download.
# 4. If the file doesn't exist, it proceeds to download the file using curl
# and checks the exit code to determine if the download was successful
# or not.
# 5. Finally it prompts user if they want to do the sudo dpkg -i install of the download.
#
# Recommended install dance:
# cd .local/bin # or $HOME/bin # or put in /usr/local/bin
# curl -O https://gist.githubusercontent.com/dayne/40c45988e7ecfd9b74b954e711470b9c/raw/discord-deb-update.sh
# chmod +x discord-deb-update.sh
DOWNDIR=/tmp
cd $DOWNDIR
url='https://discord.com/api/download/stable?platform=linux&format=deb'
location=$(curl -sI -L "$url"| grep location)
if [ $? -eq 0 ]; then
echo $location
else
echo "ERROR: unable to get location of latest discord .deb file"
exit 1
fi
filename=$(echo "$location" | awk -F'/' '{print $NF}' | tr -d '\r' )
if [ -f $filename ]; then
echo "File exists: $filename"
echo "Skipping download"
else
echo "Downloading $filename"
curl -JL -o $filename $url
if [ $? -eq 0 ]; then
echo "SUCCESS: Download of $filename complete"
echo "File downloadedto: /tmp/$filename"
else
echo "ERROR: Download of $filename failed."
exist 1
fi
fi
# ensure interactive shell is happening
if [ ! -t 0 ]; then
echo "file downloaded - non interactive shell detected - exiting"
exit
fi
while true; do
read -p "Do you want to run install using dpkg? (Y/N): " answer
case $answer in
[Yy]|[Yy][Ee][Ss])
# Run your command here
echo "Installing $filename"
sudo dpkg -i $DOWNDIR/$filename
if [ $? -eq 0 ]; then
echo "install successful"
exit 0
else
echo "install failed: try yourself with:"
echo "sudo dpkg -i $DOWNDIR/$filename"
exit 1
fi
break
;;
[Nn]|[Nn][Oo])
echo "Exiting..."
exit 0
;;
*)
echo "Invalid input. Please enter Y, N, yes, or no."
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment