[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Avoiding setting same variables multiple times
From: |
pauline-galea |
Subject: |
Avoiding setting same variables multiple times |
Date: |
Wed, 7 Apr 2021 19:57:30 +0200 |
I am working on a bash script with functions that can print text
in colour.
As you can see from the code below, to be able to use
"secn_Rd" I have to call "ansi-colr-codes" at the beginning of the
function "section-spec".
Because I have other functions defined in the bash script, I might
have to call a function to initialise variables that may have already
been available. Is there any neat way to set the variables but not end
up setting them multiple times?
function ansi-colr-codes()
{
nc="\e[0m" # reset to normal colours
dBk="\e[2;30m" # black foreground
dRd="\e[2;31m" # red foreground
dGn="\e[2;32m" # green foreground
dYl="\e[2;33m" # yellow foreground
dBl="\e[2;34m" # blue foreground
dMg="\e[2;35m" # magenta foreground
dCy="\e[2;36m" # cyan foreground
dWh="\e[2;37m" # white foreground
}
function section-spec()
{
ansi-colr-codes
secn_Nc="%s\n"
secn_Bk="${bBk}%s${nc}\n"
secn_Rd="${bRd}%s${nc}\n"
secn_Gn="${bGn}%s${nc}\n"
secn_Yl="${bYl}%s${nc}\n"
secn_Bl="${bBl}%s${nc}\n"
secn_Mg="${bMg}%s${nc}\n"
secn_Cy="${bCy}%s${nc}\n"
secn_Wh="${bWh}%s${nc}\n"
}
- Avoiding setting same variables multiple times,
pauline-galea <=