我正在尝试编写一个使用aspell检查拼写检查文件的bash脚本

时间:2017-07-17 09:21:58

标签: json bash pipe spell-checking aspell

我手边有以下脚本,我认为有错误,但我没有修复它们的知识。问题是脚本什么都不返回,但它应该返回拼写错误的单词。我用这个命令运行这个脚本

$ sh ./action.sh "{\"b64\":\"`base64 input.txt`\" }"

input.txt有6个字: 猫狗 elephent狮子 mooose bug

然后返回

{ "result": "" }

但我希望它返回

{ "result": "elephent mooose " }

#!/bin/bash
#
# This script expects one argument, a String representation of a JSON
# object with a single attribute called b64.   The script decodes
# the b64 value to a file, and then pipes it to aspell to check spelling.
# Finally it returns the result in a JSON object with an attribute called
# result
#
FILE=/home/user/output.txt

# Parse the JSON input ($1) to extract the value of the 'b64' attribute,
# then decode it from base64 format, and dump the result to a file.
echo $1 | sed -e 's/b64.://g' \
        | tr -d '"' | tr -d ' ' | tr -d '}' | tr -d '{' \
        | base64 --decode >&2 $FILE

# Pipe the input file to aspell, and then format the result on one line with
# spaces as delimiters
RESULT=`cat $FILE | aspell list | tr '\n' ' ' `

# Return a JSON object with a single attribute 'result'
echo "{ \"result\": \"$RESULT\" }"

1 个答案:

答案 0 :(得分:0)

我认为,问题在于重定向。我将>&2 $FILE更改为> $FILE 2>&1 然后它工作。 (我不知道为什么第一个不好)

这是输出:

yavuz@ubuntu:/tmp$ sh ./action.sh "{\"b64\":\"`base64 input.txt`\" }"
{ "result": "elephent mooose " }

代码:

#!/bin/bash
#
# This script expects one argument, a String representation of a JSON
# object with a single attribute called b64.   The script decodes
# the b64 value to a file, and then pipes it to aspell to check spelling.
# Finally it returns the result in a JSON object with an attribute called
# result
#
FILE=/tmp/output.txt

# Parse the JSON input ($1) to extract the value of the 'b64' attribute,
# then decode it from base64 format, and dump the result to a file.
echo $1 | sed -e 's/b64.://g' \
        | tr -d '"' | tr -d ' ' | tr -d '}' | tr -d '{' \
        | base64 --decode > $FILE 2>&1

# Pipe the input file to aspell, and then format the result on one line with
# spaces as delimiters
RESULT=`cat $FILE | aspell list | tr '\n' ' ' `

# Return a JSON object with a single attribute 'result'
echo "{ \"result\": \"$RESULT\" }"