Solved Batch installation with pkg fails when a single package is missing

Hello, I have a simple shell script that installs some packages (see the code below). However, sometimes packages disappear from the FreeBSD repository, this has happened very rarely (only once in my case, with vscode). Still, when it does happen, the entire command fails.

Code:
echo "Install packages"
doas pkg install -y aisleriot audacity calibre chromium darktable emacs evince-lite \
                    filezilla flacon foliate gimp gnome-mahjongg inkscape kid3-qt6 libreoffice \
                    lollypop obs-studio sox thunderbird tigervnc-viewer transmission-gtk vlc \
                    xournalpp en-hunspell ro-hunspell hu-hunspell nerd-fonts

Is there a way to make the installation skip packages that are not available? If not, is there a command that checks if a package exists in the repository and returns something I can use in a shell function to handle this verification?
 
Code:
for pkg in aisleriot audacity calibre chromium darktable emacs evince-lite \<br>filezilla flacon foliate gimp gnome-mahjongg inkscape kid3-qt6 libreoffice \<br>lollypop obs-studio sox thunderbird tigervnc-viewer transmission-gtk vlc \<br>xournalpp en-hunspell ro-hunspell hu-hunspell nerd-fonts ; do
    pkg install...
done
 
Just the loop alone didn't seem to fix the issue. I had to create a check function. I'm not sure if "pkg search" is the best way to handle it, but it seems to work now.

sh:
packages="aisleriot ddsjd"

check_package() {
    pkg search -Q name "$1" > /dev/null 2>&1
}

echo "Installing packages..."

for pkg in $packages; do
    echo "Checking for package: $pkg"
    if check_package "$pkg"; then
        echo "Installing $pkg..."
        doas pkg install -y "$pkg" || echo "Failed to install $pkg"
    else
        echo "Package $pkg not found in the repository, skipping."
    fi
done

echo "Installation complete."
 
You're right. I overlooked that. Thank you so much!

sh:
#!/bin/sh

packages="aisleriot ddsjd"

echo "Installing packages..."

for pkg in $packages; do
    doas pkg install -y "$pkg"
done

echo "Installation complete."
 
Back
Top