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
 
LinkBack Search this Thread
Old 08-15-2007, 06:26 PM   #1
ceashton
Member
 
Registered: Nov 2006
Distribution: Slackware 12.2
Posts: 36

Rep: Reputation: 15
Format bash output


Hi,

I'm writing a bash script for which there is a --longhelp option, which outputs several screen full of helpful information ;-). I am wandering how it is best to format these pages?

In the past I have just used:

Code:
echo "some text"
echo "some more text"
echo "yet more text"
or

Code:
echo -e "some text
some more text
yet more text"
Is there an easy way to specify a certain width to contain all the text - or indeed to fill the screen's width - instead of formatting the text as in my example?

Many thanks

Chris
 
Old 08-15-2007, 06:39 PM   #2
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 706

Rep: Reputation: 68
Hi.

Here is one solution:
Code:
#!/bin/sh

# @(#) s1       Demonstrate fmt and here document.

set -o nounset
echo

echo "GNU bash $BASH_VERSION" >&2
fmt --version | head -1

echo

fmt <<'EOF'

        This is part
        of a paragraph that
        can be entered and fixed up
        in short pieces, and will
        be tidied up by fmt.

You can use indents or not as you please, and if your lines are too long, fmt will handle that as well.

See man fmt for details.

EOF

exit 0
Producing:
Code:
% ./s1

GNU bash 2.05b.0(1)-release
fmt (coreutils) 5.2.1


        This is part of a paragraph that can be entered and fixed up in
        short pieces, and will be tidied up by fmt.

You can use indents or not as you please, and if your lines are too long,
fmt will handle that as well.

See man fmt for details.
cheers, makyo
 
Old 08-15-2007, 06:47 PM   #3
ceashton
Member
 
Registered: Nov 2006
Distribution: Slackware 12.2
Posts: 36

Original Poster
Rep: Reputation: 15
Thanks for the quick reply, this is just what I was looking for. I knew there would be a command for it, just didn't know what to look up!

Thanks once again,

Chris
 
Old 08-17-2007, 12:16 PM   #4
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mandriva, Ubuntu, LFS, gNewSense
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by ceashton View Post
Hi,

I'm writing a bash script for which there is a --longhelp option, which outputs several screen full of helpful information ;-). I am wandering how it is best to format these pages?

In the past I have just used:

Code:
echo "some text"
echo "some more text"
echo "yet more text"
or

Code:
echo -e "some text
some more text
yet more text"
Is there an easy way to specify a certain width to contain all the text - or indeed to fill the screen's width - instead of formatting the text as in my example?

You can use fold (fmt is not standard), or, to speed things up if you will be calling it several times, this shell function:

Code:
wordwrap() {
   case $1 in
       --) shift ;;
       -[0-9]*) ww=${1#-}; shift ;;
       *) ww=$(( ${COLUMNS:-80} - 8 )) ;;
   esac
   str=
   sub=0
   len=0
   ww=$(( $ww - ${WW_INDENT:=0} ))
   for word
   do
     len=$(( ${#str} + ${#word} ))
     if [ $len -le $ww ]
     then
       str=${str:+$str }$word
     else
       printf "%${WW_INDENT:-0}s%s\n" "" "$str"
       len=${#str}
       str=$word
     fi
   done
   printf "%${WW_INDENT:-0}s%s\n" "" "$str"
}
The default width is 8 characters less than your terminal window (defaults to 72 if COLUMNS is not set). This can be changed by specifying a width, -NN, as the first parameter.

If the first word to be printed could be interpreted as a width setting, use -- as the first argument.

Text can be indented by setting WW_INDENT.

 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
last command output format bujecas Linux - General 1 10-12-2006 09:54 AM
i need rpm output in table format Uday123 Fedora 1 12-17-2005 05:15 PM
bash format "cat" output bendeco13 Linux - General 3 10-29-2004 06:39 AM
tool to format report output from mysql? alar Linux - General 0 08-03-2004 08:16 PM
C++ format float number output keefaz Programming 3 06-06-2004 02:16 PM


All times are GMT -5. The time now is 04:32 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration