Simple Shell Script to Grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple Shell Script to Grep
# 8  
Old 04-24-2010
Thanks ygemici!

I try to change your code, so that i get "ERROR: Not found in (/bin/dir?)" when it cannot find a file in a directory. But it gives me error. This is what i changed:

Code:
 cat s1
#!/bin/bash
 
a=0
b=1
c=0
COUNT=0
CHKCNT=0
WRITE=0
#Which Search Files
WHICHFILE=ORProxyTC`date '+%m%d%Y'`
# Which Dir Listing
DIRS=( "/home/student/bin/dir1" "/home/student/bin/dir2" "/home/student/bin/dir3" "/home/student/bin/dir4" )
#Processing Which Files in Which Dirs
for WHICHDIR in ${DIRS[*]}
    do
 for i in $( (ls -1 $WHICHDIR/$WHICHFILE*.txt) )
  do
 
    FILES[c]="$i"
    GREPFUNC[c]=$(grep -i -E -H "login|Successfully" ${FILES[c]})
 if [ $? -eq 0 ] ; then
  ((++COUNT))
  GREPFUNCOK[a]=${GREPFUNC[c]}
  ((++a))
 fi
  ((++c))
 
dirp1=${WHICHDIR%*/*/*} ; dirp1=/${dirp1#*/*/}
dirp2=${WHICHDIR#/*/*/*/*}
DIROK=$( (echo $dirp1/$dirp2) )
DIRTMP[b]="$DIROK"
 
 if [ $CHKCNT -eq 0 ] ; then
 echo "$DIROK"
 else
     if [ "${DIRTMP[b]}" != "${DIRTMP[b-1]}" ] ; then
  echo "${DIRTMP[b]}"
     fi
       fi
   ((++CHKCNT))
  else 
	echo "ERROR: Cannot find file in directory ${DIRS}"
   ((++b))
  done
 a=0
 COUNTOK=$COUNT
 
 while [ $(( COUNT -= 1 )) -gt 0 ]
  do
  echo "Search completed and found: ${GREPFUNCOK[a]}."
  echo "#################################################################"
  ((++a))
 done
echo "Search completed and found: ${GREPFUNCOK[a]}."
echo "This is the total number of files: ${COUNTOK}."
echo "#################################################################"
echo ""
COUNTOK=0
COUNT=0
c=0
a=0
done

Do you know how to make it show error message if it cannot find a file in a directory? and when it cannot find a directory?

Thanks!

Last edited by DallasT; 04-25-2010 at 08:25 AM..
# 9  
Old 04-25-2010
MySQL

Hmm Okey just add o few controls Smilie

Code:
a=0 
b=1
c=0
COUNT=0
CHKCNT=0
dir_file_check=""
#Which Search Files
WHICHFILE=ORProxyTC`date '+%m%d%Y'`
# Which Dir Listing
DIRS=( "/home/student/bin/dir1" "/home/student/bin/dir2" "/home/student/bin/dir3" "/home/student/bin/dir4" )
 
#Processing Which Files in Which Dirs
for WHICHDIR in ${DIRS[*]}
    do
 
# Directory Controls
ls -1 $WHICHDIR &> /dev/null
if [ $? -ne 0 ] ; then
     Msg1="Directory Not Found ($WHICHDIR)"
     dir_file_check=1
else
 
# If Directory is there and Matced Files Not in our dir
FILEINDIRCHECK=`ls -1 $WHICHDIR/$WHICHFILE*.txt 2>/dev/null`
  if [ "$FILEINDIRCHECK" == "" ] ; then
       Msg2="Matched Files Not Found in ($WHICHDIR)"
       dir_file_check=2
    else
    dir_file_check=0  # Directory Ok
  fi
 
fi
 
#Directory Remove Which there is Not or No Matched Files in
if [ $dir_file_check -ne 0 ] ; then
     echo ""
     DIRNOTFOUND=$WHICHDIR
     DIRS=`echo ${DIRS[@]#$DIRNOTFOUND}`
 
#Write A Message
case $dir_file_check in
1)
echo $Msg1 ;;
2)
echo $Msg2 ;;
esac
echo ""
fi
done
 
# And Normal Process Go on
for WHICHDIR in ${DIRS}
do
for i in $( (ls -1 $WHICHDIR/$WHICHFILE*.txt) )
           do
                FILES[c]="$i"
