67. Working on the Bash Script file (Day 63)

67. Working on the Bash Script file (Day 63)

Today, I spent a bit of time fine-tuning my Bash Script.

A Bash Script is a file that contains a series of commands. In my assignment project, I am using it to run certain commands automatically which otherwise I would have to manually type in the Terminal every time I have to execute my app. This will help another user who plans to execute my app as well.

This is what I did:

I started a new file, "run.sh" in my existing Python project. Here, I have the following commands inserted:

#!/bin/bash

# 1. checks if Python is installed on the system or not
python3 --version

# 2. check if venv (virtual environment) exists or not
if [ -d ".venv" ]; then
    echo ".venv - virtual environment present"
else
    python -m venv .venv
    echo ".venv - virtual environment created"
fi

source .venv/bin/activate
pip3 install colored
python3 main-app.py

#!/bin/bash - The Bash Script file begins with this command first. You can read about what it is and why it is used in this article here: https://medium.com/@codingmaths/bin-bash-what-exactly-is-this-95fc8db817bf

The next command (1) is checking the user's Python version and letting them of the same. If the user doesn't have Python, it will display a message for the user to install one to run this app.

The 2nd command checks if the app has a virtual environment to execute certain functions of the app. If there is no .venv, it will create a new one and if there is one existing, it will activate it.

pip3 install colored is an external package I installed to give my fonts in the Terminal some colors! Otherwise, it's the usual black text on a white background, or vice versa. You can find the package here: https://pypi.org/project/colored/

And finally, python3 main-app.py which is where all the python codes are, will be executed automatically when I run this Bash Script file.

This is how you run the Bash Script file: In the Terminal, action this command - ./run.sh

That's it for today. Planning to put in more effort tomorrow!