Solved

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

Posted on 2007-03-25
7
1,579 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
  • 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
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.

 

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

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

862 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

24 Experts available now in Live!

Get 1:1 Help Now