Use printf instead of echo for this sort of thing. Something like:
printf "%-30s%s" "left justified text" "[status]"
If your process name is longer than whatever length you chose though, they'll miss-align (i.e. printf
won't truncate).
Domanda
I'm trying to create a bash script which will display the statuses of multiple services. The function I created to display the status is as follows:
printStatus() {
if checkProcess "${1}"; then
echo -e "${1} status: \t\t [RUNNING]"
else
echo -e "${1} status: \t\t [DOWN]"
fi
}
The problem is that $1
has a variable size, which for example, creates the following result:
Mysql status: [RUNNING]
PHP-fpm status: [RUNNING]
How can I manage to get the [RUNNING]
tags perfectly underneath each other?
So I'd like to have it as follows:
Mysql status: [RUNNING]
PHP-fpm status: [RUNNING]
This is my function after Mat's answer:
printStatus() {
if checkProcess "${1}"; then
printf "%-30s%s" "${1} status:" "[RUNNING]"
else
printf "%-30s%s" "${1} status:" "[DOWN]"
fi
echo # <-- I know, being lazy here for the new line...
}
Soluzione
Use printf instead of echo for this sort of thing. Something like:
printf "%-30s%s" "left justified text" "[status]"
If your process name is longer than whatever length you chose though, they'll miss-align (i.e. printf
won't truncate).
Altri suggerimenti
Use printf instead of echo. Example:
printf "%-16s%s" "${1}" "[RUNNING]"
You'll have to adjust the length of the formating to your max expected value.