GREPFUNC[c]=$(grep -i -E -H "login|Successfully" ${FILES[c]})
                      if [ $? -eq 0 ] ; then
                           ((++COUNT))
GREPFUNCOK[a]=${GREPFUNC[c]}
                           ((++a))
                    fi
((++c))
 
dirp1=${WHICHDIR%*/*/*} ; dirp1=/${dirp1#*/*/}
dirp2=${WHICHDIR#/*/*/*/*}
DIROK=$( (echo $dirp1/$dirp2) )
 
DIRTMP[b]="$DIROK"
 
if [ $CHKCNT -eq 0 ] ; then
echo "$DIROK"
else
if [ "${DIRTMP[b]}" != "${DIRTMP[b-1]}" ] ; then
echo "${DIRTMP[b]}"
fi
fi
((++CHKCNT))
((++b))
done
a=0
COUNTOK=$COUNT
while [ $(( COUNT -= 1 )) -gt 0 ]
do
  echo "Search completed and found: ${GREPFUNCOK[a]}."
  echo "#################################################################"
((++a))
done
      echo "Search completed and found: ${GREPFUNCOK[a]}."
      echo "This is the total number of files: ${COUNTOK}."
                      echo "#################################################################"
                      echo ""
                      COUNTOK=0
                      COUNT=0
                      c=0
                      a=0
done


There is a file but is not we want that pattern match files
Code:
 
ls -l /home/student/bin/dir3
total 0
-rw-r--r-- 1 root root 0 Mar 28 23:12 ORProxyTC03282.txt

Code:
root@rhnserver bashscproblems]# ./s1v1
 
Matched Files Not Found in (/home/student/bin/dir3)
 
/student/dir1
Search completed and found: /home/student/bin/dir1/ORProxyTC03282010.txt:login|Successfully.
This is the total number of files: 1.
#################################################################
 
/student/dir2
Search completed and found: /home/student/bin/dir2/ORProxyTC03282010.txt:login|Successfully.
This is the total number of files: 1.
#################################################################
 
/student/dir4
Search completed and found: /home/student/bin/dir4/ORProxyTC03282010111.txt:login|Successfully.
#################################################################
Search completed and found: /home/student/bin/dir4/ORProxyTC032820101.txt:login|Successfully.
#################################################################
Search completed and found: /home/student/bin/dir4/ORProxyTC03282010.txt:login|Successfully.
This is the total number of files: 3.
#################################################################

Another example
Code:
 
[root@rhnserver bashscproblems]# ls -ld /home/student/bin/*/
drwxr-xr-x 2 root root 4096 Mar 28 05:24 /home/student/bin/dir1/
drwxr-xr-x 2 root root 4096 Mar 28 05:25 /home/student/bin/dir2/
drwxr-xr-x 2 root root 4096 Mar 28 13:25 /home/student/bin/dir4/

Code:
 
[root@rhnserver bashscproblems]# ./s1v1
 
Directory Not Found (/home/student/bin/dir3)
 
/student/dir1
Search completed and found: /home/student/bin/dir1/ORProxyTC03282010.txt:login|Successfully.
This is the total number of files: 1.
#################################################################
 
/student/dir2
Search completed and found: /home/student/bin/dir2/ORProxyTC03282010.txt:login|Successfully.
This is the total number of files: 1.
#################################################################
 
/student/dir4
Search completed and found: /home/student/bin/dir4/ORProxyTC03282010111.txt:login|Successfully.
#################################################################
Search completed and found: /home/student/bin/dir4/ORProxyTC032820101.txt:login|Successfully.
#################################################################
Search completed and found: /home/student/bin/dir4/ORProxyTC03282010.txt:login|Successfully.
This is the total number of files: 3.
#################################################################

# 10  
Old 04-26-2010
Thanks again ygemici Smilie

Now I tried to add a few more things, but i could not make it work.

How can I make the script to email? For example:

If there is a "Matched Files Not Found" output, the email subject should say "ERROR:Problem with files"

And when there is no problem, it should say "GOOD: All files ok" in the subject?

In both cases, the email should include the output of the script in its body.

I am thinking to add this, please correct me:

