Install package with pacman or AUR helper

I want to know if this is the correct way of trying to install a package with pacman, and if that doesn’t work install the package with the AUR helper yay:

sudo pacman -S --noconfirm --needed $pkg
pacman -Qeq $pkg >/dev/null 2>&1 || yay -S --noconfirm $pkg

It’s for the following script that I’m making:

arch-post-install.sh

#!/usr/bin/env bash

dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
DOTFILES="$(dirname "$dir")"
declare -f command_exists &>/dev/null || source "$DOTFILES/helpers/helpers.sh"

function my_install_pkgs() {
    for pkg in "${pkgs[@]}" ; do
        sudo pacman -S --noconfirm --needed $pkg
        # If not installed with pacman, use AUR helper
        pacman -Qeq $pkg >/dev/null 2>&1 || yay -S --noconfirm $pkg
    done
}

function install_yay() {
    git clone https://aur.archlinux.org/yay.git
    cd yay || exit
    makepkg -si
    cd .. && rm -rf yay
}

function install_essential() {
    sudo pacman -Syyu
    pkgs=( base-devel gcc git wget nextcloud-client )
    for pkg in "${pkgs[@]}" ; do
        sudo pacman -S --noconfirm --needed $pkg
    done

    if ! command_exists yay; then
        install_yay
    fi

    ln -sf "$HOME"/Nextcloud "$HOME"/cloud
    ln -sf "$HOME"/Nextcloud/code "$HOME"/code
    mkdir -p "$HOME"/cloud/config
    
    sudo tee -a /etc/profile /etc/zsh/zshenv &>/dev/null <<EOF
export XDG_CONFIG_HOME="$HOME"/cloud/config
export XDG_CACHE_HOME="$HOME"/.cache
export XDG_DATA_HOME="$HOME"/.local/share
export XDG_STATE_HOME="$HOME"/.local/state
export ZDOTDIR="$XDG_CONFIG_HOME"/zsh
EOF
}

function install_packages() {
    pkgs=$(source get_popular_packages.sh 40)

    # For a description of the packages see the Brewfile
    pkgs+=( firefox vlc grep python python-pip tmux htop jq zsh youtube-dl tmux \
           neovim python-pynvim fzf bat fd exa highlight shellcheck gh cloc ack \
           entr wdiff z audacity zathura nerd-fonts-complete neofetch alacritty \
           librewolf-bin tor-browser xsel nodejs tree ripgrep mutt keepassxc \
           diff-so-fancy )
    for pkg in "${pkgs[@]}" ; do
        sudo pacman -S --noconfirm --needed $pkg
        # If not installed with pacman, use AUR helper
        pacman -Qeq $pkg >/dev/null 2>&1 || yay -S --noconfirm $pkg
    done

    # Reload zsh
    source "$XDG_CONFIG_HOME"/zsh/.zshrc
}

main() {
    install_essential
    install_packages
}

main "$@"

get_popular_packages.sh

#!/usr/bin/env bash

function usage() {
    cat <<EOF
Usage: get_popular_packages.sh popularity
EOF
    exit 1
}

function get_popularity() {
    if [ $# -gt 0 ]; then
        echo $1
    else
        usage
    fi
}

function get_total_packages() {
    curl -sX "GET" -H "accept: application/json" \
        "https://pkgstats.archlinux.de/api/packages?limit=1&offset=0" \
        | jq ".total"
}

function get_packages() {
    curl -sX "GET" -H "accept: application/json" \
        "https://pkgstats.archlinux.de/api/packages?limit=$2&offset=$3" \
         | jq ".packagePopularities[]" \
         | jq "select( .popularity >= $1 )" \
         | jq ".name"
}

function get_popular_packages() {
    local x total limit
    popularity=$(get_popularity "$@")
    total=$(get_total_packages)
    x=0
    limit=2000
    echo "Downloading list of popular packages..."
    while [ $x -le $total ]; do
        get_packages $popularity $limit $x
        x=$(($x+$limit))
    done
}

function install_packages() {
    pkgs=$(get_popular_packages 60)
    
    # For a description of the packages see the Brewfile
    pkgs+=( firefox vlc grep python python-pip tmux htop jq zsh youtube-dl tmux \
           neovim fzf bat fd exa highlight shellcheck gh cloc ack diff-so-fancy \
           entr wdiff z audacity zathura nerd-fonts-complete neofetch alacritty \
           librewolf-bin tor-browser xsel nodejs tree ripgrep mutt keepassxc )
    for pkg in "${pkgs[@]}" ; do
        sudo pacman -S --noconfirm --needed $pkg
        pacman -Qeq $pkg >/dev/null 2>&1 || yay -S --noconfirm $pkg
    done
    pip3 install pynvim
}

get_popular_packages "$@"
#install_packages

AUR helpers like yay already can do what you’re attempting to accomplish. Why not just do:

yay -S --noconfirm --needed $pkg

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.