Jerry L
asked on
How to Empty a File in a bash Shell Script
The code is inside a loop that redirects output to a file. That file is then tested for whether it's empty or not. So, at the beginning of each iteration of the loop, I want to empty the file.
Can you tell me how to do that?
Something like this would be nice:
FILE="$path/fileName"
for ... do
$FILE < EOF
Other stuff...
command 2> $FILE
if $FILE not empty
do something
fi
done
Can you tell me how to do that?
Something like this would be nice:
FILE="$path/fileName"
for ... do
$FILE < EOF
Other stuff...
command 2> $FILE
if $FILE not empty
do something
fi
done
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for all the suggestions.
1) cp /dev/null $FILE >> is probably closest to what I was looking for.
2) Brute force is to delete and let first redirect "2>" create it when needed, again.
3) If ">$FILE" works, that may be most elegant.
4) command 2> $FILE doesn't help when testing loop iterations.
1) cp /dev/null $FILE >> is probably closest to what I was looking for.
2) Brute force is to delete and let first redirect "2>" create it when needed, again.
3) If ">$FILE" works, that may be most elegant.
4) command 2> $FILE doesn't help when testing loop iterations.
command 2> $FILE doesn't help when testing loop iterations.
what are you testing that it doesn't help?
what are you testing that it doesn't help?
ASKER
>$FILE does work
will empty the file before it writes