Using the following script it will find all version of a command in $PATH (from Unix Power Tools)
$ cat /tmp/whereiz
#!/bin/sh
testx="test -x"
fixpath="`echo $PATH | sed \
-e 's/^:/.:/' \
-e 's/::/:.:/g' \
-e 's/:$/:./'`"
# echo $fixpath
# IFS has a colon, space and a tab
IFS=": "
for command
do
where=""
for direc in $fixpath
do $testx $direc/$command && where="$where $direc/$command"
done
case "$where" in
?*) echo $where ;;
esac
done
Example
$ /tmp/whereiz ls grep
/bin/ls /tmp/ls
/bin/grep
By setting trap one can run a logout script when a k-shell is being terminated. The built-in command trap in k-shell allows one to customize shell when a signal is received.
# Set to run $HOME/.logout script when shell is terminated
$ trap ‘. $HOME/.logout’ 0
# display list of signals setup
$ trap
trap — ‘. $HOME/.logout’ EXIT
This script will format the new 11G crsctl status resource in a tabular format.
#!/usr/bin/ksh
#
# Sample 11g CRS resource status query script
#
# Description:
# – Returns formatted version of crsctl status resource, in tabular
# format, with the complete rsc names and filtering keywords
# – The argument, $RSC_KEY, is optional and if passed to the script, will
# limit the output to HA resources whose names match $RSC_KEY.
# Requirements:
# – $ORA_CRS_HOME should be set in your environment
#
#
# HISTORY ##################################
#
# 28/06/2010 – Alex Lima – UPgraded to 11G commands.
#
################################################################
export CRS_HOME=/usr/grid/product/11.2.0
RSC_KEY=$1
QSTAT=-u
AWK=/usr/bin/awk # if not available use /usr/xpg4/bin/awk
# Table header:echo “”
echo “”
$AWK \
‘BEGIN {printf “%-30s %-70s %-18s\n”, “HA Resource”, “State”, “Target”;
printf “%-30s %-70s %-18s\n”, “———–”, “—–”, “——”;}’
# Table body:
$CRS_HOME/bin/crsctl status resource | $AWK \
‘BEGIN { FS=”=”; state = 0; }
$1~/NAME/ && $2~/’$RSC_KEY’/ {appname = $2; state=1};
state == 0 {next;}
$1~/STATE/ && state == 2 {appstate = $2; state=3;}
$1~/TARGET/ && state == 1 {apptarget = $2; state=2;}
state == 3 {printf “%-30s %-70s %-18s\n”, appname, appstate, apptarget; state=0;}’
echo “”
Here are some of the options that can be passed to grep:
-l – Print filename if pattern found otherwise it doesn’t print the filename, if found any one file exit status is 0, if not found in any files then 1
-n – Print line # where pattern was found
-i – ignore case when matching string
-v – Display lines where the given pattern doesn’t match
-c – Display total number of occurrences of pattern found, if multiple occurrences on one line it is counted as one
-s – silent displays except error message, if found then exit status is 0 else non-zero
One can use the regular expressions to match in vi, grep, sed and awk.
[a-d] – Match one character with in a-d i.e. a, b, c, d
[^a-d] – Match one character not in the range a-d
\<test\> – Match whole word test
test\> – Match words that ends with test
\<test\> \1ing – Match following text “test testing”, \1 maps to first tag i.e \(\)
x\{5,\} – Match at least 5 occurrences of x
x\{5,9\} – Match between 5 to 9 times occurrences of x
^test – Looks for test at the beginning of a line
test$ – Looks for test at the end of the line
^test$ – Looks for test on a line by itself
th.t – “.” matches one character i.e. 4 letters has th + any character and ends with t. Example: this, that are valid matches
\. – Look for period, using “\” one can escape metacharacters
Search and replace:
:s/\(square\) and \(fair\)/\2 and \1/ – searches for “square and fair” and replaces it with fair and square
# how to create a array and assign a list of values
set -A fruit apples pears oranges banana
# print the first element in the array
print $fruit[0]
# set a new value to element 1
$fruit[1]=watermelon
In ksh shell – case statement
case variable_name in
pattern1)
statements1
;;
pattern2)
statements2
;;
# default (catch all remaining)
*)
statements3
;;
esac
Example:
case $answer in
yes|Yes|y) # received yes
echo got a positive answer
;;
no|n) # received no
echo got a ‘no’
;;
q*|Q*)
#assume the user wants to quit
exit
;;
*)
echo This is the default clause
;;
esac
In csh shell – case statement
Example:
switch ( $color )
case blue:
echo $color is blue
breaksw
case red:
case purple
echo $color is red or purple
breaksw
default:
echo “Not a valid color”
endsw
# Sample script that shows how ERR signal works, when a command returns a non-zero status it triggers to calls to print the message in double quotes.
trap ‘print “You gave me non-integer. Try again”‘ ERR
typeset -i number
while true
do
print -n “Enter an integer: ”
read number 2> /dev/null
if [ $? -eq 0 ]; then
break
fi
done
# reset trap else the grep command below would also generate the error message
trap – ERR
grep “blahblah” /etc/passwd > /dev/null 2>&1
echo “Number entered: $number
I use this script to clean up all the log and trace file from each host.. You can schedule a job in OEM to go and execute this in all hosts you want.
$cat /etc/oratab
ESRTSP:/oracle/app/EPW/rdbms/v10203_ee_suse10:N
ESRTSS:/oracle/app/EPW/rdbms/v10203_ee_suse10:N
RESTST:/oracle/app/EPW/rdbms/v10203_ee_suse10:N
LISTENER_10g:/oracle/app/EPW/rdbms/v10203_ee_suse10:N
AGENT_10g:/oracle/app/EPW/agent/v10203_ee_suse10:N
#!/bin/ksh
export PATH=/usr/bin:$PATH
export USERNAME=`whoami`
export HOSTNAME=`hostname`
#
# Clean up Oracle Export logs
#
find /work/dba/logs -name ‘*${HOSTNAME}_*’ -mtime +7 -exec rm {} \;
find /tmp -name ‘*${HOSTNAME}_*’ -mtime +7 -exec rm {} \;
find /work/dba/prod/backups -name ‘tmp_ora_${HOSTNAME}_*’ -mtime +3 -exec rm -r {} \;
###############################################
###############################################
## ADD ALL INSTANCES TO THE FOR LOOK LIST ##
###############################################
###############################################
for SID in `cat /etc/oratab|egrep ‘:N|:Y’|grep -v \*|cut -f1 -d’:'|egrep -v ‘LISTENER|AGENT’`
do
find /oracle/EPW/${SID}/admin/udump -name ‘*.trc’ -mtime +13 -exec rm {} \;
find /oracle/EPW/${SID}/admin/cdump -name ‘core*’ -mtime +13 -exec rm -r {} \;
find /oracle/EPW/${SID}/admin/bdump -name ‘*.trc’ -mtime +13 -exec rm {} \;
find /oracle/EPW/${SID}/admin/adump -name ‘*.aud’ -mtime +13 -exec rm {} \;
done

