How to check if a package is installed before installing it with yay AUR helper?

I’m using yay AUR helper to install packages from a list, but it’s not skipping the packages that are already installed. How can I check if a package is already installed before installing it with yay?

Can I check packages from the AUR with pacman like this?

if ! pacman -Qs package_name > /dev/null; then
   yay -S package_name
fi
install_packages() {
  local dir="$1"
  local error_log="${dir}/errorlog.txt"
  local pkg_list="${dir}/pkglist.txt"
  local failed_packages=()

  # Install packages with yay
  echo "Installing packages..."
  while read -r package; do
    if yay -S --noconfirm --norebuild "$package"; then
      echo "Installed $package"
    else
      echo "$package" >> "$error_log"
      failed_packages+=("$package")
    fi
  done < "$pkg_list"

  # Check for failures
  if [[ ${#failed_packages[@]} -eq 0 ]]; then
    echo "All packages installed successfully"
  else
    echo "Failed to install the following packages:"
    printf '%s\n' "${failed_packages[@]}"
  fi

  # Clean up
  rm "$error_log"
}
man pacman
man yay
--needed

PS.
It looks like you are making scripts dealing with pkglists … you may be interested in referencing mapare:

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