在用户输入上Bash格式化80行

时间:2014-05-07 04:04:30

标签: bash

我想知道是否有可能确定描述或srcofhlp是否超过80行文本,它将换行并转到下一行并以相同格式创建*。我正在研究折叠,但不知道是否可以重新格式化,因此会显示带有*的换行符。 例如:

/*
 * Filename: BOB
 * Author: Whatever
 * Description: Hi lets just pretend this is over 80 character
 *              and let this continue like this
 */

-

printf "Enter the author: " ; read -r author
printf "Enter the UserID: " ; read -r usrid
printf "Enter the description: " ; read -r description 
printf "Enter sources of help: " ; read -r srcofhlp

printf "%-16s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%s\n\n\n" '/*' ' * Filename: ' "$filename" ' * Author: ' \
"$author" ' * UserId: ' "$usrid" ' * Description: ' "${description} " ' * Date: ' \
"$today" ' * Sources of Help: ' "$srcofhlp" ' */' > $filename

2 个答案:

答案 0 :(得分:0)

这是一个可能有所帮助的小功能 - 或者至少指向正确的方向:

#!/bin/bash

author='whatever'
description='Hello lets just pretend this is over 80 character and let this continue like this blah blah blah blah blah blah blah blah fdjsklf fsdf a f dsg a g asg sg g asgd a sdg sggsag   g asdg sa g s gd sag  '

function format_description() {
    local start

    fmt -w 60 <<< "$description" | while read line
    do
        if ! ((start++))
        then
            echo " * Description: $line"
        else
            echo " *              $line"
        fi
    done
}

format_description "$description"

输出:

 * Description: Hello lets just pretend this is over 80 character and let
 *              this continue like this blah blah blah blah blah blah blah
 *              blah fdjsklf fsdf a f dsg a g asg sg g asgd a sdg sggsag
 *              g asdg sa g s gd sag

答案 1 :(得分:0)

这是一种方式 - 它没有理解C风格注释的完整语法(即不是开头/*和结束*/),但它可能有用:

$ echo " * this is a somewhat longish string with a prefix" | fmt -10 -p " * "
 * this
 * is a
 * somewhat
 * longish
 * string
 * with a
 * prefix
$