Why am I getting this error:
line 9: [: too many arguments
when executing this bash script:
IN_FOLDER=period_to_import
cd $IN_FOLDER
for filename in *; do
WC=$(wc -w $filename)
if [ $WC -gt 33 ]
then
rm $filename
fi
done
Why am I getting this error:
line 9: [: too many arguments
when executing this bash script:
IN_FOLDER=period_to_import
cd $IN_FOLDER
for filename in *; do
WC=$(wc -w $filename)
if [ $WC -gt 33 ]
then
rm $filename
fi
done
wc -w filename
returns more than just the number of words. You can solve it like this:
if [ "${WC% *}" -gt 33 ]
then
rm $filename
fi
${WC% *}
trims everything to the right from the last space.
Although it is generally a bad idea to use unquoted variables in single brackets.
You may consider using awk instead:
IN_FOLDER=period_to_import
cd "$IN_FOLDER"
for filename in *; do
if awk -v n=0 '{n++} END{if (n > 33) exit 0; else exit 1}' "$filename"
then
rm "$filename"
fi
done
Just remember that globbing with *
might not work in an empty directory.