ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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?
#!/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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.