mercredi 29 septembre 2021

In bash, download and install a package but only if not installed

I'm writing a script that can run on CentOS or Ubuntu, but my logic might not be optimal. This part is just doing an install for CentOS

type apt &> /dev/null && MANAGER=dnf && DISTRO="Debian/Ubuntu"
type yum &> /dev/null && MANAGER=yum && DISTRO="RHEL/Fedora/CentOS"
type dnf &> /dev/null && MANAGER=dnf && DISTRO="RHEL/Fedora/CentOS"   # $MANAGER=dnf will be default if both dnf and yum are present

[[ "$MANAGER" = "dnf" ]] && (NOT) [ type pydf &> /dev/null ] && wget -P /tmp/ https://download-ib01.fedoraproject.org/pub/fedora/linux/development/rawhide/Everything/x86_64/os/Packages/p/pydf-12-11.fc35.noarch.rpm
[[ "$MANAGER" = "dnf" ]] && [[ -f /tmp/pydf-12-11.fc35.noarch.rpm ]] &> /dev/null && rpm -i /tmp/pydf-12-11.fc35.noarch.rpm && rm /tmp/pydf-12-11.fc35.noarch.rpm

I want to check [ is dnf is present? AND is dfc NOT present? ]. If these are true THEN download the rpm, then [ is dnf present? AND is the rpm present ]. If these are true, then install the rpm.

In each case, I'm a little confused if I am using the right approach, and I never really know if I should be using [ ] or [[ ]]. I get that && is 'only if previous statement is true, then run the next one', but I feel a bit confused that I'm maybe not approaching this is an correct / optimal way (and definitely, my logic is broken in the download line I think)?

Should it maybe look more like this, though I don't know how to construct the first condition:

if [[ $MANAGER == "dnf" && ( NOT type pydf &> /dev/null ) ]]; then 
    wget -P /tmp/ https://download-ib01.fedoraproject.org/pub/fedora/linux/development/rawhide/Everything/x86_64/os/Packages/p/pydf-12-11.fc35.noarch.rpm
    RPM=/tmp/pydf-12-11.fc35.noarch.rpm; type $RPM &> /dev/null && rpm -i $RPM && rm $RPM
fi 

Aucun commentaire:

Enregistrer un commentaire