Sunteți pe pagina 1din 52

* Solutions. Powered by people.

February 8, 2001
Scripting in the UNIX Environment
to Support Oracle
Bill Ducat (888) 775-3778 x227
wducat@ambassadorsolutions.com
* Solutions. Powered by people.
BUILDING BLOCKS

STDOUT
STDERR
STDIN
Environment Variables
Scripts
* Solutions. Powered by people.
STDOUT
Most commands send output to
the screen
You can redirect output using the
> and >> redirectors
The tee command can be used
to send results to the screen as
well as a file
* Solutions. Powered by people.
STDERR
STDERR is the destination of
error information
Most commands redirect
STDERR to the screen
It can be redirected to a file using
2><filename>
It can be redirected to the same
place as STDOUT using 2>&1
* Solutions. Powered by people.
PUTTING THEM TOGETHER

ls myfile* >results.txt 2>errors.txt
or
ls myfile* >results.txt 2>&1
* Solutions. Powered by people.
STDIN
STDIN is the source of input for a
command
It usually comes from the
keyboard or from the results of
another command
The pipe | is used to set the
results of one command as the
input to another command
* Solutions. Powered by people.
STDIN - PIPING RESULTS
sort myfile
Does the same thing as
cat myfile |sort

* Solutions. Powered by people.
STDIN - PIPING RESULTS (cont.)
Grep ORA- myfile|sed e
s/^*//;s/^/ /|sort
Looks for ORA- in myfile
For each line found, it replaces
any number of spaces at the
beginning of the line with 3
spaces
Sorts the results
* Solutions. Powered by people.
STDIN Keyboard Simulation
sqlplus scott/tiger s <<!!
select * from global_name;
quit;
!!
Nothing executes until the final
return is pressed
Nothing can follow the !! On a
line
* Solutions. Powered by people.
Environment Variables
The env command shows all
environment variables that have
been exported
Variables are referenced by
placing a $ in front of the
variable name
{} should enclose every
reference to a variable
* Solutions. Powered by people.
Environment Variables (cont.)
echo $ORACLE_SID
Produces the same result as
echo ${ORACLE_SID}
* Solutions. Powered by people.
Environment Variables (cont.)
echo $ORACLE_SID_world
Does not do the same thing as
echo ${ORACLE_SID}_world
* Solutions. Powered by people.
Environment Variables (cont.)
SYSTEM_CONNECT=system/manager
Sqlplus ${SYSTEM_CONNECT}
* Solutions. Powered by people.
*** WARNINGS ***
The export command can be used to
increase the scope of environment
variables.
Exported environment variables can
be seen by other users using the
ps aew command.
Do not place passwords in exported
environment variables!
* Solutions. Powered by people.
Scripts
Simple scripts are nothing more than
a series of commands
There are various shells, but we will
use the KORN shell
Use vi, or some lesser editor , to
create scripts
Use the chmod command to add
execute permissions to the file
chmod +x myfile.ksh
* Solutions. Powered by people.
Scripts (cont.)
Simple script:
date
pwd
whoami
* Solutions. Powered by people.
Scripts Parameter Passing
Parameters can be treated like
environment variables within a
script
Special parameter variables can
be used to get information about
parameters

* Solutions. Powered by people.
Scripts Parameter Variables
$1 The first parameter
$2 The second parameter
$0 The command without
parameters
$# - The number of parameters
passed in
* Solutions. Powered by people.
Scripts Sample
#! /bin/ksh
echo $1
echo $2
echo $#
echo $0
echo Done
* Solutions. Powered by people.
Scripts Functions
#! /bin/ksh
__doit ()
{
echo $1 $2
}
echo $1 $2
__doit test $1

* Solutions. Powered by people.
Scripts IF Processing
x=5
if ( test ${X} gt 0 ) ;then
echo ${x} is greater than 0
else
echo ${x} is less than 0
fi
* Solutions. Powered by people.
Scripts FOR Processing
for x in a b c ;do
echo ${x}
done

for x in ls ;do
lp ${x}
done
* Solutions. Powered by people.
Scripts Process IDs
The $$ variable will identify
your process ID
This can be used to create
unique file names
ls >my_results_$$.out
* Solutions. Powered by people.
Practical Examples
We will build a script that will
display session information for
all instances on a machine. This
script will demonstrate:
Password Management
Embedded SQL
Embedded Subroutines
Parameter Passing
* Solutions. Powered by people.
PASSWORD FILE
The file setup_passwords.ksh
contains environment variables
defining passwords
The system password is the
same on each instance
The file must be protected from
non DBA users
* Solutions. Powered by people.
SAMPLE PASSWORD FILE
SYSTEM_CONNECT=system/manager
SYS_CONNECT=sys/change_on_install
DUCAT_CONNECT=wducat/cat
* Solutions. Powered by people.
Sample 1
#! /bin/ksh
. /common/bin/setup_passwords.ksh
$ORACLE_HOME/bin/sqlplus -s /nolog <<!!
set line 200
set feedback off
connect ${SYSTEM_CONNECT};
column global_name heading "Instance" format a15
column sessions_max heading "Max" format 9999
column sessions_warning heading "Warning" format 9999
column sessions_current heading "Current" format 9999
column sessions_highwater heading "Highwater" format 9999
select
global_name,sessions_max,sessions_warning,
sessions_current-1 sessions_current,
sessions_highwater
from
v\$license,global_name ;
quit;
!!
* Solutions. Powered by people.
Sample 1 Results

