How To Iterate Over RPM Files List and Install RPM if not Already Installed

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
PACKAGE="sqlite-devel"

cd "/tmp/${PACKAGE}"

i=0
# lists the RPMS in the directory
for RPM in *
do
    # remove the extension of the RPM
    RPM_NAME_ONLY=$(echo "${RPM}" |
        sed -e 's/\([^.]*\).*/\1/'
        -e 's/\(.*\)-.*/\1/'
    )

    if rpm -qa | grep "${RPM_NAME_ONLY}";
    then
        echo "${RPM_NAME_ONLY} is already installed."
    else
        echo "Installing ${RPM} offline."

        yum localinstall --assumeyes --cacheonly \
            --disablerepo=* "${RPM}"
    fi

    ((i+=1))
done

Explanation of sed REGEX

Reference is at https://www.unix.com/shell-programming-and-scripting/150883-remove-version-numbers-package-lists.html

Using sqlite-devel-3.7.17-8.el7_7.1.x86_64.rpm as an example this is what the first stage sed does:

1
echo "sqlite-devel-3.7.17-8.el7_7.1.x86_64.rpm" | sed -e 's/\([^.]*\).*/\1/'

Gives this output:

1
sqlite-devel-3

In \([^.]*\).*:

  • The [^.]* matches everything that is not a . zero or more times.

  • The \([^.]*\) matches everything that is not a . zero or more times and assigns the result to variable 1.

  • The ([^.]*\). matches everything up to but the first ..

  • The last part .* strips away the characters after the first ..

  • Finally the \1 substitutes the the match we got in variable 1.


The second stage sed:

1
2
3
echo "sqlite-devel-3.7.17-8.el7_7.1.x86_64.rpm" | \
  sed -e 's/\([^.]*\).*/\1/' \
  -e 's/\(.*\)-.*/\1/'

Gives this output:

1
sqlite-devel

In (.*\)-.*:

  • (.*\)- matches everything before the first - and stores the result into variable 1.

  • -.* strips away everything after the first -.

  • Finally the \1 substitutes the the match we got in variable 1.