The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
hpux vg accessible but device files missing! mariusp Filesystems, Disks and Memory 1 02-10-2007 03:18 PM
checking missing files in side a folder Nayanajith UNIX for Dummies Questions & Answers 4 06-26-2006 06:05 AM
Missing init files for zsh and bash maag SUN Solaris 2 05-04-2006 04:37 PM
Missing Library Files jays337 UNIX for Dummies Questions & Answers 5 08-18-2005 08:23 PM
files missing cubicle^dweller UNIX for Dummies Questions & Answers 4 09-22-2003 03:10 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-26-2008
gholdbhurg gholdbhurg is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 26
how to check missing files?

I have 50 files in this directory (/home/unixnewbie/wklyfiles) namely:
statistics.1
statistics.2
statistics.3
statistics.4
statistics.5
statistics.6
statistics.7
statistics.8
statistics.9
statistics.10
....
statistics.20
....
statistics.50

How can i determine if ever there will be some files missing in there?
And how to know which exact files are those? Like for example, the statistics.5, statistics.9, statistics.20 and statistics.35 files are missing that week.

Thanks in advance.
  #2 (permalink)  
Old 04-26-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Create a list of all files which should be there, and compare.

Code:
perl -le 'for my $i (1..50) { print "missing: statistics.$i" unless -f "statistics.$i" }'
I meant for the Perl script to just generate the list, but it turned out to be so easy to do it all in Perl. Sorry, folks (-:

Just to illustrate my original proposal, here's another approach, using just simple shell commands:

Code:
yes . | head -50 | nl | sed -e 's/^ *\([1-9][0-9]*\) .*/statistics.\1/' >list
ls -rt statistics.* | diff list -
The file "list" is generated with the nl utility to have line numbers, and the file name prefix is added using sed. (This came out pretty tortured -- it would arguably be a lot easier with awk, but let's just say that using scripting languages would be cheating, okay?) Then we compare that list against the actual directory listing. This requires that your diff accepts "-" to mean "read the other file from standard input;" otherwise, you'll need to use two temporary files. (Don't forget to remove them when you're done.)

Last edited by era; 04-26-2008 at 12:34 PM.. Reason: Alternate approach, just for illustration
  #3 (permalink)  
Old 04-26-2008
gholdbhurg gholdbhurg is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 26
Wow great, thanks a lot era!
Hope you don't mind a follow-up question, what if i want to store in a file all those existing statistics files? And just just put spaces on those missing files?
Example
==>
statistics.1;statistics.2;statistics.3;statistics.4;<space>;statistics.6;......statistics.19; <space>;statistics.21;......statistics.34;<space>;....statistics.50

Hmmm..
  #4 (permalink)  
Old 04-26-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Code:
perl -e 'for my $i (1..50) { my $f = "statistics.$i"; print ($i > 1 ? ";" : ""), (-f "$f" ? "$f" : " ") }
print "\n"'
The "if ? then : else" construct is a bit hard to sort out at first, but makes for pretty straightforward logic once you understand how it works.
  #5 (permalink)  
Old 04-26-2008
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
Another bash solution:

Code:
#!/bin/bash

for nbr in $(seq 50); do
    if [ ! -e "statistics.$nbr" ]; then echo "statistics.$nbr is missing"; fi
done
For the one-liners fanatics:
Code:
for nbr in $(seq 50); do if [ ! -e "statistics.$nbr" ]; then echo "statistics.$nbr is missing"; fi; done
  #6 (permalink)  
Old 04-26-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Proper fanatics would perhaps prefer

Code:
for n in $(seq 50); do [ -e statistics.$n ] || echo statistics.$n is missing; done
seq is not universal, that's why I started out with Perl; but if you have it, it's excellent for this type of job.
  #7 (permalink)  
Old 04-27-2008
gholdbhurg gholdbhurg is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 26
Unfortunately, the seq doesnt work
seq: command not found

Any other unix scripting lines possible?
Btw, i'm using ksh. Thanks!
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:33 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0