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

 1PACKAGE="sqlite-devel"
 2
 3cd "/tmp/${PACKAGE}"
 4
 5i=0
 6# lists the RPMS in the directory
 7for RPM in *
 8do
 9    # remove the extension of the RPM
10    RPM_NAME_ONLY=$(echo "${RPM}" |
11        sed -e 's/\([^.]*\).*/\1/'
12        -e 's/\(.*\)-.*/\1/'
13    )
14
15    if rpm -qa | grep "${RPM_NAME_ONLY}";
16    then
17        echo "${RPM_NAME_ONLY} is already installed."
18    else
19        echo "Installing ${RPM} offline."
20
21        yum localinstall --assumeyes --cacheonly \
22            --disablerepo=* "${RPM}"
23    fi
24
25    ((i+=1))
26done

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:

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

Gives this output:

1sqlite-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:

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

Gives this output:

1sqlite-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.