我想写一个bash脚本,使用aspell来检查拼写检查文件
问题描述:
我手边有以下脚本,我认为有错误但我没有修复它们的知识。问题是脚本没有返回任何内容,但它应该返回拼写错误的单词。我运行此脚本使用此命令我想写一个bash脚本,使用aspell来检查拼写检查文件
$ sh ./action.sh "{\"b64\":\"`base64 input.txt`\" }"
input.txt中有6个字: 猫狗 elephent狮 mooose错误
,并返回
{ "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\" }"
答
I t这个问题是关于重定向的。我改变了>&2 $FILE
到> $FILE 2>&1
然后它工作。 (我不知道为什么第一个是不正常)
这里是输出:
[email protected]:/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\" }"
+0
是的谢谢你的答案,我认为这是阅读空输出文件,我想要的教程和一切都是错误的 –
运行使用bash -x脚本。这将帮助您更好地跟踪对变量RESULT的更改。 –
当我与SH尝试-x它输出FILE = /家庭/侯赛因/ output.txt的 +回波{ “B64”: “Q2F0IGRvZyAKZWxlcGhhbnQgbGlvbgptb29zZSBidWcK”} + SED -es/B64://克 + TR -d“ + TR -d + TR -d} + TR -d { +的base64 --decode /home/huseyin/output.txt +猫/home/huseyin/output.txt +中的aspell列表 + TR \ n + RESULT = + echo {“result”:“”} {“result”:“”} –