Connected.

Instance Max Warning Current Highwater
--------------- ----- ------- ------- ---------
DEVEL.WORLD 0 0 0 1

* Solutions. Powered by people.
Sample 2
#! /bin/ksh
. /common/bin/setup_passwords.ksh

__show_licenses()
{
$ORACLE_HOME/bin/sqlplus -s /nolog <<!!
set line 200
set feedback off
connect ${SYSTEM_CONNECT};
column global_name heading "Instance" format a15
column sessions_max heading "Max" format 9999
column sessions_warning heading "Warning" format 9999
column sessions_current heading "Current" format 9999
column sessions_highwater heading "Highwater" format 9999

* Solutions. Powered by people.
Sample 2 (Cont.)

select
global_name,sessions_max,sessions_warning,
sessions_current-1 sessions_current,
sessions_highwater
from
v\$license,global_name ;
quit;
!!
}

__show_licenses
* Solutions. Powered by people.
Sample 2 Results
Connected.

Instance Max Warning Current Highwater
--------------- ----- ------- ------- ---------
DEVEL.WORLD 0 0 0 1
* Solutions. Powered by people.
Sample 3
#! /bin/ksh
. /common/bin/setup_passwords.ksh

__show_licenses()
{
$ORACLE_HOME/bin/sqlplus -s /nolog <<!!
set line 200
set feedback off
connect ${SYSTEM_CONNECT};
column global_name heading "Instance" format a15
column sessions_max heading "Max" format 9999
column sessions_warning heading "Warning" format 9999
column sessions_current heading "Current" format 9999
column sessions_highwater heading "Highwater" format 9999

* Solutions. Powered by people.
Sample 3 (Cont.)

select
global_name,sessions_max,sessions_warning,
sessions_current-1 sessions_current,
sessions_highwater
from
v\$license,global_name ;
quit;
!!
}

__show_licenses | grep v ^Connected\.$
* Solutions. Powered by people.
Sample 3 Results

Instance Max Warning Current Highwater
--------------- ----- ------- ------- ---------
DEVEL.WORLD 0 0 0 1
* Solutions. Powered by people.
Sample 4
#! /bin/ksh
. /common/bin/setup_passwords.ksh

__show_licenses()
{
$ORACLE_HOME/bin/sqlplus -s /nolog <<!!
set line 200
set feedback off
connect ${SYSTEM_CONNECT}@${1};
column global_name heading "Instance" format a15
column sessions_max heading "Max" format 9999
column sessions_warning heading "Warning" format 9999
column sessions_current heading "Current" format 9999
column sessions_highwater heading "Highwater" format 9999

* Solutions. Powered by people.
Sample 4 (Cont.)

select
global_name,sessions_max,sessions_warning,
sessions_current-1 sessions_current,
sessions_highwater
from
v\$license,global_name ;
quit;
!!
}

__show_licenses ${1}| grep v ^Connected\.$
* Solutions. Powered by people.
Sample 4 Results
If the first parameter passed was systest:


Instance Max Warning Current Highwater
--------------- ----- ------- ------- ---------
SYSTEST.WORLD 0 0 0 1
* Solutions. Powered by people.
Sample 5
#! /bin/ksh
. /common/bin/setup_passwords.ksh

__show_licenses()
{
$ORACLE_HOME/bin/sqlplus -s /nolog <<!!
set line 200
set feedback off
connect ${SYSTEM_CONNECT}@${1};
column global_name heading "Instance" format a15
column sessions_max heading "Max" format 9999
column sessions_warning heading "Warning" format 9999
column sessions_current heading "Current" format 9999
column sessions_highwater heading "Highwater" format 9999
* Solutions. Powered by people.
Sample 5 (Cont.)

select
global_name,sessions_max,sessions_warning,
sessions_current-1 sessions_current,
sessions_highwater
from
v\$license,global_name ;
quit;
!!
}

