Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A way to check stdin for value?

Status
Not open for further replies.

bigdmy

Technical User
Dec 1, 2002
2
US
I'm troubleshooting a script written years ago by another developer. Roughly the script does the following:

Create an empty file

cat > new_file

Process new_file that now has something in it


The script has been working fine with one exception. If the stdin is empty, the script hangs at "cat > new_file". Is there a way to check stdin for value before attempting the cat or a way past the cat if stdin is empty?

Thanks in advance
 
Why can not you switch to something like "touch new_file", or is there something I am missing?
 
you can simply do to create an empty file:
> new_file

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks for taking the time to reply.

The problem is not with the empty file. The script does create an emty file based on date/time, etc. Once the 0 byte file is created on the disk, the next step is to put something into the new file, hence &quot;cat > new_file&quot;. The something that we are attempting to &quot;cat&quot; into the new_file from stdin is report output generated from a 4gl program. For the most part this process works ok as long as there is actually data in stdin. However, once in a while a user runs a report of some type with criteria that produces an empty report. If this happens, stdin is empty (or appears to be empty) and the script hangs at the &quot;cat&quot;. What I'm interested in is a way to prevent the script from hanging. I thought there might be a way to check if stdin has data in it and code around the cat to exit the script nicely if stdin is empty.

Thanks again for your help.
 
I have been using ksh, but there should be many way to do what you want.
1) use an condition check like if, ! -s, which means its size is zero.
2) could also use a simple command like ls to check if file is empty
3) use awk or sed to see if anything between the beginning and ending

if returns a positive response, then go ahead with your cat command. let me know if you need more specifics on those.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top