Created
October 8, 2024 12:08
-
-
Save EleotleCram/255308e62eec93a01fd123aefa85afaf to your computer and use it in GitHub Desktop.
Install a google-chrome .deb to a folder (will create google-chrome-VERSION in it) so that you can have multiple versions on your system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Print Help Message | |
function print_help() { | |
echo "Usage: $0 [OPTIONS] DEB_FILE DIR" | |
echo "" | |
echo "Install a Google Chrome .deb package to the specified directory." | |
echo "" | |
echo "Options:" | |
echo " -h, --help Show this help message and exit." | |
echo "" | |
echo "Arguments:" | |
echo " DEB_FILE Path to the Google Chrome .deb package." | |
echo " DIR Parent directory to install Google Chrome." | |
exit 0 | |
} | |
# Function to validate if the .deb file is a valid Google Chrome package | |
function validate_deb() { | |
local deb_file=$1 | |
if [[ ! -f "$deb_file" ]]; then | |
echo "Error: The specified file '$deb_file' does not exist." | |
exit 1 | |
fi | |
# Check if it is a .deb file and contains 'google-chrome' in its name | |
if [[ ! "$deb_file" =~ \.deb$ ]] || [[ ! "$deb_file" =~ google-chrome ]]; then | |
echo "Error: The file '$deb_file' is not a valid Google Chrome .deb package." | |
exit 1 | |
fi | |
} | |
# Function to extract the Chrome version from the .deb package | |
function get_chrome_version() { | |
local deb_file=$1 | |
dpkg-deb -I "$deb_file" | grep 'Version:' | awk '{print $2}' | |
} | |
# Function to install Google Chrome .deb to the target parent directory | |
function install_chrome() { | |
local deb_file=$1 | |
local parent_dir=$2 | |
# Get the Chrome version | |
local version | |
version=$(get_chrome_version "$deb_file") | |
if [[ -z "$version" ]]; then | |
echo "Error: Could not determine the version of Google Chrome." | |
exit 1 | |
fi | |
# Define the target directory as google-chrome-VERSION inside the parent directory | |
local target_dir="$parent_dir/google-chrome-$version" | |
# Check if the target directory already exists | |
if [[ -d "$target_dir" ]]; then | |
echo "Warning: Directory '$target_dir' already exists. Proceeding..." | |
else | |
echo "Creating directory '$target_dir'..." | |
mkdir -p "$target_dir" | |
fi | |
# Extract the package into the new subdirectory | |
echo "Extracting the package to '$target_dir'..." | |
dpkg-deb -x "$deb_file" "$target_dir" | |
# Check if the extraction was successful | |
if [[ -d "$target_dir/opt/google/chrome" ]]; then | |
echo "Google Chrome successfully installed to '$target_dir'." | |
else | |
echo "Error: Failed to extract Google Chrome files. Please verify the .deb package." | |
exit 1 | |
fi | |
# Create bin directory and symlink the google-chrome executable | |
local bin_dir="$target_dir/bin" | |
mkdir -p "$bin_dir" | |
# Create symlink for easy access | |
local chrome_exec="$target_dir/opt/google/chrome/google-chrome" | |
if [[ -f "$chrome_exec" ]]; then | |
ln -s "$chrome_exec" "$bin_dir/google-chrome" | |
echo "Created symlink: '$bin_dir/google-chrome' -> '$chrome_exec'" | |
else | |
echo "Error: Google Chrome executable not found at '$chrome_exec'." | |
exit 1 | |
fi | |
} | |
# Check for help flag | |
if [[ "$1" == "-h" || "$1" == "--help" ]]; then | |
print_help | |
fi | |
# Validate the number of arguments | |
if [[ $# -lt 2 ]]; then | |
echo "Error: Missing arguments." | |
echo "Use -h or --help for usage information." | |
exit 1 | |
fi | |
# Assign arguments | |
DEB_FILE=$1 | |
PARENT_DIR=$2 | |
# Validate the .deb package | |
validate_deb "$DEB_FILE" | |
# Install Google Chrome to the target parent directory | |
install_chrome "$DEB_FILE" "$PARENT_DIR" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment