Bash Samples

 

* See this post for more examples.

Timestamp

ts=`date +%m%d%Y`
ts2=`date +%H%M%S_%m%d%Y`

Find with Permission Denied Filtered Out

find . -name "something" -print 2>/dev/null > find.something.file

Find String in Type of Files

#!/bin/sh
if [ $# -lt 2 ]; then
  echo "Usage: findstr string_to_find type_of_file"
  exit 1
fi
 
str2find=$1
type2find=$2
dir2find="."
if [ $3 ];then
  dir2find=$3
fi
 
echo "Finding string \"${str2find}\" in files ending with \"${type2find}\" in \"${dir2find}\" directory..."
for f in `find ${dir2find} -name "*.${type2find}"`
do
 if [ ${type2find} = "jar" -o ${type2find} = "war" -o ${type2find} = "zip" ]; then
   jar tvf $f | grep -i ${str2find}
   if [ $? = '0' ]; then
     echo "Found ${str2find} in $f file."
   fi
 else
  grep -i ${str2find} $f
  if [ $? = '0' ]
  then
    echo "Found ${str2find} in $f file."
  fi
 fi
done
echo "done."

* Example:

]$ ./findstr test jar
Finding string "test" in files ending with "jar" in "." directory...
    12 Wed Jun 04 12:01:10 EDT 2014 test.txt
Found test in ./test.jar file.
done.

Find a particular class file in a directory of jar files

#!/bin/sh
findclass='ClassFileName'
for f in `ls`
do
  jar tvf $f | grep ${findclass}
  if [ $? = '0' ]
  then
    echo "Found ${findclass} in $f file."
  fi
done

Find a particular class file in all jar files

#!/bin/sh
 
if [ $# -lt 2 ]
then
  echo "Usage: findclass fileNameToFind dirToStartSearch"
  exit 1
fi
 
findclass=$1
finddir=$2
for f in `find ${finddir} -name "*.jar"`
do
jar tvf $f | grep ${findclass}
if [ $? = '0' ]
then
echo "Found ${findclass} in $f file."
fi
done

* Usage example:

findclass 'MyClassName' '/app'

Use getopts

#--------------------------------------------
# Get options
#--------------------------------------------
while getopts ":a:b:r:" opt
do
  case $opt in
    a)
      alpha=$OPTARG
      ;;
    b)
      beta=$OPTARG
      ;;
    r)
      gamma=$OPTARG
      ;;
    \?)
      echo "Invalid option: -$OPTARG"
      ;;
    :)
      echo "Option -$OPTARG requires an argument."
      ;;
  esac
done

grep Examples

# grep one of multiple strings
grep "string1\|string2\|string3"
 
# Inverse grep
grep -v "notThisString"

Remove Old Log Files

# List log files older than 30 days
find . -type f -mtime +30 -name "*.log" -exec ls -altr {} \;
 
# Remove log files older than 30 days
find . -type f -mtime +30 -name "*.log" -exec rm {} \;
 
# Remove log files older than 3 days
find . -type f -mtime +3 -name "*.log" -exec rm {} \;
 
# Find log files less than one day old
find . -type f -mtime -1 -name "*.log" -exec ls -l {} \;

Some Debug Methods

# --------------------------------------------------------- Constants
# Turn debug on/off
DEBUG=1
 
# Turn dryrun on/off
DRYRUN=1
 
# Turn logfile on/off
USELOGFILE=0
 
path=/home/me
 
# --------------------------------------------------------- Debug methods
echo_debug() {
	if [ $DEBUG -eq 1 ]
	then
		if [ ! $2 ]
		then
			echo "" # Add a new line
			echo "---> $1"
		else
			echo "         $1"
		fi
 
	fi
}
 
dry_run() {
	if [ $DRYRUN -eq 1 ]
	then
		echo "Dry run: ${@}"
		return 0;
	else
		eval "${@}"
	fi
}
 
use_logfile() {
	if [ $USELOGFILE -eq 1 ]
	then
		# Redirect all outputs to a log file
		exec >> $SCRIPT_DIR/`basename $0`.log 2>&1
	fi
}
This entry was posted in shell. Bookmark the permalink.