Code:
!/bin/bash
# script to send simple email
# email subject
SUBJECT="SET-EMAIL-SUBJECT"
# Email To ?
EMAIL="admin@somewhere.com"
# Email text/message
EMAILMESSAGE="/tmp/emailmessage.txt"
echo "This is an email message test"> $EMAILMESSAGE
echo "This is email text" >>$EMAILMESSAGE
# send an email using /bin/mail
/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

# 11  
Old 04-27-2010
MySQL

Quote:
Originally Posted by DallasT
Thanks again ygemici Smilie

Now I tried to add a few more things, but i could not make it work.

How can I make the script to email? For example:

If there is a "Matched Files Not Found" output, the email subject should say "ERROR:Problem with files"

And when there is no problem, it should say "GOOD: All files ok" in the subject?

In both cases, the email should include the output of the script in its body.

I am thinking to add this, please correct me:

Code:
!/bin/bash
# script to send simple email
# email subject
SUBJECT="SET-EMAIL-SUBJECT"
# Email To ?
EMAIL="admin@somewhere.com"
# Email text/message
EMAILMESSAGE="/tmp/emailmessage.txt"
echo "This is an email message test"> $EMAILMESSAGE
echo "This is email text" >>$EMAILMESSAGE
# send an email using /bin/mail
/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

Hi DallasT

Your solution is seem correct Smilie
Did you test?
# 12  
Old 04-27-2010
Hi ygemici. Yes I wrote the below code and this is the problem I am having:

Code:
a=0 
b=1
c=0
COUNT=0
CHKCNT=0
dir_file_check=""
#Which Search Files
WHICHFILE=ORProxyTC`date '+%m%d%Y'`
# Which Dir Listing
DIRS=( "/home/student/bin/dir1" "/home/student/bin/dir2" "/home/student/bin/dir3" "/home/student/bin/dir4" )
 
#Processing Which Files in Which Dirs
for WHICHDIR in ${DIRS[*]}
    do
 
# Directory Controls
ls -1 $WHICHDIR &> /dev/null
if [ $? -ne 0 ] ; then
     Msg1="ERROR: Directory Not Found ($WHICHDIR)"
     dir_file_check=1
else
 
# If Directory is there and Matced Files Not in our dir
FILEINDIRCHECK=`ls -1 $WHICHDIR/$WHICHFILE*.txt 2>/dev/null`
  if [ "$FILEINDIRCHECK" == "" ] ; then
       Msg2="ERROR: Matched Files Not Found in ($WHICHDIR)"
       dir_file_check=2
    else
    dir_file_check=0  # Directory Ok
  fi
 
fi
 
#Directory Remove Which there is Not or No Matched Files in
if [ $dir_file_check -ne 0 ] ; then
     echo ""
     DIRNOTFOUND=$WHICHDIR
     DIRS=`echo ${DIRS[@]#$DIRNOTFOUND}`
 
#Write A Message
case $dir_file_check in
1)
echo $Msg1 ;;
2)
echo $Msg2 ;;
esac
echo ""
fi
done
 
# And Normal Process Go on
for WHICHDIR in ${DIRS}
do
for i in $( (ls -1 $WHICHDIR/$WHICHFILE*.txt) )
           do
                FILES[c]="$i"
GREPFUNC[c]=$(grep -i -E -H "login|Successfully" ${FILES[c]})
                      if [ $? -eq 0 ] ; then
                           ((++COUNT))
GREPFUNCOK[a]=${GREPFUNC[c]}
                           ((++a))
                    fi
((++c))
 
dirp1=${WHICHDIR%*/*/*} ; dirp1=/${dirp1#*/*/}
dirp2=${WHICHDIR#/*/*/*/*}
DIROK=$( (echo $dirp1/$dirp2) )
 
DIRTMP[b]="$DIROK"
 
if [ $CHKCNT -eq 0 ] ; then
echo "$DIROK"
else
if [ "${DIRTMP[b]}" != "${DIRTMP[b-1]}" ] ; then
echo "${DIRTMP[b]}"
fi
fi
((++CHKCNT))
((++b))
done
a=0
COUNTOK=$COUNT
while [ $(( COUNT -= 1 )) -gt 0 ]
do
  echo "Search completed and found: ${GREPFUNCOK[a]}."
  echo "#################################################################"
((++a))
done
      echo "Search completed and found: ${GREPFUNCOK[a]}."
      echo "This is the total number of files: ${COUNTOK}."
                      echo "#################################################################"
                      echo ""
                      COUNTOK=0
                      COUNT=0
                      c=0
                      a=0
echo "##############################################################"

#Mail Process

# script to send simple email
# email subject
SUBJECT="ERROR: Problem with System"
# Email To ?
EMAIL="admin@somewhere.com"
# Email text/message
EMAILMESSAGE="/tmp/emailmessage.txt"
echo "The script ran"> ${GREPFUNCOK[a]}

But how do I add logic for subject to change "GOOD" when no error and "BAD" when there is an error?

Smilie
# 13  
Old 04-28-2010
Which state is Good or Bad?
# 14  
Old 04-28-2010
When the result comes with "ERROR:" it is bad.

When there is no ERROR, it is good.

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help on simple shell script

Hello, I need to create one very simple shell script that checks if the first character of the file ./pump.txt is 0 and in that case gives a message. If the first character is instead 1, it does give a different message. I have written: irr= head -c 1 ./pump.txt if ]; then echo... (4 Replies)
Discussion started by: dcaccount
4 Replies

