Solved

How to Empty a File in a bash Shell Script

Posted on 2010-11-15
7
715 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:WizeOwl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 350 total points
ID: 34141658
To empty a file, you can just do:

cp /dev/null $FILE
0
 
LVL 5

Assisted Solution

by:sandipshah
sandipshah earned 50 total points
ID: 34141667
Or delete the file and re-open it with the same name.

Ss
0
 
LVL 12

Assisted Solution

by:tel2
tel2 earned 100 total points
ID: 34141683
Or simply:

    >$FILE
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 34141689
command 2> $FILE
will empty the file before it writes
0
 

Author Comment

by:WizeOwl
ID: 34141739
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 34141773
command 2> $FILE doesn't help when testing loop iterations.
what are you testing that it doesn't help?
0
 

Author Closing Comment

by:WizeOwl
ID: 34141784
>$FILE does work
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Shell Mail is not working 5 123
Call Shell Script from Perl Script 6 165
AWK: Pythagoras script 6 35
Merging two files with Perl 5 29
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question