Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple bash/script formatting tool, for paragraph text inside the script?

Context: You know how you type sentences in a word processor, and you don't have to do special formatting if you insert a new sentence in a paragraph, or make an existing sentence longer or shorter?

I'm writing some paragraphs to document what a given script does, inside it. I want to be able to output that when the user invokes the script with a "-help", etc option, but not constantly have to adjust echo or printf statements everytime I add a sentence, or make one longer or shorter. How can I do that?

I'm willing to make the output be 80 columns or some fixed width.

EDIT TO READER: All answers are very informative I found.

like image 261
Ray Avatar asked Aug 10 '26 11:08

Ray


2 Answers

You could use the widely-available fmt utility.

If you have Gnu coreutils (which you almost certainly will if you are using Linux), then it you can use the Gnu version. There is a very similar utility available on many BSD systems, including Mac OS X.

Here's a simple example, which should work with either of those implementations. Note that the second paragraph is indented two spaces, and that indentation is preserved:

description="\
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ornare leo non dolor porttitor
euismod. Cras
commodo, nisi vel gravida volutpat, enim turpis tempor eros, ut venenatis elit leo ut nunc. Nulla fermentum
ligula id tincidunt porttitor.

  Morbi ut massa vitae tortor rutrum
  gravida ut id nunc. Integer imperdiet pharetra augue, quis finibus justo
  luctus id. Phasellus a diam ac risus consequat pharetra. Cras
  lacinia neque
  sed ipsum euismod, non commodo felis facilisis.

Suspendisse luctus purus justo, sed iaculis lectus consequat nec. Etiam pretium ultricies
ligula, a pretium sapien facilisis eu. Nulla rhoncus viverra turpis a rutrum.
Cras eu porttitor urna. Duis nec metus vel nisi accumsan scelerisque. Cras lectus erat, mattis non mauris in, consectetur vulputate ipsum.
"
fmt -w 60 <<<"$description"

Output:

Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Aenean ornare leo non dolor porttitor euismod. Cras
commodo, nisi vel gravida volutpat, enim turpis tempor
eros, ut venenatis elit leo ut nunc. Nulla fermentum ligula
id tincidunt porttitor.

  Morbi ut massa vitae tortor rutrum gravida ut id
  nunc. Integer imperdiet pharetra augue, quis finibus
  justo luctus id. Phasellus a diam ac risus consequat
  pharetra. Cras lacinia neque sed ipsum euismod, non
  commodo felis facilisis.

Suspendisse luctus purus justo, sed iaculis lectus
consequat nec. Etiam pretium ultricies ligula, a pretium
sapien facilisis eu. Nulla rhoncus viverra turpis a rutrum.
Cras eu porttitor urna. Duis nec metus vel nisi accumsan
scelerisque. Cras lectus erat, mattis non mauris in,
consectetur vulputate ipsum.

Incorporating the above in a script.

You can trivially add this into a script (thanks to @ninjaj for the suggestion about using tput to get the desired column width.)

#!/bin/bash
description=... # as above, not repeated for space

# Check to see if the first argument is a cry for help
if [[ $1 == -help ]]; then
  fmt -w $(tput cols) <<< "$description"
  exit 0
fi
like image 97
rici Avatar answered Aug 13 '26 04:08

rici


The classic way to do this in UNIX/Linux is fold. Here is an example:

     chicks ~ $ cat /etc/printcap  | fold -w 20
# This file was auto
matically generated 
by cupsd(8) from the
# /etc/cups/printers
.conf file.  All cha
nges to this file
# will be lost.
     chicks ~ $ cat /etc/printcap  | fold -w 40
# This file was automatically generated 
by cupsd(8) from the
# /etc/cups/printers.conf file.  All cha
nges to this file
# will be lost.
     chicks ~ $ cat /etc/printcap 
# This file was automatically generated by cupsd(8) from the
# /etc/cups/printers.conf file.  All changes to this file
# will be lost.

and to make it look more like it would in a script for Ray:

$ echo "Ray wants to see how this works inside of a script" | fold -w 20
Ray wants to see how
 this works inside o
f a script
$ cat foo 
echo "Ray wants to see how this works inside of a script" | fold -w 20
$ bash foo
Ray wants to see how
 this works inside o
f a script
$
like image 32
chicks Avatar answered Aug 13 '26 02:08

chicks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!