Solved

How to Empty a File in a bash Shell Script

Posted on 2010-11-15
7
700 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now