首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Bash格式化80行用户输入

Bash格式化80行用户输入
EN

Stack Overflow用户
提问于 2014-05-07 12:04:30
回答 2查看 92关注 0票数 1

我想知道是否有可能确保description或srcofhlp超过80行文本,它会换行并转到下一行,并创建相同格式的*。我正在查看fold,但不知道是否可以重新格式化,以便显示带有*的换行符。所以举个例子:

代码语言:javascript
复制
/*
 * Filename: BOB
 * Author: Whatever
 * Description: Hi lets just pretend this is over 80 character
 *              and let this continue like this
 */

-

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2014-05-09 22:47:17

这里有一个小函数,它可能会有所帮助--或者至少为你指明正确的方向:

代码语言:javascript
复制
#!/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"

这将输出以下内容:

代码语言:javascript
复制
 * 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
票数 0
EN

Stack Overflow用户

发布于 2014-05-10 00:45:50

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

代码语言:javascript
复制
$ echo " * this is a somewhat longish string with a prefix" | fmt -10 -p " * "
 * this
 * is a
 * somewhat
 * longish
 * string
 * with a
 * prefix
$
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23508522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档