Solved

Check if Data file zero length and Count File contains all zeros

Posted on 2007-03-25
7
1,588 Views
Last Modified: 2013-12-26
Hi,

I am writing a script in korn shell and need to do the following
1. Check if the incoming DATA file is of zero length
2. and if the DATA file is zero length check if the COUNT file is either empty or contains all zeros as nos

Hence
if [ -s DATAfilename ] and [ Count File contains nothing (empty) OR Count File contains all zeros]

then
  something
else
  something else
An actual example would be useful.
0
Comment
Question by:hcdev
[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
  • 4
  • 3
7 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 18789439
This is a little long winded, but will do what you require.

#!/bin/ksh
zero=`grep -v 0 countfile`

if [[ -f datafile && ! -s datafile ]] && [[ -f countfile && ! -s countfile || -z "$zero" ]]
then
   echo true
else
   echo false
fi
0
 

Author Comment

by:hcdev
ID: 18794850
Hi Tintin,

Thanks for the reply. However I had 2 questions

1. The count file must contain all zeros. For example the count file can contain '000119' in this case the count file does not contain all zeros and hence the result should be a failure (else step). It should succeed only if the count file contains all zeros eg '000000'

OR

2. The count file is empty then succeed
0
 
LVL 48

Expert Comment

by:Tintin
ID: 18796535
Assuming count file only contains numbers, then change

zero=`grep -v 0 countfile`

to

zero=`grep  0 countfile | grep "[1-9]"`
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!

 

Author Comment

by:hcdev
ID: 18796673
Hi Tintin

I responded to your earlier solution too. The problem here is with

[[ -f countfile && ! -s countfile || -z "$zero" ]]

Again what I need is to

1. Check if count file is not empty then it contains all zero's so for eg "0001119" is not valid but "0000000" is valid. Hence the only nos must be only zero's
OR
2. The count file is empty i.e. contains nothing'

All this with a zero byte data file

Your solution greps the file for any occurence of zero and then checks if the $zero is an empty string.
It does not provide me with what I am looking for. I tried it but does not work.

Thanks
0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 18796779
My script works according to the information given, eg:

$ cat script.ksh
#!/bin/ksh
zero=`grep 0 countfile | egrep "[1-9]"`

if [[ -f datafile && ! -s datafile ]] && [[ -f countfile && ! -s countfile || -z "$zero" ]]
then
   echo true
else
   echo false
fi

$ ls -l countfile datafile
-rw-r--r--   1 tintin   tintin        8 Mar 27 11:45 countfile
-rw-r--r--   1 tintin   tintin        0 Mar 26 08:39 datafile

$ cat countfile
00000

$ ./script.ksh
true

$ echo 0001119 >countfile
$ ./script.ksh
false
0
 

Author Comment

by:hcdev
ID: 18796820
Hi Tintin,

The reason it was failing was because the count file necessarily does not contain a zero with a positive number [1-9] Hence I had count files with values such as 121 which did not have a zero and hence it would evaluate to true. But I figured it out. I shall still accept your answer. Thanks for all your help
0
 
LVL 48

Expert Comment

by:Tintin
ID: 18796860
In that case, if you change

zero=`grep 0 countfile | egrep "[1-9]"`

to

zero=`egrep "[0-9]" countfile | egrep "[1-9]"`
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

Suggested Solutions

Title # Comments Views Activity
Issue with cURL on Windows 7 130
Showing a username in UNIX and using that name in the script 4 73
Run same command on multiple files in Linux 3 84
How to monitor servers ? 4 64
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
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…

739 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