[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

Unix Script

Hi All

 I’m back again with the same script on the use of the command find in a Solaris
Environment.  This time I have a problem with the regex statement specifically this line

GREP_EXCLUDE_DIRs="${GREP_EXCLUDE_DIRs}tcb$|dev$|devices$|proc$|vol$|xfn$|cdrom$|mnt$|fd$|kernel$|opt1$|opt2$|lost+found$"

On the surface it looks good and works. But it is flawed  and this is the problem
I’m creating a list of dir. That are to be exclude from the find.  What actually happens is that any dir that ends or contains for example
The word  dev  also gets exclude.

I want to exlude /dev   and not  /totodev .  I include the complete script
#######################################################################
LC_ALL="C"; export LC_ALL
umask 066
#######################################################################
trap 'rm -f "${Tmp0}" exit 1' 1 2 3 15
rm -f "${Tmp0}"
#######################################################################
if test -x /bin/nawk
then
  AWk="/bin/nawk"
else
  AWk="awk"
fi
#######################################################################
#
# Get the required input
#
# Define directories that need to be fExcluded (multiple folder must be
# separated by commas) or the entire filesystem will be scanned.
# Default value for fExcludeS is N (no dir to be fExcluded).
#
# # 1: Function: parse_args ()
#
parse_args ()
{
  if [ $# -eq 1  ]
  then
    USER_DIRs=`echo $1 | sed 's/,/ /g'`
    GREP_EXCLUDE_DIRs=`echo "^$1" | sed 's/,/\\|^/g'`
"unownde.sh" 127 lines, 4176 characters
# Criteria:  must be specified by the user as a parameter.
# Action:    Don't display the file
#
exclude_find_dirs()
{
  for USER_DIR in $USER_DIRs
  do
    if [ -n "$fExclude" ]
    then
      fExclude="$fExclude -o"
    fi
        SEARCH_PATH=`echo $USER_DIR | sed 's/\// /g' | awk '{print $NF}'`
        fExclude="${fExclude} -name $SEARCH_PATH -prune"
 echo $SEARCH_PATH
  done

  if [ -n "$fExclude" ]
  then
    fExclude="( $fExclude )"
  fi
}
######################################################################
# Make call - Function #1, #2
#
parse_args $@
exclude_find_dirs

######################################################################
# Rule:      fUnowned
# Search:    Search for any object that has an owner whose UID is not
#            registered in /etc/passwd.  Search for any object that
#            has a group-owner whose GID is not registered in /etc/group.
# Criteria:  * must either have no valid owner or no valid group
# Action:    Display directory listing
#
fUnowned='( -nouser -o -nogroup )'

######################################################################
#
# Output unowned files and directories.
######################################################################
GREP_EXCLUDE_DIRs="${GREP_EXCLUDE_DIRs}tcb$|dev$|devices$|proc$|vol$|xfn$|cdrom$|mnt$|fd$|kernel$|opt1$|opt2$|lost+found$"
DIRs=`ls -1ALl / | grep "^d" | egrep -v "$GREP_EXCLUDE_DIRs" | awk '{print "/"$9}'`

if [ -n "${fExclude}" ]
then
        fExclude=`echo "${fExclude} -o"`
fi

 for i in `find / $DIRs $fUnowned -mount -local`; do dirname $i; done | sort | uniq -c | awk '{print $2","$1}''{SUM +=$1}END  {print " Total Count ="  SUM}'

#######################################################################
# Exit.
exit 0
#######################################################################
# End of unowned.sh

0
d_asselin
Asked:
d_asselin
1 Solution
 
sentnerCommented:
You can simply your regex statement to:  

 GREP_EXCLUDE_DIRs="$GREP_EXCLUDE_DIRs|tcb|dev|devices|proc|vol|xfn|cdrom|mnt|fd|kernel|opt1|opt2|lost+found"

And then, when you actually use it, change the line to:

DIRs=` /bin/ls -1ALl / | grep "^d" | awk '{print "/"$9}'| egrep -v "^/($GREP_EXCLUDE_DIRs)$"`

That will only exclude those specific directories (and not subdirs) as listed in the variable.
0
 
d_asselinAuthor Commented:
Perfect  can’t ask for more many thanks

Dan
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now