Simple Shell Script to Grep

Hi guys, I have written this script, however the outcome is invalid. It contains grep search that is not needed:

Script:

#!/bin/bash
#this is a test script

FILES=$(ls /home/student/bin/dir1/*)
GREPFUNC=$(grep -E -i "login|Successfully" ORProxyTC`date '+%m%d%Y'`*.txt/ ${FILES})
COUNT=$(grep -E -i "login|Successfully" ORProxyTC`date '+%m%d%Y'`*.txt/ ${FILES} | wc -l)
echo "Search completed and found: ${GREPFUNC}."
#################################################################
echo "This is the total number of files: ${COUNT} ."
##################################################################
##################################################################

Output:

[student@station14 bin]$ script_local
grep: ORProxyTC04212010*.txt/: No such file or directory
grep: /home/student/bin/dir1/text: No such file or directory
grep: 2.txt~: No such file or directory
grep: ORProxyTC04212010*.txt/: No such file or directory
grep: /home/student/bin/dir1/text: No such file or directory
grep: 2.txt~: No such file or directory
Search completed and found: /home/student/bin/dir1/ORProxyTC0421201000008875.txt:pos server login is established
/home/student/bin/dir1/ORProxyTC0421201000008875.txt:connection is made Successfully.
This is the total number of files: 2 .
[student@station14 bin]$ 
Test 2211

What are you trying to do? - not how you decided to do it.
It looks a little like part of some kind of adduser or install verification script.

You can try this..:wink:

  
 
# cat search.sh
 
#!/bin/bash
LDIR="/home/student/bin/dir1"
cd $LDIR
ls -1 $FILES > FILESLIST
COUNT=0
c=0
 
for i in $( (ls -1 ORProxyTC`date '+%m%d%Y'`*.txt) )
do
FILES[c]="$i"
while read
do
GREPFUNC=$(grep -i -E -H "login|Successfully" ${FILES[c]})
if [ $? -eq 0 ] ; then
((++c))
((++COUNT))
echo ""
echo "Search completed and found: ${GREPFUNC}."
echo "#################################################################"
fi
done < FILESLIST
done
 
echo ""
echo "This is the total number of files: ${COUNT}."
echo "#################################################################"
 
 # ./search.sh
Search completed and found: ORProxyTC03282010101.txt:login|Successfully.
#################################################################
Search completed and found: ORProxyTC03282010.txt:login|Successfully.
#################################################################
This is the total number of files: 2.
#################################################################

Ygemici, thanks. One question: can multiple file locations be added to LDIR?

Jim: trying to grep perticular strings from a newly generated current file from various locations. Then displaying the result. And then email the result.

#!/bin/bash
 
a=0
c=0
COUNT=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=$(grep -i -E -H "login|Successfully" ${FILES[c]})
                                if [ $? -eq 0 ] ; then
                                    ((++COUNT))
                                    echo ""
                                    echo "Search completed and found: ${GREPFUNC}."
                                    echo "#################################################################"
                                fi
                                    ((++c))
                       done
                        echo ""
               done
                        echo "This is the total number of files: ${COUNT}."
                        echo "#################################################################"
[root@sistem1lnx bin]# ./search
Search completed and found: /home/student/bin/dir1/ORProxyTC0422201011.txt:login|Successfully.
#################################################################
Search completed and found: /home/student/bin/dir1/ORProxyTC04222010.txt:login|Successfully.
#################################################################
 
Search completed and found: /home/student/bin/dir2/ORProxyTC0422201011.txt:login|Successfully.
#################################################################
Search completed and found: /home/student/bin/dir2/ORProxyTC04222010193.txt:login|Successfully.
#################################################################
 
Search completed and found: /home/student/bin/dir3/ORProxyTC0422201012.txt:login|Successfully.
#################################################################
 
Search completed and found: /home/student/bin/dir4/ORProxyTC0422201011.txt:login|Successfully.
#################################################################
Search completed and found: /home/student/bin/dir4/ORProxyTC042220101999.txt:login|Successfully.
#################################################################
Search completed and found: /home/student/bin/dir4/ORProxyTC04222010.txt:login|Successfully.
#################################################################
This is the total number of files: 8.
#################################################################

Thanks ygemici!

I have a question, how would i make sure that the results come as indexed like this:

/student/dir1: 
Search completed and found: /home/student/bin/dir1/ORProxyTC0422201011.txt:login|Successfully.
#################################################################
Search completed and found: /home/student/bin/dir1/ORProxyTC042220101999.txt:login|Successfully.
This is the total number of files: 2
#################################################################

and then

/student/dir2: 
Search completed and found: /home/student/bin/dir2/ORProxyTC0422201011.txt:login|Successfully.
#################################################################
Search completed and found: /home/student/bin/dir2/ORProxyTC042220101999.txt:login|Successfully.
This is the total number of files: 2
#################################################################

....?

 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="$DIROK"
 
 if [ $CHKCNT -eq 0 ] ; then
 echo "$DIROK"
 else
     if [ "${DIRTMP}" != "${DIRTMP[b-1]}" ] ; then
  echo "${DIRTMP}"
     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
 
./s1
/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/dir3
Search completed and found: /home/student/bin/dir3/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.
#################################################################
 

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:

 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="$DIROK"
 
 if [ $CHKCNT -eq 0 ] ; then
 echo "$DIROK"
 else
     if [ "${DIRTMP}" != "${DIRTMP[b-1]}" ] ; then
  echo "${DIRTMP}"
     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!

Hmm Okey just add o few controls :wink:

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="$DIROK"
 
if [ $CHKCNT -eq 0 ] ; then
echo "$DIROK"
else
if [ "${DIRTMP}" != "${DIRTMP[b-1]}" ] ; then
echo "${DIRTMP}"
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

 
ls -l /home/student/bin/dir3
total 0
-rw-r--r-- 1 root root 0 Mar 28 23:12 ORProxyTC03282.txt
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

 
[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/
 
 
[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.
#################################################################
 

Thanks again ygemici :slight_smile:

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:

!/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 :b:
Did you test?

Hi ygemici. Yes I wrote the below code and this is the problem I am having:

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="$DIROK"
 
if [ $CHKCNT -eq 0 ] ; then
echo "$DIROK"
else
if [ "${DIRTMP}" != "${DIRTMP[b-1]}" ] ; then
echo "${DIRTMP}"
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?

:slight_smile:

Which state is Good or Bad?

When the result comes with "ERROR:" it is bad.

When there is no ERROR, it is good.

:slight_smile:

#!/bin/bash
a=0
b=1
c=0
COUNT=0
CHKCNT=0
forproblemmail=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
     forproblemmail=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
       forproblemmail=1
    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
     DIRSNEW=( ${DIRS
[*]} )
     DIRS=("")
     DIRS=$( (echo ${DIRSNEW
[*]#$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="$DIROK"
 
                      if [ $CHKCNT -eq 0 ] ; then
                           echo "$DIROK"
                             else
                                  if [ "${DIRTMP}" != "${DIRTMP[b-1]}" ] ; then
                                        echo "${DIRTMP}"
                                  fi
                      fi
          ((++CHKCNT))
          ((++b))
 
       done
 
           a=0   
           COUNTOK=$COUNT
 
        while [ $(( COUNT -= 1 )) -gt 0 ]
             do
                 echo "Search completed and found: ${GREPFUNCOK[a]}." >/tmp/emailmessage.txt
                 echo   "#################################################################" >>/tmp/emailmessage.txt
                 ((++a))
             done
 
         echo "Search completed and found: ${GREPFUNCOK[a]}." >>/tmp/emailmessage.txt
         echo "This is the total number of files: ${COUNTOK}." >>/tmp/emailmessage.txt
          echo "#################################################################" >>/tmp/emailmessage.txt
 
           COUNTOK=0
           COUNT=0
           c=0
           a=0
          echo "" >>/tmp/emailmessage.txt
  done
 
#Mail Process
if [ $forproblemmail - eq 1 ] ; then
       SUBJECT="ERROR: Problem During Find Files"
         else
              SUBJECT="NO ERROR: Complete Find Files With Success"
fi
# Email To ?
EMAIL="admin@somewhere.com"
# Email text/message
EMAILMESSAGE="/tmp/emailmessage.txt"
# send an email using /bin/mail
mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE