I have this Python script for formatting and coloring output:
def error(message):
print(f"\033[91m{message}\033[00m")
def warning(message):
print(f"\033[93m{message}\033[00m")
def info(message):
print(f"\033[96m{message}\033[00m")
def success(message):
print(f"\033[92m{message}\033[00m")
And it works great when I write messages directly to the terminal.
However, some of my scripts are used indirectly in other shell scripts, and in that case, my output won't work properly.
For example, I have a Python script to find out the name of the OS. It's called GetOs
and when executed, it would print Ubuntu
in a blue color.
Now when I want to use this Python script in my Shell script as follow:
if [[ $(GetOs) == Ubuntu ]]; then
echo "This feature is not supported on Ubuntu"
fi
it fails. The equality operator won't work because the $(GetOs)
would return a blue result to the stdout.
Is it possible for me to conditionally color messages in my Message.py
file? To know if it's being called from another script or directly from the terminal?