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:
- You can't have spaces around the
= when assigning values to variables (except in arithmetic expressions);
- In order to save a command's output in a variable, you must use command substitution, either
var=`command` or var=$(command);
- When referring to the value of a variable, you must use
$var, not var and generally, that needs to be quoted ("$var");
- When doing an arithmetical comparison, you need to use the
-lt of the [ command, not < unless you're using double parentheses;
- The
command > file format will overwrite file with the output of command. You probaly meant to use wc < "$file" and not wc > $file;
- 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++));
- Your
ifsyntax is wrong. The right format is if condition; then do something; fi
- Same goes for the
for loop, the right syntax is for loop-specification; do something; done;
- There is no
print command (not built in bash anyway), only printf and echo;
- 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.
cshinstead.