我手头有以下脚本,我认为有错误,但我没有修复它们的知识。问题是脚本不返回任何内容,但它应该返回拼写错误的单词。我用这个命令运行这个脚本
$ sh ./action.sh "{"b64":"`base64 input.txt`" }"
输入.txt有 6 个单词: 猫狗 埃莱芬特狮子 麋鹿虫
它返回
{ "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" }"
我认为,问题在于重定向。我把>&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" }"