Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to Empty a File in a bash Shell Script

Posted on 2010-11-15
7
Medium Priority
?
728 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 27

Accepted Solution

by:
wilcoxon earned 1400 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 200 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 400 total points
ID: 34141683
Or simply:

    >$FILE
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Industry Leaders: 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

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Utilizing an array to gracefully append to a list of EmailAddresses
Six Sigma Control Plans
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…

618 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