Emptying files in a loop

I'm trying to write a script that will empty all contents in a set of files in a directory - without deleting the files themselves.
I know the command (at BASH prompt) is: > (filename)

I'm iterating over all files in the directory and calling the > command thus :

FILES=*.log
for f in "$FILES"
do
        echo "Emptying file "$f
        > $f
        echo "Emptied file "$f
done

However, this doesn't seem to be doing anything - what am I missing?

Jim

It should work:

civ:/datanfs/data $ echo $F;ll $F;>$F;ll $F
ls_out.txt
-rwxr-x---   1 vbe        bin           1039 Jun 14 17:48 ls_out.txt
-rwxr-x---   1 vbe        bin              0 Jun 14 17:49 ls_out.txt

---------- Post updated at 17:56 ---------- Previous update was at 17:52 ----------

Second thought: Its maybe your script - but a guess since I use ksh and have no bash to test... I suspect the FILES=*...
It should be:

FILES=$(ls *.log)

Or its equivalent for bash...

Changed it to

FILES=$(ls *.log)
for f in $FILES
do
        >"The file : $f"
done

It throws
syntax error at line 1: `FILES=$' unexpected

this is odd since
FILES=$(ls *.log) works at the bash prompt
but not inside a .sh file!!!! Any idea why this could be happening?

AS I said Im no bash specialist... but I would try:

export FILES="$(ls *.log)"
...

How about ...

for FILE in $( ls *.log )

This works in Bash. (Not sure about the sh shell you're mentioning, though ...)

That code is fine, just don't quote "$FILES". The double quotes prevent file globbing from occuring, so "$FILES" expands to "*.log" and that's it. The loop will execute once and try to truncate a file named *.log. Without the quotes, $FILES expands to *.log which is then expanded further into a list of all files that match the pattern.

Regards,
Alister

Alister - that did it. Tho am pretty sure everyone was pretty much right in the end as well!! Sucks to be newb!!! :b:

Thanks all!!!