LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-17-2012, 06:27 PM   #1
dannin
LQ Newbie
 
Registered: May 2011
Posts: 11

Rep: Reputation: 0
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!
 
Old 03-18-2012, 02:24 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
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)
 
1 members found this post helpful.
Old 03-19-2012, 05:24 PM   #3
dannin
LQ Newbie
 
Registered: May 2011
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by NevemTeve View Post
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?
 
Old 03-19-2012, 06:10 PM   #4
Karl Godt
Member
 
Registered: Mar 2010
Location: Kiel , Germany
Distribution: once:SuSE6.2,Debian3.1, aurox9.2+3,Mandrake?,DSL? then:W7st,WVHB, #!8.10.02,PUPPY4.3.1 now:Macpup
Posts: 314

Rep: Reputation: 45
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
 
Old 03-20-2012, 05:41 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
> 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

Last edited by NevemTeve; 03-20-2012 at 06:21 AM. Reason: More examples
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem running an Expect script within a Bash script mbeipi Programming 9 02-10-2018 05:00 AM
Bash script problem with ftp session exiting the script early edomingox Programming 5 02-23-2010 05:39 AM
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 10:53 AM
problem changing X terminal colors with script shadowsnipes Slackware 5 10-10-2005 11:15 PM
Bash colors ryedunn Mandriva 0 12-09-2004 08:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration