How to put apt and python packages in a list and check if installed or not then install I am not an expert in shell scripting,
I need help optimizing the shell script I wrote, This shell script I wrote is run by users on the client side who connect to our vpn server from the setting passed in the config.tar.
I check if pip or or tar or other packaches are installed if not isntalled than install it, I am doing it in separate if condition, what I want to achieve is I put the packages to check in a  listand then iterate if not installed than install it for all apt, pip or pip3 packages.
#!/usr/bin/env bash                                                                
set -

command=`which pip`

if [ &quot;$command&quot; = &quot;&quot; ]
then
    echo &quot;The pip program not exist on this system.&quot;
    sudo apt-get install python-pip -y
    sudo apt install python3-pip -y
else
    echo &quot;Pip is intalled.&quot;
fi
sudo apt install speedtest-cli
pip install -U pip
pip3 install mysql-connector-python
pip3 install pymysql
pip3 install sqlalchemy

TAR=`which tar`

if [ &quot;$TAR&quot; = &quot;&quot; ]
then
    echo &quot;Tar is not installed,  installing now.&quot;
    sudo apt-get install tar -y
else
    echo &quot;Tar is installed.&quot;
fi

# lets install requests 
pip install requests

cd $HOME
echo &quot;Iniating download...&quot;
curl -sL &quot;http://my.domain.com/config&quot; -o &quot;config.tar&quot;

tar xvf config.tar
sudo chmod 604 ucc.py 

if [ -e ucc.py ]
then
    echo &quot;ucc.py installed.&quot;
else
    echo &quot;ucc.py failed to download.&quot;
fi

if [ -e ucc.service ]
then
    echo &quot;ucc.service installed.&quot;
else
    echo &quot;ucc.service failed to download.&quot;
fi

sudo mv ucc.service &quot;/lib/systemd/system/&quot;

if [ -e client.conf ]
then
    sudo mv client.conf /etc/openvpn/
    echo &quot;client.conf downloaded.&quot;
else
    echo &quot;client.conf failed to download.&quot;
fi

sudo mv -f client.conf /etc/openvpn/

cd /etc/openvpn/

if [ -e client.conf ]
then
    echo &quot;client.conf installed.&quot;
else
    echo &quot;client.conf failed to download.&quot;
fi

sudo systemctl restart openvpn@client
sudo systemctl status  openvpn@client

any help will be greatly appreciated.
Use for loops for expandability and readability.
If you want to test for individual utilities, try using command -v pip instead of which and comparing against that.
Look up and understand the functional difference between single bracket comparisons and double bracket. [ ] vs [[ ]]
`` syntax is very out of date, use $()
If you have to call /usr/bin/env for the location of bash, but broadly assume apt is the package manager on the system, it's much safer to use /bin/bash.
Python requests is also stored as an apt package, I moved it into the first for loop statement.

#!/bin/bash

for PACKAGE in python-pip python-pip3 speedtest-cli tar python-requests; do
    if [[ ! $(dpkg -l ${PACKAGE}) ]] ; then
        echo &quot;${PACKAGE} is not installed, installing now.&quot;
        sudo apt-get install ${PACKAGE} -y
    else
        echo &quot;${PACKAGE} is installed.&quot;
    fi
done

pip3 install -U pip

for PACKAGE in mysql-connector-python pymysql sqlalchemy ; do
    pip3 install -U ${PACKAGE}

    if [[ $? -ne 0 ]] ; then echo &quot;${PACKAGE} failed to installed&quot; ; fi
done

cd $HOME
echo &quot;Iniating download...&quot;
curl -sL &quot;http://my.domain.com/config&quot; -o &quot;config.tar&quot;

tar xvf config.tar

sudo chmod 604 ucc.py
## I don't know why you need to use sudo to chmod an item inside your own home dir, but ill leave this here.
## Also, you can tar a package with certian perms and then unpack it to obey those specific permissions.

for ITEM in ucc.py ucc.service client.conf; do
    if [[ -e ${ITEM} ]] ; then
         echo &quot;${ITEM} is installed.&quot;
    else
        echo &quot;${ITEM} failed to download&quot;
    fi
done

sudo mv ucc.service /lib/systemd/system/
sudo mv client.conf /etc/openvpn/
sudo mv -f client.conf /etc/openvpn/

sudo systemctl restart openvpn@client
sudo systemctl status  openvpn@client