user5010107
user5010107

Reputation:

Bash code error unexpected syntax error

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''

Upvotes: 1

Views: 69

Answers (2)

tripleee
tripleee

Reputation: 189317

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.

Upvotes: 5

augurar
augurar

Reputation: 13016

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.

Upvotes: 1

Related Questions