LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem with using colors in bash script (https://www.linuxquestions.org/questions/programming-9/problem-with-using-colors-in-bash-script-935007/)

dannin 03-17-2012 06:27 PM

Problem with using colors in bash script
 
I'm trying to make a script that prints a list of services and drives etc and their status.

It outputs various services status in color. Fx. "up" colored green if running and "down" colored red if not running. That works perfectly when using echo - but when I'm using printf, it just shows everything in green - even if it's down. I checked the scripts colorcodes but can't find the error. See attached file.

The relevant code:
Code:

chkapache2=`ps aux | grep apache2 | grep -v "grep apache2" | wc -l`

apache2=`if [ $chkapache2 == "0" ]; then
echo -e "down"
else echo -e "up"
fi`

PR_AP=`if [ chkapache2 == "0" ]; then
echo "s\E[31m%"
else echo "s\E[32m%"
fi`

chksamba=`ps aux | grep smb | grep -v "grep smb" | wc -l`

smb=`if [ $chksamba == "0" ]; then
echo -e "down"
else echo -e "up"
fi`

PR_SA=`if [ chksamba == "0" ]; then
echo "s\E[31m%"
else echo "s\E[32m%"
fi`

PRINT_FORMA2="\E[01m%-15s%"$COLS_PER_COL"s%"$COLS_PER_COL"s\E[00m\n"
PRINT_APACHE="%-15s%"$COLS_PER_COL$PR_AP$COLS_PER_COL"s\E[00m\n"
PRINT_SAMBAD="%-15s%"$COLS_PER_COL$PR_SA$COLS_PER_COL"s\E[00m\n"
PRINT_WINSDE="%-15s%"$COLS_PER_COL$PR_WI$COLS_PER_COL"s\E[00m\n"

printf $PRINT_FORMA2 "Service:" "Version:" "Status:"
printf $PRINT_APACHE "Apache" "$apachever" "$apache2"
printf $PRINT_SAMBAD "Samba" "$smbver" "$smb"
printf $PRINT_WINSDE "Wins/nmbd" "$nmbdver" "$nmbd"

Any help would be greatly apprecieated!

NevemTeve 03-18-2012 02:24 AM

The first usage of variables chkapace, chksamba is prefixed with $, the second isn't (check your if-statements).
And in test, it is not ==, use either = (strings) or -eq (numbers)

dannin 03-19-2012 05:24 PM

Quote:

Originally Posted by NevemTeve (Post 4629597)
The first usage of variables chkapace, chksamba is prefixed with $, the second isn't (check your if-statements).
And in test, it is not ==, use either = (strings) or -eq (numbers)

Thanks! It seems to be working now. I corrected all the if statements to use = instead of == and corrected some variables as you pointed out might be the problem.


I have another question about alignment I hope you can answer as well :-)

Right now the script left-aligns the first column and right-aligns the second and third. I would like it to left-align the second/mid column - but how to do that?

Karl Godt 03-19-2012 06:10 PM

Quote:

Right now the script left-aligns the first column and right-aligns the second and third. I would like it to left-align the second/mid column - but how to do that?
Have not too much experiences with echo -e and even fewer with printf . but it seems simply missing something like

COLS_PER_COL=15

The variable would be set to "" or "0" if not declared .

test should work with '==' because it would also work with '!=' .

'==' and '=' spits fewer error messages than '-eq','-lt','-ge',... because they can match a char or string also .

it is also advisable to double quote the first value in a test comparison for a number in case the string is empty .

Code:

a=
if [ $a = "0" ];then echo 'a is 0';fi
bash: [: =: unary operator expected

if [ "$a" = "0" ];then echo 'a is 0';fi

a=a
if [ $a -gt 0 ];then echo 'a is greater than 0';fi
bash: [: a: integer expression expected


NevemTeve 03-20-2012 05:41 AM

> Right now the script left-aligns the first column and right-aligns the second and third. I would like it to left-align the second/mid column - but how to do that?

An example might help:

Code:

$ printf "%-15s %-15s %-15s\n" "Left_1" "Left_2" "Left_3"
$ printf "%15s %15s %15s\n" "Right_1" "Right_2" "Right_3"

Left_1          Left_2          Left_3
        Right_1        Right_2        Right_3

or:
Code:

WIDTH=15
$ printf "%-*s %-*s %-*s\n" $WIDTH Left_1 $WIDTH Left_2 $WIDTH Left_3
$ printf "%*s %*s %*s\n" $WIDTH Right_1 $WIDTH Right_2 $WIDTH Right_3



All times are GMT -5. The time now is 11:13 PM.