2. Shell Programming and Scripting

Help on simple shell script

Hello, I am running openmediavault on my Raspberry and I would like to use it as a backup FTP server of snapshots taken from my IP cams. So I get the network recorder to upload every 3 seconds a snapshot to the Raspberry. Everything works perfectly. I would need now a simple script that... (5 Replies)
Discussion started by: dcaccount
5 Replies

3. Shell Programming and Scripting

Help with simple Shell Script

Hi , I am in need of simple shell script that has one input file containing some words Input file 1 : ****ALEX***JOHN*******VIRGIL***** CHRITINE*****FAISAL*****DON***** ****ALEX***JOHN*******VIRGIL***** CHRITINE*****FAISAL*****DON***** ****ALEX***JOHN*******VIRGIL*****... (6 Replies)
Discussion started by: kmanjuna
6 Replies

4. Shell Programming and Scripting

Help with very simple Shell Script

Write a shell that receives as parameters two folder names. Copies the second folder as subfolder into the first one. Only folders and files with the '.txt' extension will be copied. Detect and avoid recursive copy. This is what I have to do and I don't know where to start. In fact, I started... (5 Replies)
Discussion started by: sharpsh07er
5 Replies

5. Shell Programming and Scripting

Simple grep script

I'm trying to write a simple script to identify every user who tried to “sudo” on the system. I have the first portion down to grep the log file grep “sudo” /var/log/secure. What I want to do is have the script identify the person just one time not every instance the user tried... (4 Replies)
Discussion started by: bouncer
4 Replies

6. Shell Programming and Scripting

Simple Shell Script

Hello Friends, I am writing a shell script which will grab a file if it exists and copies it to another folder and will append with current date. I have written but gives me error, plz help: -------------------------------------------- #!/usr/bin/sh source=/home/dev4rice/naveen/test1... (4 Replies)
Discussion started by: ganesh123
4 Replies

7. Shell Programming and Scripting

Problem with IF - CAT - GREP in simple shell script

Hi all, Here is my requirement I have to search 'ORA' word in out.log file,if it is present then i need to send that file (out.log) content to some mail id.If 'ORA' word is not in that file then i need to send 'load succesful' message to some mail id. The below the shell script is not... (5 Replies)
Discussion started by: mak_boop
5 Replies

8. Shell Programming and Scripting

simple shell - how to get a parameter typed in a shell script

Hi, I am new to unix and using linux 7.2. I would like to create a script that would make it easyer for me to run my java programms. At the moment I have to type java myJavaprogram I am trying to write a script that will allow me to type something like this "myscript myJavaprogram" or maybe... (4 Replies)
Discussion started by: cmitulescu
4 Replies

9. Shell Programming and Scripting

Please Help On Simple Shell Script

This is an assignment I have to do everyone but I was viewing the threads and I assumed that I could write this assignment simpler than what My instructor is directing.. Here is the Assignment This shell script has the following specifications: - It must be named: "printpasswd." - It must... (1 Reply)
Discussion started by: dmosheye
1 Replies

10. Shell Programming and Scripting

A Simple shell Script

Hi there. im creating a shell script which helps me perform simple functions when i use it, but there are still two functions which i cannot figure out yet. 1) how can I count the number of occurences of a certain file within the whole file system? (filename supplied as an argument.) 2) How... (2 Replies)
Discussion started by: provo
2 Replies
Login or Register to Ask a Question