上下文:您知道如何在文字处理程序中键入句子,如果在段落中插入新句子,或者使现有句子更长或更短,则不必进行特殊格式化?
我正在编写一些段落来记录给定脚本的内容。我希望能够在用户使用“-help”等选项调用脚本时输出,但不是每次添加句子时都要调整echo或printf语句,或者更长或更短。我怎么能这样做?
我愿意将输出设为80列或固定宽度。
编辑阅读:我发现所有答案都非常有用。
答案 0 :(得分:4)
您可以使用广泛使用的fmt
实用程序。
如果您有Gnu coreutils(如果您使用Linux,几乎肯定会这样),那么您可以使用the Gnu version。许多BSD系统都有very similar utility,包括Mac OS X.
这是一个简单的例子,它可以与这些实现中的任何一个一起使用。请注意,第二段缩进了两个空格,并且保留了缩进:
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"
输出:
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.
您可以将其简单地添加到脚本中(感谢@ninjaj提供有关使用tput获取所需列宽的建议。)
#!/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
答案 1 :(得分:2)
在UNIX / Linux中执行此操作的经典方法是fold
。这是一个例子:
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.
并使其看起来更像是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
$
答案 2 :(得分:1)
如果我理解你的要求,你想在脚本本身中包含描述脚本的文本,一个简单的方法是使用包含脚本中描述的 heredoc ,然后简单地测试用于'-h'
或'--help'
以显示您的说明。通过将 heredoc 包装在一个函数中可以获得相当大的灵活性,该函数可以作为一般usage
函数来响应帮助,显示错误条件并设置任何退出代码给定的情况。
一个简单的例子是:
#!/bin/bash
maxval=127
usage() {
local ecode=${2:-0}
test -n "$1" && printf "\n %s\n" "$1" >&2
cat >&2 << MSG
usage: ${0//*\//} srcdir tgtdir [maxname] (maxname default: $maxval)
This script will copy all files from 'srcdir' to 'tgtdir' with numeric names less
than 'maxname'.
Options:
-h | --help program help (this file)
-a | --all copy all files from srcdir to tgtdir
MSG
exit $ecode;
}
## respond to help
[ "$1" = -h -o "$1" = --help ] && usage
## example of error usage
[ -d "somedir" ] || usage "Error: directory does not exist" 2
MSG
标记之间的所有内容都包含 heredoc 。文本可以像您希望的那样长或精细,并且比一系列echo
或printf
语句更容易维护。虽然它不会像fmt
或flow
那样为您重新传输文本,但维护并提供使用第三方工具的简单替代方法是微不足道的。
示例回应--help
$ bash usage.sh --help
usage: usage.sh srcdir tgtdir [maxname] (maxname default: 127)
This script will copy all files from 'srcdir' to 'tgtdir' with numeric names less
than 'maxname'.
Options:
-h | --help program help (this file)
-a | --all copy all files from srcdir to tgtdir
显示错误条件的示例
$ bash usage.sh
Error: directory does not exist
usage: usage.sh srcdir tgtdir [maxname] (maxname default: 127)
This script will copy all files from 'srcdir' to 'tgtdir' with numeric names less
than 'maxname'.
Options:
-h | --help program help (this file)
-a | --all copy all files from srcdir to tgtdir
$ echo $?
2