column命令没有按预期处理转义序列?

时间:2015-11-25 21:57:09

标签: bash

我有一个工作脚本检查mimetypes,即text/x-diff; image/svg+xml等。

#!/bin/bash

mimetype="$1"

_Bold="\e[1m"
_unBold="\e[22m"

output="${_Bold}Testing MimeType${_unBold}:\t${mimetype}\n\t---------------"

get_default() {
xdg-mime query default "$1"
}

alias_of=`grep "^$mimetype " /usr/share/mime/aliases | cut -d' ' -f2`
if [ -n "$alias_of" ]; then
    default=`get_default $alias_of`
    output="$output\nWhich is an alias of:\t$alias_of\t-> ${default:-"none"}"
else
    output="$output\nWhich is not an alias."
    alias_of=$mimetype
fi

while : ; do
    subclass_of=`grep "^$alias_of " /usr/share/mime/subclasses | cut -d' ' -f2`
    if [ -n "$subclass_of" ]; then
      default=`get_default $subclass_of`
      output="$output\nWhich is a subclass of:\t$subclass_of\t-> ${default:-"none"}"

      alias_of=$subclass_of
    else
      output="$output\nWhich is not a subclass"
      break
    fi
done

echo -e $output | column -t -s$'\t'

输出不带粗体符合预期:

Testing MimeType:        text/rss
                         -------------
Which is an alias of:    application/rss+xml  -> none
Which is a subclass of:  application/xml      -> none
Which is a subclass of:  text/plain           -> vim.desktop

以粗体更改:

Testing MimeType:                          text/rss
                         -------------
Which is an alias of:    application/rss+xml  -> none
Which is a subclass of:  application/xml      -> none
Which is a subclass of:  text/plain           -> vim.desktop

我已经删除了我示例中的其余格式,但添加了颜色会让情况变得更糟。

我已经将它缩小到column以某种方式包括其间距中的转义序列。同样的事情发生在xterm,gnome-terminal和urxvt。

这是预期的行为吗?我学到了很多东西,但有时我会得到像这样的头像。

0 个答案:

没有答案