0

Not sure why I am getting this. I realize this must be a common question, but can't figure it out.

#!/bin/bash
 #Checks word count of each text file directory and deletes if less than certain   amount of words
 #Lastly, displays number of files delter

 count = 0 #Set counter to 0
 limit = 2000

 for file in *.txt
 do
     words = wc -w > $file
     if words < $limit
         rm $file
         count = $count + 1
     end
 end

 print "Number of files deleted: $count" 
Vinz
  • 2,150
  • 14
  • 16

3 Answers3

5

I'm afraid your script is full of syntax errors. The specific error you are seeing is because you're not closing the for loop correctly, but there are many, many more:

  1. You can't have spaces around the = when assigning values to variables (except in arithmetic expressions);
  2. In order to save a command's output in a variable, you must use command substitution, either var=`command` or var=$(command);
  3. When referring to the value of a variable, you must use $var, not var and generally, that needs to be quoted ("$var");
  4. When doing an arithmetical comparison, you need to use the -lt of the [ command, not < unless you're using double parentheses;
  5. The command > file format will overwrite file with the output of command. You probaly meant to use wc < "$file" and not wc > $file;
  6. You can't add a value to a variable using var=$var+1 unless that variable has been previously declared as an integer, you need ((var=var+1)), var=$((var+1)) or declare -i var; var=var+1. For adding 1, you can also use ((var++));
  7. Your ifsyntax is wrong. The right format is if condition; then do something; fi
  8. Same goes for the for loop, the right syntax is for loop-specification; do something; done;
  9. There is no print command (not built in bash anyway), only printf and echo;
  10. You should always quote your variables unless there is a good reason not to.

So, a working version of your script with slight improvements, would be:

#!/bin/bash -
# Checks word count of each text file directory and deletes if less than certain   amount of words
# Lastly, displays number of files deleted

count=0 # Set counter to 0
limit=2000

for file in *.txt
do
     words=$(wc -w < "$file")
     if [ "$words" -lt "$limit" ]
     then
         rm -- "$file"
         ((count++))
     fi
done

echo "Number of files deleted: $count" 

Next time, I recommend you familiarize yourself with a language before attempting to code in it. Each language has its own rules and syntax.

terdon
  • 242,166
0

Your loop terminators are incorrect. A for loop is terminated by done, not end. Likewise, the if condition is terminated by fi, not by end.

This is Bourne/POSIX-family shell syntax. Other shells may indeed have those two things terminated by end, but bash does not.

Mingye Wang
  • 1,181
John
  • 17,011
0
words = wc -w > $file

the > char here seems out of place. Without it you would be counting the lines of $file but as it stands you are counting the line of stdin and outputting the result in $file.

chaos
  • 48,171
Aaron
  • 281