I have the following script at hand, i think there are errors but i don't have the knowledge to fix them. The problem is that the script returns nothing but it is supposed to return misspelled words. I run this script with this command
$ sh ./action.sh "{\"b64\":\"`base64 input.txt`\" }"
input.txt has 6 words: Cat dog elephent lion mooose bug
and it returns
{ "result": "" }
but i want it to 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 ($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 think, the problem is about redirecting. I changed >&2 $FILE
to > $FILE 2>&1
then it works. (I don't know why first one is not ok)
Here is the output:
yavuz@ubuntu:/tmp$ sh ./action.sh "{\"b64\":\"`base64 input.txt`\" }"
{ "result": "elephent mooose " }
Code:
#!/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\" }"