1

I am not sure why i am getting the unexpected syntax '( err

#!/bin/bash
DirBogoDict=$1
BogoFilter=/home/nikhilkulkarni/Downloads/bogofilter-1.2.4/src/bogofilter
echo "spam.."

for i in 'cat full/index |fgrep spam |awk -F"/" '{if(NR>1000)print$2"/"$3}'|head -500'
do
        cat $i |$BogoFilter -d $DirBogoDict -M -k 1024 -v
done

echo "ham.."
for i in 'cat full/index | fgrep ham | awk -F"/" '{if(NR>1000)print$2"/"$3}'|head -500'
do
        cat $i |$BogoFilter -d $DirBogoDict -M -k 1024 -v
done

Error:

./score.bash: line 7: syntax error near unexpected token `('
./score.bash: line 7: `for i in 'cat full/index |fgrep spam |awk -F"/" '{if(NR>1000)print$2"/"$3}'|head -500''
5
  • Please clean up your code... four leading spaces on each line would create a code block (with the code block separated with two returns). This is super hard to read as is... No offense. Commented Feb 16, 2016 at 3:27
  • Could you also paste your exact error that you are receiving? Commented Feb 16, 2016 at 3:33
  • stackoverflow.com/tour This will show you a good way to ask a question and you get a token for it. Commented Feb 16, 2016 at 3:34
  • 4
    It looks like there is bunch errors with this. A good resource that I use whenever I am troubleshooting my scripts is: shellcheck.net Commented Feb 16, 2016 at 3:43
  • Also don't forget to select an answer below as resolving your issue. This will help future people to find a correct answer. Commented Feb 16, 2016 at 3:51

2 Answers 2

5

Uh, because you have massive syntax errors.

The immediate problem is that you have an unpaired single quote before the cat which exposes the Awk script to the shell, which of course cannot parse it as shell script code.

Presumably you want to use backticks instead of single quotes, although you should actually not read input with for.

With a fair bit of refactoring, you might want something like

for type in spam ham; do
    awk -F"/" -v type="$type" '$0 ~ type && NR>1000 && i++<500 {
         print $2"/"$3 }' full/index |
    xargs $BogoFilter -d $DirBogoDict -M -k 1024 -v
done

This refactors the useless cat | grep | awk | head into a single Awk script, and avoids the silly loop over each output line. I assume bogofilter can read file name arguments; if not, you will need to refactor the xargs slightly. If you can pipe all the files in one go, try

... xargs cat | $BogoFilter -d $DirBogoDict -M -k 1024 -v

or if you really need to pass in one at a time, maybe

... xargs sh -c 'for f; do $BogoFilter -d $DirBogoDict -M -k 1024 -v <"$f"; done' _

... in which case you will need to export the variables BogoFilter and DirBogoDict to expose them to the subshell (or just inline them -- why do you need them to be variables in the first place? Putting command names in variables is particularly weird; just update your PATH and then simply use the command's name).

In general, if you find yourself typing the same commands more than once, you should think about how to avoid that. This is called the DRY principle.

Sign up to request clarification or add additional context in comments.

1 Comment

Tee hee, we could cheat by passing your $1 as the $0 to the subshell. But this is getting fairly obscure. PATH=/home/nikhilkulkarni/Downloads/bogofilter-1.2.4/src/:$PATH; stuff | xargs sh -c 'for f; do bogofilter -d "$0" -M -k 1024 -v <"$f"; done' "$1"
1

The syntax error is due to bad quoting. The expression whose output you want to loop over should be in command substitution syntax ($(...) or backticks), not single quotes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.