if [ $# -ne 1 ] ;then
__show_licenses ${1}| grep v ^Connected\.$
else
echo ERROR: Invalid number of parameters specified
echo One instance name must be specified.
fi

* Solutions. Powered by people.
Sample 5 Results
If the no parameter was passed:


ERROR: Invalid number of parameters specified
One instance name must be specified.
* Solutions. Powered by people.
Sample 6
#! /bin/ksh
. /common/bin/setup_passwords.ksh

__show_licenses()
{
$ORACLE_HOME/bin/sqlplus -s /nolog <<!!
set line 200
set feedback off
connect ${SYSTEM_CONNECT}@${1};
column global_name heading "Instance" format a15
column sessions_max heading "Max" format 9999
column sessions_warning heading "Warning" format 9999
column sessions_current heading "Current" format 9999
column sessions_highwater heading "Highwater" format 9999

* Solutions. Powered by people.
Sample 6 (Cont.)

select
global_name,sessions_max,sessions_warning,
sessions_current-1 sessions_current,
sessions_highwater
from
v\$license,global_name ;
quit;
!!
}

__show_licenses prod| grep v ^Connected\.$
__show_licenses devel| grep v ^Connected\.$
* Solutions. Powered by people.
Sample 6 Results
Instance Max Warning Current Highwater
--------------- ----- ------- ------- ---------
PROD.WORLD 0 0 0 1

Instance Max Warning Current Highwater
--------------- ----- ------- ------- ---------
DEVEL.WORLD 0 0 0 1
* Solutions. Powered by people.
Sample 7
#! /bin/ksh
. /common/bin/setup_passwords.ksh

__show_licenses()
{
$ORACLE_HOME/bin/sqlplus -s /nolog <<!!

set line 200
set feedback off
set heading ${heading}
connect ${SYSTEM_CONNECT}@${1};
column global_name heading "Instance" format a15
column sessions_max heading "Max" format 9999
column sessions_warning heading "Warning" format 9999
column sessions_current heading "Current" format 9999
column sessions_highwater heading "Highwater" format 9999
select
global_name,sessions_max,sessions_warning,
sessions_current-1 sessions_current,
sessions_highwater

* Solutions. Powered by people.
Sample 7 (Cont.)

from
v\$license,global_name ;
quit;
!!
}

heading=on
__show_licenses prod|grep -vE "^Connected\.$|^$"
heading=off
__show_licenses devel|grep -vE "^Connected\.$|^$"
__show_licenses systest|grep -vE "^Connected\.$|^$"

* Solutions. Powered by people.
Sample 7 Results
Instance Max Warning Current Highwater
--------------- ----- ------- ------- ---------
PROD.WORLD 0 0 0 1
DEVEL.WORLD 0 0 0 1
SYSTEST.WORLD 0 0 0 1

* Solutions. Powered by people.
Sample 8
%ps -aef |grep ora_pmon_
oracle 863 1 0 09:11 ? 00:00:00 ora_pmon_devel
oracle 888 1 0 09:11 ? 00:00:00 ora_pmon_prod
oracle 913 1 0 09:11 ? 00:00:00 ora_pmon_systest
wducat 2584 2582 0 16:28 tty1 00:00:00 grep ora_pmon_
%ps -aef |grep ora_pmon_ |grep -v grep
oracle 863 1 0 09:11 ? 00:00:00 ora_pmon_devel
oracle 888 1 0 09:11 ? 00:00:00 ora_pmon_prod
oracle 913 1 0 09:11 ? 00:00:00 ora_pmon_systest
%ps -aef | grep ora_pmon_ | grep -vE 'sed|grep'| \
sed -e 's/^.*ora_pmon_//'
devel
prod
systest
%

* Solutions. Powered by people.
Sample 9
1 #! /bin/ksh
2
3 gen_sid_list()
4 {
5
6 sid_list=""
7 touch /tmp/sids_$$.out
8 ps -aef | \
9 grep ora_pmon | \
10 grep -vE 'sed|grep'| \
11 sed -e "s/^.*ora_pmon_//" \
12 >/tmp/sids_$$.out
13



* Solutions. Powered by people.
Sample 9 (Cont.)
14 for iName in `sort /tmp/sids_$$.out` ;do
15 tnsping ${iName} >/tmp/ping_$$.out
16 eCount=`grep TNS- /tmp/ping_$$.out|wc -l`
17 if [ $eCount -eq 0 ] ;then
18 sid_list="${sid_list} ${iName}"
19 else
20 echo ERROR: Unable to connect to ${iName}
21 fi
22 rm /tmp/ping_$$.out
23 done
24 rm /tmp/sids_$$.out
25 }



* Solutions. Powered by people.
Sample 10
#! /bin/ksh
. /common/bin/setup_passwords.ksh
. /common/bin/functions.ksh

__show_licenses()
{
$ORACLE_HOME/bin/sqlplus -s /nolog <<!!
set line 200
set feedback off
set heading ${heading}
connect ${SYSTEM_CONNECT}@${1};
column global_name heading "Instance" format a15
column sessions_max heading "Max" format 9999
column sessions_warning heading "Warning" format 9999
column sessions_current heading "Current" format 9999
column sessions_highwater heading "Highwater" format 9999

* Solutions. Powered by people.
Sample 10 (Cont.)
select
global_name,sessions_max,sessions_warning,
sessions_current-1 sessions_current,
sessions_highwater
from
v\$license,global_name ;
quit;
!!
}

gen_sid_list
heading=on
for iName in $sid_list ;do
__show_licenses ${iName}|grep -vE "^Connected\.$|^$"
heading=off
done
* Solutions. Powered by people.
Sample 10 Results
Instance Max Warning Current Highwater
--------------- ----- ------- ------- ---------
PROD.WORLD 0 0 0 1
DEVEL.WORLD 0 0 0 1
SYSTEST.WORLD 0 0 0 1

S-ar putea să vă placă și