Checking Files with a Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking Files with a Shell Script
# 1  
Old 05-20-2008
Question Checking Files with a Shell Script

Hey everyone,

I'm writing a shell script that needs to loop thru a directory and check a defined type of files(.df).

I use "checkfile.x" which is a compiled program to check those files. Sintaxis : checkfile.x DatefileName.df

The program displays something like this:
File: kanswer.df - start search
File: kanswer.df has 1 records
File: kaprvlmt.df - start search
File: kaprvlmt.df has 72 records
File: kapyhour.df - start search
File: kapyhour.df - Last record is NOT a ZZZ record
File: kapyhour.df has 90 records
File: kaseitem.df - start search
File: kaseitem.df has 173 records

Here comes the catch, what I would like to do is, get the files with "Error" (we know if a file has an error buy checking the status for example the legend "Last Record not a ZZZ record" or "Error") and display them so the user will know which files nee do to be fixed. So if we continue working with the example included I should get something like this:
File: kapyhour.df - start search
File: kapyhour.df - Last record is NOT a ZZZ record

And nothing else.


Ok here is what I got so far:
1-Access the files in the directory
2-Check them with the "checkfile.x" program
3-Save the result on $FILE_W_ERR
4-Validate if there's one of the legends("Error" or "ZZZ").
5-If true display the checkfile result else next record.

Code:
# Goes thru the data files
for FILE in *.df
do

    # Checks each of the files and stores the result
    FILE_W_ERR=`checkfile.x $FILE`

    #Looks for the Err message in the return value of checkfile
    INSTR=`echo "$FILE_W_ERR" | egrep -c "ZZZ"`

    # Validate if the checkfile found any errors
    if [ "$INSTR" -ne "0" ]
    then

        # Display file with possible Error
        echo $FILE_W_ERR
        echo #INSTR

    fi

done

This is returning this:
File: kanswer.df - start search
File: kanswer.df has 1 records
File: kaprvlmt.df - start search
File: kaprvlmt.df has 72 records
File: kapyhour.df - start search
File: kapyhour.df - Last record is NOT a ZZZ record
File: kapyhour.df has 90 records
File: kaseitem.df - start search
File: kaseitem.df has 173 records

The exact same result.

I ran out of ideas here, please let me know if you have something I can use to get this done.

Thanks in advance everyone !
# 2  
Old 05-20-2008
If you mean "display" as in "edit" you could for instance:
Code:
#!/bin/bash
[ $# -eq 0 -o -d "$1" ] || { echo "${0//*\//}: dirname"; exit 1; }
find "$1" -type f -iname \*.df | while read FILE; do
 # Results go to stdout, and
 checkfile.x "${FILE}" 2>/dev/null
 # ...provided "checkfile.x" uses proper exit codes, you could...
 [ $? -eq 0 ] || vi "${FILE}"; wait
done
exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to keep checking the directory for files.

Hello Folks, Looking for a script which can keep doing ls to the directories and once file landed to the directory then ,read the files do further calculation and exit. Kindly guide. Regards, Sadique (3 Replies)
Discussion started by: sadique.manzar
3 Replies

2. Shell Programming and Scripting

Help me with C-shell Script: Disk checking

Hi, i am new in shell script. i have given a task to make a C-shell script. I have list of ip address and device name respectively. For example; cal 1 : 100.21.25.10 cal 2 : 100.21.25.11 cal 3 : 100.21.25.12 cal 4 : 100.21.25.14 and so on... Right now, i have this. #! /bin/csh -f ... (0 Replies)
Discussion started by: lattey
0 Replies

3. Shell Programming and Scripting

Service checking through shell script

I want to check the postgres service for client PC which is remotely placed through shell script , whether the Postgres service is working or not.I don't have an idea to develop this script.Please give me a code. Client PC IP Address: 10.66.1.133 (2 Replies)
Discussion started by: kannansoft1985
2 Replies

4. Shell Programming and Scripting

Shell Script for continuously checking status of a another script running in background, and immedia

Hi, I want to write a script which continuously checking status of a script running in background by nohup command. And if same script is not running then immediately start the script...please help.. i am using below command to run script nohup system_traps.sh & but in some... (9 Replies)
Discussion started by: ketanraut
9 Replies

5. Shell Programming and Scripting

Script should keep checking for availability of files.

Hi, I want my script to check for availability of few files contineously for some time. Lets say from 10:00 AM to 10:30 AM script should keep checking on a particular location for each file one by one. If all the files are available then only it should complete the next processing part, else... (2 Replies)
Discussion started by: amit.mathur08
2 Replies

6. Shell Programming and Scripting

checking to see if a service is running in a shell script

How can I tell, in a shell script, if a certain service is running? I know how to do this on the command line, but not in a script. Is an error thrown somehow that I can check? Thanks. (6 Replies)
Discussion started by: daflore
6 Replies

7. Shell Programming and Scripting

Checking the password thru Regex via Shell Script

All, I'm new to shell script. How do I check the password 1)password should have atleast one alpha character and one non-alpha character (0-9 Special) 2)password should not contain three or more repeated alpha numeric characters in a row Can somebody help me in creating the regular... (1 Reply)
Discussion started by: gudikal_karanam
1 Replies

8. Shell Programming and Scripting

Regularly checking with shell script

Hi, I am new in scripting and also in Linux. I wrote my first script recently. I have an external hdd connected to my debian machine. I am spinning it down using a spindown code (code.google.com/p/spindown/)when I am not using it ( automatically after 1 hour). The problem was I wanted to know when... (2 Replies)
Discussion started by: aspedisca
2 Replies

9. Shell Programming and Scripting

checking exit status of a shell script

Hi, I have tried with the following code; if ;then echo "Failure." else echo "Success." fi to test the exit status of the test.ksh shell script. But whatever is the exit status of the test.ksh shell script Failure. is always printed. Please help. regards, Dipankar. (2 Replies)
Discussion started by: kdipankar
2 Replies

10. Shell Programming and Scripting

Checking the valid paths in the shell script

Hi i am using a shell script for renaming the files and placing in the remote location(FTP) my code is: cd $1 day=$(date +%d) for i in `ls -1 BBW*` do last=`tail -1 $i` pat=`expr "$last" : '.*(\(.*\)).*'` pat=`echo $pat | sed 's/ /_/'` pat=$pat$day mv $i $pat rm -f $day done... (3 Replies)
Discussion started by: srivsn
3 Replies
Login or Register to Ask a Question