我正在尝试编写一个使用 aspell 来检查拼写检查文件的 bash 脚本
I am trying to write a bash script that uses aspell to check spell check files
我手头有以下脚本,我认为有错误,但我不知道如何修复它们。问题是脚本 return 只是应该 return 拼写错误的单词。我 运行 这个脚本用这个命令
$ sh ./action.sh "{\"b64\":\"`base64 input.txt`\" }"
input.txt有6个字:
猫狗
象狮
驼鹿虫
它 returns
{ "result": "" }
但我想要它 return
{ "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 () to extract the value of the 'b64' attribute,
# then decode it from base64 format, and dump the result to a file.
echo | 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 () to extract the value of the 'b64' attribute,
# then decode it from base64 format, and dump the result to a file.
echo | 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\" }"
我手头有以下脚本,我认为有错误,但我不知道如何修复它们。问题是脚本 return 只是应该 return 拼写错误的单词。我 运行 这个脚本用这个命令
$ sh ./action.sh "{\"b64\":\"`base64 input.txt`\" }"
input.txt有6个字: 猫狗 象狮 驼鹿虫
它 returns
{ "result": "" }
但我想要它 return
{ "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 () to extract the value of the 'b64' attribute,
# then decode it from base64 format, and dump the result to a file.
echo | 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 () to extract the value of the 'b64' attribute,
# then decode it from base64 format, and dump the result to a file.
echo | 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\" }"