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

 #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
     words = wc -w > $file
     if words < $limit
         rm $file
         count = $count + 1

 print "Number of files deleted: $count" 
  • You may want to learn csh instead. Oct 27 '15 at 16:40
  • this piece of code burns my eyes !
    – netmonk
    Oct 27 '15 at 16:52

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

for file in *.txt
     words=$(wc -w < "$file")
     if [ "$words" -lt "$limit" ]
         rm -- "$file"

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.

  • Thanks. I'm a RA who has to do a crack jobs on a lot of these programming things. Good at the logic but not the syntax. Many thanks. Oct 27 '15 at 16:58
  • @stéphane great edit, as always. Could you explain why you prefer ((var++)) over let var++?
    – terdon
    Oct 27 '15 at 17:16
  • Hmm, why not if ((words < limit)) then? @StéphaneChazelas Oct 27 '15 at 20:01
  • 1
    @Arthur2e5, yes. Personaly, I'd do [ "$words" -lt "$limit" ] and count=$((count + 1)) so it can be a standard sh script as there's no need for bash here. Oct 27 '15 at 21:28

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.