Solved

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

Posted on 2007-03-25
7
1,583 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Making an alias 7 95
Determine Who is Runnig my Bash Shell Script 4 80
Run same command on multiple files in Linux 3 32
powershell function location 6 42
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.

770 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