5
\$\begingroup\$

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 [ "$command" = "" ]
then
    echo "The pip program not exist on this system."
    sudo apt-get install python-pip -y
    sudo apt install python3-pip -y
else
    echo "Pip is intalled."
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 [ "$TAR" = "" ]
then
    echo "Tar is not installed,  installing now."
    sudo apt-get install tar -y
else
    echo "Tar is installed."
fi

# lets install requests 
pip install requests

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

tar xvf config.tar
sudo chmod 604 ucc.py 

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

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

sudo mv ucc.service "/lib/systemd/system/"

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

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

cd /etc/openvpn/

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

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

any help will be greatly appreciated.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ For the apt-get install, why not just run the install command? Nothing bad should happen from attempting to install a package that already exists \$\endgroup\$ Nov 8, 2020 at 5:13
  • \$\begingroup\$ thats because on client side end user is non-technical person \$\endgroup\$
    – Chang Zhao
    Nov 8, 2020 at 9:15

1 Answer 1

1
\$\begingroup\$
  1. Use for loops for expandability and readability.
  2. If you want to test for individual utilities, try using command -v pip instead of which and comparing against that.
  3. Look up and understand the functional difference between single bracket comparisons and double bracket. [ ] vs [[ ]]
  4. `` syntax is very out of date, use $()
  5. 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.
  6. 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 "${PACKAGE} is not installed, installing now."
        sudo apt-get install ${PACKAGE} -y
    else
        echo "${PACKAGE} is installed."
    fi
done

pip3 install -U pip

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

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

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

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 "${ITEM} is installed."
    else
        echo "${ITEM} failed to download"
    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
\$\endgroup\$
2
  • \$\begingroup\$ do you mean if i set the file permission and tar it on extraction those permission attributes will be preserved? \$\endgroup\$
    – Chang Zhao
    Nov 8, 2020 at 23:41
  • \$\begingroup\$ @ChangZhao askubuntu.com/questions/463325/… \$\endgroup\$
    – Arlion
    Nov 9, 2020 at 0:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.