首页 > 解决方案 > 无法在脚本中打印 shell 命令的结果

问题描述

我发现这篇关于如何从 DOCX 文件中提取文本的有用帖子,我想把它变成一个小 shell 脚本。我的尝试如下

#!/bin/sh

if [[ $# -eq 0 ]]; then
    echo "pass in a docx file to get the text within"
    exit 1
fi

text="$(unzip -p $1 word/document.xml | sed -e 's/<\/w:p>/\n/g; s/<[^>]\{1,\}>//g; s/[^[:print:]\n]\{1,\}//g')"
echo $text

但是,这不会按预期打印结果。

有什么建议么?

标签: shell

解决方案


感谢shellcheck.net,我发现我需要在$1. 经 shellcheck 批准的最终脚本是:

#!/bin/sh

if [ $# -eq 0 ]; then
    echo "pass in a docx file to get the text within"
    exit 1
fi

text=$(unzip -p "$1" word/document.xml | sed -e 's/<\/w:p>/\n/g; s/<[^>]\{1,\}>//g; s/[^[:print:]\n]\{1,\}//g')
echo "$text"

推荐阅读