Fedora Linux Support Community & Resources Center
  #1  
Old 21st February 2012, 05:05 PM
Adunaic Offline
Registered User
 
Join Date: Mar 2009
Location: Lancaster, UK
Posts: 918
linuxfirefox
bash output in columns

I have a bash script that does some checks and out puts pass / fail I would like the output to appear as two separate columns.

The code is:

Code:
#
installedPackages(){
    echo "python\.i[0-9]86";
    echo "python\.x86_64";
    echo "bash";
    echo "gcc";
    echo "gnome";
    echo "gnome-shell";
}


yum list installed > installed.tmp;

echo "Checking that requested packages are installed..."
for i in $(installedPackages);
do
    checkForTrailingDot=`echo $i | grep '\.'`
 if [[ -z $checkForTrailingDot ]]
    then 
	isInstalled=`grep ^$i'\.' installed.tmp`
    else
	isInstalled=`grep ^$i installed.tmp`
    fi;
#echo $isInstalled
    if [[ -z $isInstalled ]]
    then
	echo -e $i "\t \t \t \t \t \t \t \t" "[\033[31m FAILED \033[0m]";
    else
	echo -e $i "\t \t \t \t \t \t \t \t" "[\033[32m PASSED \033[0m]";
    fi;
done;

and it outputs:

Code:
python\.i[0-9]86 	 	 	 	 	 	 	 	 [ PASSED ]
python\.x86_64 	 	 	 	 	 	 	 	 [ FAILED ]
bash 	 	 	 	 	 	 	 	 [ PASSED ]
gcc 	 	 	 	 	 	 	 	 [ PASSED ]
gnome 	 	 	 	 	 	 	 	 [ FAILED ]
gnome-shell 	 	 	 	 	 	 	 	 [ PASSED ]

which is not in nice pretty columns. any suggestions?
Reply With Quote
  #2  
Old 21st February 2012, 05:29 PM
DBelton Offline
Administrator
 
Join Date: Aug 2009
Posts: 7,320
linuxfirefox
Re: bash output in columns

You can have it put it into a specific column

To put it into column 60:
Code:
echo -e $i "[\033[60G\033[31m[ FAILED \033[0m]"
Reply With Quote
  #3  
Old 21st February 2012, 08:26 PM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,620
linuxfedorafirefox
Re: bash output in columns

Quote:
Originally Posted by DBelton View Post
To put it into column 60:
Code:
echo -e $i "[\033[60G\033[31m[ FAILED \033[0m]"
Cool, I didn't know about the ESC[60G sequence. I used printf to go to column 60:
Code:
if [[ -z $isInstalled ]]; then
   printf "%60s[ $(echo -en "\033[1;31m")FAILED$(echo -en "\033[0m") ]\r$i\n"
else
   printf "%60s[ $(echo -en "\033[1;32m")PASSED$(echo -en "\033[0m") ]\r$i\n"
fi
If you redirect the output to a file then the color escape sequences are in there but the spaces do show up. So that may make it easier to put an option in the script to turn off color if redirecting to a file.

Here's the output on my system:
Attached Thumbnails
Click image for larger version

Name:	columns.gif
Views:	138
Size:	188.4 KB
ID:	22728  
__________________
OS: Fedora 20 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 5GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N

Last edited by RupertPupkin; 21st February 2012 at 10:09 PM. Reason: Added screenshot, because I love screenshots. :)
Reply With Quote
  #4  
Old 5th March 2012, 11:46 PM
php1ic Offline
Registered User
 
Join Date: Oct 2008
Location: London - England
Posts: 137
linuxfirefox
Re: bash output in columns

Quote:
Originally Posted by RupertPupkin View Post
Cool, I didn't know about the ESC[60G sequence.
In that case tput will blow your mind (as long as you don't output to a file). Try resizing the terminal and re-running the script.

Code:
column=`tput cols`
column=$(($column-10))
.
.
.
if [[ -z $isInstalled ]]; then
	printf "%${column}s[ $(echo -en "\033[1;31m")FAILED$(echo -en "\033[0m") ]\r$i\n"
else
	printf "%${column}s[ $(echo -en "\033[1;32m")PASSED$(echo -en "\033[0m") ]\r$i\n"
fi

Last edited by php1ic; 6th March 2012 at 10:05 AM. Reason: typo when redefing column
Reply With Quote
  #5  
Old 21st February 2012, 05:48 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,815
linuxfirefox
Re: bash output in columns

Though if the output is redirected to a disk file for other use it will no longer be in a prittyprint form - it will have embeded escape sequences...

Better try something like:

Code:
name="beginning"
value="[ PASSED ]"
blanks="                                                                    "

echo "${name}${blanks:${#name}} ${value}"
Where the "blanks" has as many blanks (60 in your case) to get to the columne you want the "[ FAILED ]" (or passed...) text to come out.

The slightly arcane "${blanks:${#name}}" is used to expand the "blanks" string minus the length of the value of the "name" parameter. So the ${#name} evaulates to the number of characters in the name string. Using this length for the offset (ie number of characters to skip) in the blanks string should always put the output in a fixed format that doesn't have escape characters, and is suitable for including in other documents.

Another thing - embedding specific escape sequences in output will not always work. It depends entirely on what terminal emulator you are using as to whether it will be correct or not. It is better to get the appropriate escape sequence by using the tput utility to translate what you want.

Last edited by jpollard; 21st February 2012 at 05:53 PM. Reason: another thing.
Reply With Quote
  #6  
Old 21st February 2012, 05:56 PM
DBelton Offline
Administrator
 
Join Date: Aug 2009
Posts: 7,320
linuxfirefox
Re: bash output in columns

True. If you redirected it to a file, then echo would put the escape sequences in the file instead of expanding them out.

However, if you then used cat to display the file, it would interpret the escape sequences and display it properly, but other methods may or may not interpret them.
Reply With Quote
  #7  
Old 21st February 2012, 05:59 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,815
linuxfirefox
Re: bash output in columns

Quote:
Originally Posted by DBelton View Post
True. If you redirected it to a file, then echo would put the escape sequences in the file instead of expanding them out.

However, if you then used cat to display the file, it would interpret the escape sequences and display it properly, but other methods may or may not interpret them.
Only if it goes to the same type of terminal as the one used when the escape sequences are generated.
Reply With Quote
Reply

Tags
bash, columns, output

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use 'IFS' in bash script to traeverse in the output returned by 'ls' command uditkr Programming & Packaging 3 25th September 2011 11:26 AM
Help me file my bug report and troubleshoot logs and bash output xtian Installation, Upgrades and Live Media 0 6th August 2011 03:29 PM
bash help: output and logging leaded Programming & Packaging 4 22nd March 2006 06:08 PM
Bash script to browse directory with a custom output ? raf_iso Using Fedora 8 12th January 2006 05:13 PM
BASH: How to redirect ping output to file? funchords Using Fedora 22 27th December 2005 03:34 AM


Current GMT-time: 21:37 (Friday, 22-08-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat