UNIX Shell Scripting (Solaris) for File Checking

Hi R.Singh,

followed your code

tahnks.

If we start from my script in post #32 in this thread:

#!/bin/ksh
bad_found=0
dir='/files05/home/chgtprd/MXS/inb/CHTR/FTR/9/'
log='/files05/home/chgtprd/logfile.txt'

cd "$dir" && for file in *
do	if [ -f "$file" ] && ! /usr/xpg4/bin/grep -q FTRORD "$file"
	then	printf 'Invalid file "%s" in "%s"\n' "$file" "$dir"
		bad_found=1
	fi
done > "$log"
if [ $bad_found -eq 0 ]
then	printf 'There are no invalid files in "%s"\n' "$dir" > "$log"
fi

we find two things (marked in red) that you want to change as you process various directories. We can't simply use RavinderSingh13's proposal because we need to modify not only the directory as we process different directories, but also change the search pattern, and because just putting the code I had in a simple loop will overwrite the results of all but the last directory processed in the output log file. So, instead of just having the directories listed in Input_file (as Ravinder suggested), we also need to have the corresponding search patterns in that file. With the data you supplied in post #35, that would be something like:

/files05/home/chgtprd/MXS/inb/CHTR/FTR/9 FTRORD
/files05/home/chgtprd/GT/outb/CHTR/VRZ/9/send CHTRVRZPRD
/files05/home/chgtprd/GT/outb/CHTR/VRZ/9/send_ia/success CHTRVRZPRD
/files05/home/chgtprd/GT/outb/CHTR/FTR/9/send CHTRVRZPRD
/files05/home/chgtprd/GT/outb/CHTR/FTR/9/send_ia CHTRFTRORDP
/files05/home/chgtprd/GT/outb/CHTR/FTR/997/send CHTRFTRORDP
/files05/home/chgtprd/GT/outb/CHTR/VRZ/997/send CHTRFTRORDP

I used a space between the directory name and the search pattern (since there are no spaces in any of your pathnames and there are no spaces in any of your search patterns). The character chosen to separate these two pieces of data can be any character except a NUL byte, but needs to a character that is not in any of your directory pathnames. Using a space or a tab character as the separator allows us to use the shell's default field separators to keep the code as simple as possible.

Since this file contains configuration data for your script, let us call this a configuration file. And, if the script that uses this file is named dave2.sh , let us name this configuration file dave2.conf . And, your script needs to know where to find this file (just like it needs to know where to write your log file). So, we will make a few minor modifications to the script above to:

  1. add a variable specifying where to find your configuration file,
  2. add a loop to process each directory (with its associated search pattern),
  3. move the output redirection so the output from each pass through the loop will be written to your log file instead the output from each pass overwriting the output from the previous pass, and
  4. since your search patterns are all fixed strings, use grep -Fq (instead of just grep -q ) to make it run slightly faster.

That leads to something like:

#!/bin/ksh
config='/files05/home/chgtprd/dave2.conf'
log='/files05/home/chgtprd/logfile.txt'

while read dir pattern
do	bad_found=0
	cd "$dir" && for file in *
	do	if [ -f "$file" ] && ! /usr/xpg4/bin/grep -Fq "$pattern" "$file"
		then	printf 'Invalid file "%s" in "%s"\n' "$file" "$dir"
			bad_found=1
		fi
	done
	if [ $bad_found -eq 0 ]
	then	printf 'There are no invalid files in "%s"\n' "$dir"
	fi
done < "$config" > "$log"

Note that this assumes that you have placed the file dave2.conf in the same directory where you want the log file to be placed. You can choose a different location for that file if you want to, but wherever you put it, an absolute pathname for that file has to be the value assigned to the variable config at the start of the script.

This is totally untested, but should come close to what you seem to want.

2 Likes

Hi don,

i followed all your suggestions and instrcutions and i work perfectly as i want.. THANK YOU VERY VERY MUCH for all the help.

i got the output that i wanted and thank you thank you honestly thank you so much for all the help..

ALSO BIG THANKS TO:
RudiC
MadeInGermany
RavinderSingh13 (R.Singh)
jlliagre

for all there contributions.. THANK YOU ALL GUYS.

(note: please dont close this thread yet so that we can come back on this thread any time..)

thank you all!!!! thank you guys so much.>! thank you sir don cragun! Thank you..! :smiley: :smiley: :smiley:

Hi guys../don,

im back and i need again your help. .sorry.. :frowning:

#!/bin/ksh
config='/home/abainzd/dave.conf'
log='/home/abainzd/logfile.txt'

while read dir pattern
do	bad_found=0
	cd "$dir" && for file in *
	do	if [ -f "$file" ] && ! /usr/xpg4/bin/grep -Fq "$pattern" "$file"
		then	printf 'Invalid file "%s" in "%s"\n' "$file" "$dir"
			bad_found=1
		fi
	done
	if [ $bad_found -eq 0 ]
	then	printf 'There are no invalid files in "%s"\n' "$dir"
	fi
done < "$config" > "$log"

thank you for your code but my boss wants modifacations so i edited it so that the only ouput it will log is

Invalid file "dave2.txt" in "/files05/home/chgtpst/MXS/inb/CHTR/FTR/9"

i mean the only output it will log in the logfile.txt is the invalid files and its directories. so it looks like this

#!/bin/ksh
config='/home/abainzd/dave.conf'
log='/home/abainzd/logfile.txt'

while read dir pattern
do      bad_found=0
        cd "$dir" && for file in *
        do      if [ -f "$file" ] && ! /usr/xpg4/bin/grep -Fq "$pattern" "$file"
                then    printf 'Invalid file "%s" in "%s"\n' "$file" "$dir"
                        bad_found=1
                fi
        done
done < "$config" > "$log"

and it perfectly works..

and another modification is they dont want the script to create a logfile.txt, they want it to append a logfile which is

/ats/1.0.0/pst/release/logs/events.log

in the format of

12/24/2015_17:52:17|ALERT2|ATS|chk_dirs.pl@mhapdwmap005:

i mean like this

*DATE*_*TIME|ALERT2|ATS|chk_dirs.pl@mhapdwmap005: Invalid files "dave.txt" in this *directory*

the date and time is my probelam beacuse

|ALERT2|ATS|chk_dirs.pl@mhapdwmap005:

is a standard/default phrase of ours..

pleease help me once again.. thanks.

