• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1597
  • Last Modified:

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

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
hcdev
Asked:
hcdev
  • 4
  • 3
1 Solution
 
TintinCommented:
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
 
hcdevAuthor Commented:
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
 
TintinCommented:
Assuming count file only contains numbers, then change

zero=`grep -v 0 countfile`

to

zero=`grep  0 countfile | grep "[1-9]"`
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
hcdevAuthor Commented:
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
 
TintinCommented:
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
 
hcdevAuthor Commented:
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
 
TintinCommented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now