#!/usr/bin/ksh
##################################################################################################################
#
# This script is to delete the arch logs for the standby database after it has applied the logs to the instance.
#
##################################################################################################################
script=`basename $0`
export ORACLE_SID=$1
dir=/oracle/EPW/${ORACLE_SID}/temp/arch
tmpf=$dir/.$script.tmp
logcount=50
function GetAppliedLogfileSequenceNumber
{
sqlplus -S /nolog <<EOF > $tmpf
connect / as sysdba
set head off
set pages 0
select max(sequence#) from v\$archived_log where applied = ‘YES’;
select resetlogs_id from v\$database_incarnation where status = ‘CURRENT’;
exit
EOF
return
}
if [ -d $dir ]
then
cd $dir
GetAppliedLogfileSequenceNumber
if [ -s $tmpf ]
then
count=`cat $tmpf | awk ‘{print $1}’ | sed -n ’1p’;`
db_incarnation=`cat $tmpf | awk ‘{print $1}’ | sed -n ’3p’;`
if [ ${#count} -ne 0 ]
then
let count=$count-$logcount
if ((count <= 0))
then
echo “$script: log count is set to (non)zero no log(s) to remove”
exit 0
fi
else
exit 0
fi
else
echo “$script: no archive log(s) to remove”
exit 0
fi
while [ -f ${ORACLE_SID}_${count}_1_${db_incarnation}.arc ]
do
rm -f ${ORACLE_SID}_${count}_1_${db_incarnation}.arc
#ls ${ORACLE_SID}_${count}_1_${db_incarnation}.arc
rcode=$?
if ((rcode != 0))
then
echo “$script: cannot remove: ${ORACLE_SID}_${count}_1_${db_incarnation}.arc”
exit 1
else
let count=$count-1
fi
done
rm -f $tmpf
else
print “$script: $dir no such file or directory”
exit 1
fi
exit 0