Going back and removing significant portions of your earlier posts (that provided information on which responses to those posts were based is VERY BAD FORM! Do Not Do That! People who read this (horrendously long) thread in the future need to be able to see the entire discussion, not a discussion with major chunks of the text removed! (If this isn't acceptable to you, we can remove the thread and ban you from this site.)

We are not your unpaid programming staff. If your boss wants me to rewrite code I have supplied as a method to help you learn how to write code, have your boss send me a private message telling me what changes he or she wants me to make and how much he will pay me to do it. Do not keep changing your requirements and expect us to keep changing code we have suggested to meet new requirements the unnamed "they" want. Do not tell us our code is wrong because we did not supply output that meets your output formatting standards when you never mentioned any output formatting standards before.

I told you exactly what you need to change in this script to append to a log file instead of replace it in an earlier message in this thread. Do I really have to tell you how to do that again? Can't you reread this thread instead of asking us to repeat ourselves?

Are you really unable to figure out how to change your script to use a different pathname for the output log file used by your script? Please try to do this on your own, and show us what you have done if it doesn't work when you try it.

I would not think you would have trouble modifying the printf command in the script I suggested to get your standard output format, although we might be willing to help you add the date and timestamp. I have absolutely no idea why your standard would call for timestamps approximately five and a half months old. If you expect us to help you add seemingly random old timestamps to log entries you are creating, you need to give us exact specifications on what date and time are to be used. Are the dates and timestamps somehow related to one of the timestamps on the invalid files? If these are intended to be current timestamps and you just provided a confusing example, please try using the date utility to capture the data you want to include your log entries instead of just asking us to do it for you. If you show us that you have made an effort to make these changes on your own, but need a little help; we will be happy to help you. If you don't show us that you are willing to try to learn how to do this on your own, we are wasting our time here.

Hi don,

im so sorry for being stupid and stubborn on my posts.. sorry sorry sorry.. i trul am sorry.. and..

i remember what you told me in "appending" and in changing the directories of log so here it is..

#!/bin/ksh
config='/home/abainzd/dave.conf'
log='/ats/1.0.0/pst/release/logs'

while read dir pattern
do      bad_found=0
        cd "$dir" && for file in *
        do      if [ -f "$file" ] && ! /usr/xpg4/bin/grep -Fq "$pattern" "$file"
                then    printf '|ALERT2|ATS|chk_dirs.pl@mhappmmap001:Invalid file "%s" in "%s"\n' "$file" "$dir"
                        bad_found=1
                fi
        done
done < "$config" >> "$log"

then it works fine,,

results is:

|ALERT2|ATS|chk_dirs.pl@mhappmmap001:Invalid file "dave.txt" in "/files05/home/c
hgtpst/MXS/inb/CHTR/FTR/9"

then i tired to add date_time stamp..

#!/bin/ksh
config='/home/abainzd/dave.conf'
log='/ats/1.0.0/pst/release/logs/events.log'
now="$(date +'%d/%m/%Y_%X')"

while read dir pattern
do      bad_found=0
        cd "$dir" && for file in *
        do      if [ -f "$file" ] && ! /usr/xpg4/bin/grep -Fq "$pattern" "$file"
                then    printf "$now" '|ALERT2|ATS|chk_dirs.pl@mhappmmap001:Inva
lid file "%s" in "%s"\n' "$file" "$dir"
                        bad_found=1
                fi
        done
done < "$config" >> "$log"

and resulted to:

09/06/2016_23:12:50

may i ask what is wrong with my addition into the script ? why is that the expected output which is

09/06/2016_23:12|ALERT2|ATS|chk_dirs.pl
@mhappmmap001:Invalid file "dave.txt" in "/files05/home/chgtpst/MXS/inb/CHTR/FTR
/9"

is not met? thank you and sorry once again.

Hi Dave,
OK. So now we know that you are not stupid (and please do not say that you are again) and, if you try to make some changes on your own, you get most of it right. As I said before, if you make an honest effort to do the work on your own, we are happy to help you with problems if you get stuck. Just don't expect us to do everything for you when you should be able to do most of it on your own.

The only real problem you have left is that you changed the printf command format argument to a fixed string (the date and timestamp) instead of adding another %s argument to the format string and adding the date and timestamp data in the appropriate spot in the argument list.

What happens if you change:

		then	printf "$now" '|ALERT2|ATS|chk_dirs.pl@mhappmmap001:Invalid file "%s" in "%s"\n' "$file" "$dir"

to:

		then	printf '%s|ALERT2|ATS|chk_dirs.pl@mhappmmap001:Invalid file "%s" in "%s"\n' "$now" "$file" "$dir"

or, to avoid lines in your script getting wider than 80 characters:

		then	printf '%s|%s file "%s" in "%s"\n' "$now" \
			    'ALERT2|ATS|chk_dirs.pl@mhappmmap001:Invalid' \
			    "$file" "$dir"

(Note that this assumes that you want one line of output per invalid file instead of the three lines of output shown in post #46.)

And, since you no longer need to keep track of whether or not an invalid file is found in a directory (since you no longer want to print a message if all files in a directory are valid), there is no need to reset the bad_found variable in each directory-processing loop nor to update it when an invalid file is found. So, you might want to see if the slightly simpler:

#!/bin/ksh
config='/home/abainzd/dave.conf'
log='/ats/1.0.0/pst/release/logs/events.log'
now="$(date +'%d/%m/%Y_%X')"

while read dir pattern
do	cd "$dir" && for file in *
	do	if [ -f "$file" ] && ! /usr/xpg4/bin/grep -Fq "$pattern" "$file"
		then	printf '%s|%s file "%s" in "%s"\n' "$now" \
			    'ALERT2|ATS|chk_dirs.pl@mhappmmap001:Invalid' \
			    "$file" "$dir"
		 fi
	done
done < "$config" >> "$log"

works for you.

Note, however, that the date and time format string you said was your standard output format in post #44 showed a date in the format MM/DD/YYYY and the output your are producing above is in the format DD/MM/YYYY. I assume you can change the date command format string in your script if you want to switch the order of the month and day numbers in your output.

hi don,

i have used your final code and changed that format of the date,

#!/bin/ksh
config='/home/abainzd/dave.conf'
log='/ats/1.0.0/pst/release/logs/events.log'
now="$(date +'%m/%d/%Y_%X')"

while read dir pattern
do	cd "$dir" && for file in *
	do	if [ -f "$file" ] && ! /usr/xpg4/bin/grep -Fq "$pattern" "$file"
		then	printf '%s|%s file "%s" in "%s"\n' "$now" \
			    'ALERT2|ATS|chk_dirs.pl@mhappmmap001:Invalid' \
			    "$file" "$dir"
		 fi
	done
done < "$config" >> "$log"

and it works perfecly.. result is..

06/12/2016_22:55:53|ALERT2|ATS|chk_dirs.pl@mhappmmap001:Invalid file "dave.txt"
in "/files05/home/chgtpst/MXS/inb/CHTR/FTR/9"

thank you so much once again for the perfect results.