See How to script in Oracle See Command list
Prevent this script from running if another instance is already running
#
# Prevent this script from running if another instance is already running
#
scriptName=`basename $0`
if [ "`echo $0 | cut -c1`" = "/" ]; then
scriptPath=`dirname $0`
else
scriptPath=`pwd`/`echo $0 | sed -e s/$scriptName//`
fi
running=`ps -ef | grep $scriptName | wc -l`
if [ $running -gt 2 ]; then
echo "An instance of $scriptName is already running"
exit 0
fi |
Input/Output Redirections < Redirect STDIN from a file > Redirect STDOUT to a file overwriting >> Redirect STDOUT to a file appending 2> Redirect STDERR to a file overwriting &> Redirect STDERR and STDOUT 2>&1 Redirect STDERR and STDOUT |
Variable operations var=$((7*3)) #increase 1 var=$((var+1)) Number operations a=5.55 b=7.3 echo $a + $b ---- 5.55 + 7.3 echo $a + $b | bc ---- 12.85 c=`echo $a + $b | bc` echo $c ---- 12.85 |
Unix Test conditions
(man test, for more info)
if [ $running -ge 2 ]; then
echo "Tutto ok"
else
echo "da lanciare"
fi
-eq True if the integers n1 and n2 are algebraically equal.
-ne True if the integers n1 and n2 are not algebraically equal.
-gt True if the integer n1 is algebraically greater than the integer n2.
-ge True if the integer n1 is algebraically greater than or equal to the integer n2.
-lt True if the integer n1 is algebraically less than the integer n2.
-le True if the integer n1 is algebraically less than or equal to the integer n2.
Backslash Escaped Characters
See echo in Command list
\c Suppressing trailing newline
\v Vertical Tab
\r Carriage return
\n Newline |
For loop classic for (( i=0; i<4; i++ )) do echo $i done ---- 0 1 2 3 For loop seq for i in $(seq 3) do echo $i done ---- 1 2 3 |
While loop classic read X echo "OUT:" while [ $X -le 5 ] do echo $X X=$((X+1)) done ----- 3 (<- input) OUT: 3 4 5 |
Read user input & "case" loop read a case $a in 1 ) echo "one" ;; 2 ) echo "two" ;; * ) echo "any other" ;; esac exit 0 |
Create a file whose size is based on file system %
filename=tmp
perc=85
folder=/myfolder
mbytes=0
kbytes=`df -k | grep ${folder} | awk '{print $2}'`
used=`df -k | grep ${folder} | awk '{print $3}'`
#for ksh
#let "mbytes = 1024 * ($perc * $kbytes / 100 - $used)"
mbytes=`expr $perc \* $kbytes / 100 - $used`
mbytes=`expr $mbytes \* 1024`
mkfile $mbytes $filename
exit 0 |
Create a date based folder
#create dir
dateDir=$1/`date +%Y%m%d`
echo $dateDir
if [ -e $dateDir ]; then
echo Dest dir alredy exists
else
echo Creating dest dir
mkdir $dateDir
fi
|
Check existance of a remote directory
remoteLogin=batch_ch@lmelb003.lmuk.local
remoteDir=/i02-ch/batch_ch/DATA/DWHDATA/`date +%Y%m%d`
#if source dir not exists then exit
srcEx=`ssh ${remoteLogin} ls -l ${remoteDir} | wc -l`
if [ $srcEx -eq 0 ]; then
echo Source directory does not exists
exit 0;
fi |
Delete files from ls
#40 older files
ls -lt | tail -40 | while read a b c d e f g h nome
#Files for a date
ls -l | grep "Jun 09" | while read a b c d e f g h nome
do
echo "deleting "$nome
rm -rf $nome
done
Delete files older than 3 days
find . -mtime +3 -exec rm -f {} \;
|
Kill processes
#Substitute the green part
for pid in `ps -ef|grep oracle|grep racg|grep -v grep |awk '{print $2}'`;do kill -9 $pid; done |
Sort by folder size in current dir
du -k | while read size nome
do
if [ $size -ge 22596 ]; then
echo $size $nome
echo $size $nome >> /arch_oem/out.txt
fi
done
sort -n out.txt |
Replace a string recursively over many files I want to replace the string "dba.support@fastweb.it" with "fwictopdba.supportlog@fastweb.it" over many files recursively in all subdirectories 1) Backup files first: /usr/bin/find . -name *.ksh | /usr/bin/xargs /usr/bin/grep -i "dba.support@fastweb.it"| cut -f1 -d: | while read file do cp $file /tmp/ echo $file done 2) (Solaris)perform replace: /usr/bin/find . -name *.ksh | /usr/bin/xargs /usr/bin/grep -i "dba.support@fastweb.it"| cut -f1 -d: | while read file; do vi $file >/dev/null 2>&1 << ! #case sensitive :1,%s/dba.support@fastweb.it/fwictopdba.supportlog@fastweb.it/g :wq ! done 2) (Linux) /usr/bin/find pum*.ksh | /usr/bin/xargs /usr/bin/grep -i "runsqlplus_with_logging"| cut -f1 -d: | while read file; do vi $file >/dev/null 2>&1 << ! #case sensitive :1,%s/runsqlplus_with_logging/runsubmit/g :wq ! done |
Wich files contains a string?
/usr/bin/find . -name *.sql | /usr/bin/xargs /usr/bin/grep -i "alter user" | while read file;
do
echo $file
done |
Log all variables passed to system log #!/bin/ksh logger "ONS:" $* |
Easy(and brutal) way to synchronize two system dates Set up root user equivalence, see ssh - Automatic Connection [setDate.sh] #!/bin/ksh d=`ssh node1 "date"` date -s "$d" [crontab] * * * * * /root/setDate.sh > /dev/null 2>&1 |