2 min read

bash : Debian Linux Init - Install

#!/bin/bash
## Software installation script (batch mode)

set -u  # safer: error on undefined variables, but don't exit on command errors

# Function to check if software is installed
is_installed() {
    if command -v "$1" &> /dev/null; then
        return 0
    elif dpkg -l | grep -q "^ii.*$1"; then
        return 0
    else
        return 1
    fi
}

# Function to log non-fatal errors
log_error() {
    echo "⚠️  [ERROR] $1"
}

# Function to install VSCode
install_vscode() {
    echo "➡️ Installing Visual Studio Code..."
    URL="https://code.visualstudio.com/sha/download?build=stable&os=linux-deb-x64"
    TMP_DEB="/tmp/vscode_latest_amd64.deb"
    
    echo "Downloading Visual Studio Code..."
    if ! wget -O "$TMP_DEB" "$URL"; then
        log_error "Failed to download VSCode."
        return 1
    fi
    
    echo "Installing/Updating Visual Studio Code ..."
    if ! sudo dpkg -i "$TMP_DEB"; then
        echo "Trying to fix dependencies..."
        sudo apt install -f -y || log_error "Failed to fix VSCode dependencies."
    fi
    
    echo "✅ Visual Studio Code is installed/updated."
}

# Function to install espanso
install_espanso() {
    echo "➡️ Installing espanso..."
    mkdir -p ~/Downloads
    cd ~/Downloads || { log_error "Cannot access ~/Downloads"; return 1; }
    
    if ! wget https://github.com/espanso/espanso/releases/latest/download/espanso-debian-x11-amd64.deb; then
        log_error "Failed to download espanso."
        return 1
    fi
    
    if ! sudo dpkg -i espanso-debian-x11-amd64.deb; then
        echo "Trying to fix dependencies..."
        sudo apt -f install -y || log_error "Failed to fix espanso dependencies."
    fi
    
    echo "✅ espanso is installed."
}

# Generic function to install via apt
apt_install() {
    local app_name="$1"
    echo "➡️ Installing $app_name..."
    if ! sudo apt update; then
        log_error "apt update failed for $app_name"
    fi
    if ! sudo apt install -y "$app_name"; then
        log_error "Installation failed for $app_name"
        return 1
    fi
    
    echo "✅ $app_name is installed."
}

# List of software to install
declare -A software=(
    ["VSCode"]="code"
    ["espanso"]="espanso"
    ["flameshot"]="flameshot"
    ["CopyQ"]="copyq"
)

# Step 1: Ask user for confirmations
to_install=()

for app_name in "${!software[@]}"; do
    command_name="${software[$app_name]}"
    
    echo ""
    echo "═══════════════════════════════════════"
    
    if is_installed "$command_name"; then
        echo "ℹ️  $app_name is already installed."
        read -p "Do you want to reinstall it? (y/N): " response
    else
        echo "ℹ️  $app_name is not installed."
        read -p "Do you want to install $app_name? (y/N): " response
    fi
    
    case "$response" in
        [oOyY]*)
            to_install+=("$app_name")
            ;;
        *)
            echo "⏭️  $app_name skipped."
            ;;
    esac
done

# Step 2: Perform installations in one go
echo ""
echo "═══════════════════════════════════════"
echo "Starting batch installation for: ${to_install[*]}"

for app_name in "${to_install[@]}"; do
    case "$app_name" in
        "VSCode")
            install_vscode || log_error "VSCode installation encountered issues."
            ;;
        "espanso")
            install_espanso || log_error "espanso installation encountered issues."
            ;;
        "flameshot")
            apt_install "flameshot" || log_error "flameshot installation encountered issues."
            ;;
        "CopyQ")
            apt_install "copyq" || log_error "CopyQ installation encountered issues."
            ;;
    esac
done

echo ""
echo "═══════════════════════════════════════"
echo "✅ Batch script completed (with possible warnings)."

For cloudflare-warp, follow this link to add the cloudflare repository and download cloudflare-warp from their official repository with sudo apt install cloudflare-warp.

See this article if you are getting an architecture error like Error : The configured file ‘main/binary-i386/Packages’ will not be taken into account because the repository does not support the ‘i386’ architecture.