16666060-Unix-for-DBA | Computing | Technology

Oracle Center of Excellence

Unix For DBA’s

Oracle CoE TCS - GDC , SEEPZ

Oracle Center of Excellence Variables we need in .profile
PATH Variable : • set in .profile • Sequence of directories that the shell will search to loo for a co!!and • The O"#C$E%&O'E !ust (e added to P#T& ORACL !"#D )aria(le * 'ust (e set in the shell, can (e set in .profile or at co!!and pro!pt.+dentifies the oracle instance ,ou are currentl, wor in- on. $echo ORACLE_SID This variable will identify the Oracle instance you are currently wor in! in"

Oracle Center of Excellence Perfor$ "ear%&es in Unix:
'rep * Searches for a pattern in a file • -rep -i .pattern/ filena!e

find * to search for files on the files,ste! find 0 -na!e #filena$e% -print

(oni)orin' Ba%*'ro+nd Pro%esses r+nnin' on Unix
ps * Displa,s the processes on the s,ste! • ps -ef • ps -ef 1 -rep ora * Shows the oracle (ac -round processes runnin-. Can (e used to deter!ine whether an instance is runnin- or not.

Oracle Center of Excellence "%&ed+lin' ,obs in Unix
a) * run a particular 2o(0co!!and at a particular ti!e at &'()* test"sh at +l --3 shows the list of 2o(s su(!itted (, current user %ron * +t is a dae!on runnin- continuousl, and after e)er, !inute chec s into 0usr0spool0cron0cronta(s for su(!itted 2o(s # user can also place a file na!ed as his user-id in this director,. Each cronta( file has a list of co!!ands alon- with the schedule of e4ecution 55-65 67 8 9,:,;,6< 6 find 0 -n .last%ti!e -print 3 (ac uplist The fi)e fields separated (, (lan s represent date and ti!e.

Oracle Center of Excellence Cron -Con)d..
• • • • • =irst =ield specifies the no. of !inutes after the hour when the co!!and is to (e e4ecuted >le-al )alues 55 to ?;@ Second filed specifies the hour in <A hour for!at >6 to <A@ Third field is da, of the !onth >6 to 96@ .The 8 on this field !eans it is to (e e4ecuted dail, =ourth field specifies the !onth >6-6<@ =ifth field is the da, of the wee >5-: Sunda, (ein- 5@

# user can add a co!!and to his file (, cronta( #co$$ands%

Oracle Center of Excellence R+nnin' a pro%ess in )&e ba%*'ro+nd
sort -o emp.lst emp.lst & - runs the sort as a (ac -round process no&+p * will continue runnin- the (ac -round process e)en if the user lo-s out nohu, sort e$,"lst -

Oracle Center of Excellence (oni)orin' Dis* +sa'e
df * reports free space a)aila(le on dis df + ( will show all the $ount ,oints and their si.es in /ilo 0ytes d+ * reports the dis usa-e %o$press * co!presses file and -enerates the filena!e.Z co$,ress filena$e ( !ive a co$,ressed file as filena$e"1

Oracle Center of Excellence Ba%*in' +p opera)in' s/s)e$ files:
dd * Bsed !ainl, for cop,in- !edia li e flopp, and tape dd ifC0de)0rds 0f5q6Dt ofCte!p (sC6A7A?: if23in,ut filena$e4 of23out,ut filena$e4 bs23bloc si.e4 %pio * used to cop, files to and fro! a (ac up de)ice ls 5 c,io +ov %6dev6r$t6)$ ls !enerates the list of files 3find too can be used for this4 +o creates an archive which is to be redirected to the bac u, device c,io +iv # 6dev6r$t6)$ restores the files fro$ bac u, device

Oracle Center of Excellence Ba%*in' +p opera)in' s/s)e$ files -%on)d..:
)ar * creates archi)es on tapes and floppies. Older cousin of cpio tar cvf 6dev6r$t6)$ 6Oracle6dbs6initTest"ora tar cvf b 6dev6r$t6)$ &'') &* inde7"s8l ( $ulti+volu$e bac u, tar 7vf 6dev6r$t6)$ tar uvf 6dev6r$t6)$ inde7"s8l

Oracle Center of Excellence
0&a) is (o+n)in' 1
'ountin- is the process of attachin- a files,ste! to the root files,ste!. The point at which a director,0files,ste! is attached is called the $ount ,oint" 9ount 6dev6charlie 6oracle ($ounts the :6dev6charlie; at $ount ,oint :6oracle;" 9ount +f <S= lower 6dev6cd) 6cdro$ +f is used to s,ecify the ty,e of filesyste$ to be $ounted <S is the ty,e of CD+RO9 for$at lower eyword ensures lowercase files are dis,layed in lowercase only +r o,tion if used will $ount the filesyste$ in read+only $ode B!ount can (e used to dis!ountin- the file s,ste!, either the files,ste! or the !ount point is the ar-u!ent. Dis!ountin-0un!ountin- !a es the files,ste! inaccessi(le. u$ount 6oracle ( $ount ,oint u$ount 6dev6charlie ( device na$e of filesyste$

Oracle Center of Excellence
How do # %&an'e )&e a%%ess ri'&)s )o a file1
Ch!od is used to set the per!issions for all three cate-ories of users >owner, -roup, others@ ch$od u>7 initTEST"ora ( adds3>4 e7ecutable374 ,er$ission on initTEST"ora to owner3u4 ch$od cate!ory o,eration ,er$ission filena$e3s4 cate!ory ( u3owner4= !3!rou,4= o3others4= a3all= is the default4 ,er$ission ( r3read4= w3write4= 73e7ecutable4 ch$od ?@' initTEST"ora ( !ives rw73?4 to owner= rw3@4 to !rou,= r3'4 to others r3read4 2 '= w3write4 2 A= 73e7ecutable4 2 &

Oracle Center of Excellence
How do # %&an'e )&e a%%ess ri'&)s )o a file1 -%on)d..
Chown is used to chan!e the ownershi, of a file" Chown oracle initTEST"ora ( now oracle is the owner of the file Ch!r, chan!es the !rou, ownershi, of the file ch!r, dba initTEST"ora ( the !rou, dba has the !rou, ownershi, of the file= which $eans all the users belon!in! to the !rou, dba will have sa$e ri!hts to the file as the !rou, ,er$issions"

Oracle Center of Excellence
Can # lo' in as a differen) +ser wi)&o+) dis%onne%)in' )&e %+rren) session1 su -u oracle * will atte!pt to lo- ,ou in as user oracle

Oracle Center of Excellence (is%ellaneo+s
rlo'in* Bnli e telnet does not pro!pt for userna!e. $o-s into re!ote !achine as the user of the current !achine.So entr, for this user !ust (e in 0etc0passwd in re!ote !0c. rlo!in &BA"&@*")"A lps)a) * status of the printer l,stat +, ,r& ( status of ,rint Cob on ,rinter ,r& .exr% file * #ll sets, !aps and a((re)iations for vi can (e stored. #t startup vi loo s for the co!!ands in this file.
Set i!norecase

2e)%2passwd file * &as all the users in the s,ste! with their passwords

Oracle Center of Excellence
(is%ellaneo+s -%on)d..
U$as* : The )alue of this )aria(le decides the default per!issions which a file will ha)e when created (, a user. +s nor!all, declared in .profile. D$as ))) ( will create files with ??? ,er$issions"

ln is used to -i)e files se)eral filena!es. +ts li e a shortcut in Eindows.
ln e$,"s8l e$,loyee ( now e$,"s8l can also be accessed as e$,loyee.

Oracle Center of E7cellence

"&ell Pro'ra$$in' Basi%s

Oracle Center of Excellence "&ell Pro'ra$$in'

when a -roup of BF+G co!!and to (e e4ecuted re-ularl, ,then the, are stored in a file , such files are called Shell Scri,t, Shell Ero!ra$s or Shell Erocedure no restriction on the e4tension (ut it is a co!!on practice to use H"shF e4tension for shell script pro-ra!.

Oracle Center of Excellence xa$ple of "i$ple "&ell "%rip)

• 3%a) s%rip).s& # This is a co$$ent line date G co$$and cal Hdate I>J$ A)JyF H G co$$and echo :Calendar for the current $onth shown above; G co$$and • HviF editor is use to create file and ha)e to !a e e4ecuta(le (, usin-* ch$od >7 filena$e 3or@ ch$od ?KK filena$e • to e4ecute the shell pro-ra! 2ust ha)e to t,pe the na!e of the file at the co!!and pro!pt.

Oracle Center of Excellence read : (a*in' "&ell #n)era%)i4e
The HreadI state!ent is shell/s internal tool for ta in- input fro! the user.

J cat e!p6.sh G Scri,t( e$,&"sh L Interactive version G The ,attern and filena$e is to be su,,lied by the user echo IMn Enter the ,attern to be searched ( Mc I read ,na$e echo I Enter the file to be used( McF read flna$e echo IMn Searchin! for $,na$e for file $filena$eMnF !re, I$,na$eF $flna$e echo IMn Selected record shown aboveMnF-%on)d5.

Oracle Center of Excellence xa$ple of "i$ple "&ell "%rip)

• 3%a) s%rip).s& # This is a co$$ent line date G co$$and cal Hdate I>J$ A)JyF H G co$$and echo :Calendar for the current $onth shown above; G co$$and • HviF editor is use to create file and ha)e to !a e e4ecuta(le (, usin-* ch$od >7 filena$e 3or@ ch$od ?KK filena$e • to e4ecute the shell pro-ra! 2ust ha)e to t,pe the na!e of the file at the co!!and pro!pt.

Oracle Center of Excellence read : (a*in' "&ell #n)era%)i4e
The HreadI state!ent is shell/s internal tool for ta in- input fro! the user.

J cat e!p6.sh G Scri,t( e$,&"sh L Interactive version G The ,attern and filena$e is to be su,,lied by the user echo IMn Enter the ,attern to be searched ( Mc I read ,na$e echo I Enter the file to be used( McF read flna$e echo IMn Searchin! for $,na$e for file $filena$eMnF !re, I$,na$eF $flna$e echo IMn Selected record shown aboveMnF-%on)d5.

Oracle Center of Excellence read : (a*in' "&ell #n)era%)i4e -Con)d5.

• • • •

The script pauses at two points K =irst as for pattern to (e entered . +nputin- the strin- for .,na$e/ #nd assi-n its )alue to .,na$e;. Fe4t it as for filena!e ,entr, the strin- assi-ns the )alue for it.

Oracle Center of Excellence Co$$and Line Ar'+$en)s: Posi)ional Para$e)ers

• • • •

Shell pro-ra! accepts ar-u!ents in another situation too K when specified in the co!!and line itself. This non-interacti)e !ethod of specif,in- ar-u!ents is quite useful for script requirin- few inputs. Ehen ar-u!ents are specified with shell procedure, the, are assi-ned to certain Hs,ecial variablesI or rather H,ositional+,ara$etersI =irst ar-u!ent to $1, second to $2 ,third to $3 and so onL.

Oracle Center of Excellence xa$ple

$ cat e$,A"sh echo IEro!ra$( $0 The nu$ber of ar!u$ent s,ecified is $G The ar!u$ents are $* F !re, I$1F $2 echo IMn Cob is overF where J8 * stores the co!plete set of positional para!eter JM * nu!(ers of ar-u!ents J5 * pro-ra! na!e. E4a!ple* J e!p<.sh director e!p<.lst 'a4i!u! upto ; ar-u!ents can (e passed.

Oracle Center of Excellence Exit ")a)+s of a Co$$and

$? - -i)es the status of the latest co!!and +f the output is * - 6 7 if )&e %o$$and is s+%%essf+l - non89ero if i) fails 7 - : if )&e file is +nreadable -in dealin' wi)& files. E4a!ple * J -rep director e!p.lst 3 0de)0null N echo JO Output* J 5 J -rep !ana-er e!p.lst 3 0de)0null N echo 31 Output* J 6 .

Oracle Center of Excellence T&e Lo'i%al Opera)ors 33 and ;; Condi)ional xe%+)ion
<< operator is used in the sa!e sense as C. E4a!ple * J -rep .director/ e!p6.lst PP echo Hpattern found in file H Output * J 655:1chanchal sin-h)i1 director1sales1705;0791:755 pattern found in file The .;;/ operator is used to e4ecute the co!!and followin- onl, if when the pre)ious co!!and fails. E4a!ple * J -rep .!ana-er/ e!p<.lst 11 echo Hpattern not foundI

Oracle Center of Excellence exit : "%rip) Ter$ina)ion
• • • • • The exit state!ent is used to per!anentl, ter!inates the pro-ra!. Ehen this state!ent is encountered in a script, e4ecution is halted and control is returned to the callin- pro-ra! in !ost cases the shell. Feed not to (e included , shell understand the end of the pro-ra!. Quite often used with a co!!and when it fails. E4a!ple * J -rep HJ6I J< 11 exit 2
e%&o =pa))ern fo+nd > ,ob o4er?

• •

The default ar-u!ent is Rero which i!plies success Fon-Rero )alues are used to indicates )aries errors.

Oracle Center of Excellence T&e “if” Condi)ion
• +n the shell, the state!ent uses the followin- for!s, !uch li e the one used in other lan-ua-es if >condition@ is true then E4ecute co!!and else E4ecute co!!and fi E4a!ple * J if -rep HdirectorI e!p.lst then echo HPattern found K So( O)erI else echo HPattern not foundI fi > contd..@

Oracle Center of Excellence T&e “if” Condi)ion - Con)d.. . .
J ;D7: 1 2ai shar!a 1 director 1 production 1 6<0590D517555 Pattern found K So( O)er • "elational operator used (, HifF ( Operator 'eanin+e8 Equal to +ne Fot equal to +!t Greater than +!e Greater than or equal to +lt $ess than +le $ess than or equal to

Oracle Center of Excellence T&e “if” Condi)ion - Con)d.. . .
J ;D7: 1 2ai shar!a 1 director 1 production 1 6<0590D517555 Pattern found K So( O)er • "elational operator used (, HifF ( Operator 'eanin+e8 Equal to +ne Fot equal to +!t Greater than +!e Greater than or equal to +lt $ess than +le $ess than or equal to

Oracle Center of Excellence T&e =Case” Condi)ion
• S,nta4 * case e4pression in ,attern&@ e4ecute co!!ands NN ,atternA@ e4ecute co!!ands NN ,atternN@ e4ecute co!!ands NN esac

Oracle Center of Excellence T&e =Case” Condi)ion
$ cat men .s! echo I 9EODMn &.List of filesMn A"Erocess of userMn N" Today;s Date ".Dses of the syste$Mn K" Puit to Dni7Mn Enter your o,tion( McF read choice case I$choiceF in 1#ls Ll QQ A#,s Lf QQ N#date QQ "#who QQ K#e7it QQ esac

Oracle Center of Excellence expr : Co$p+)a)ion
+t perfor! arith!etic operation on inte-er, and also !anipulate strin-s to a li!ited e4tent. =or J 4C9 ,C? J e7,r JD J e7,r J-< J e7,r J6? J e7,r J6 J e7,r J9

9T? J4 - J, 9 U8 ? J, 0 J4 69 V ?

Output* Output* Output* Output* Output*

Oracle Center of Excellence $leep And %ait
• +s used in order to introduce so!e dela, in a shell script =or e4a!ple* +f the user needs to see so!e !essa-es on screen (efore the script starts doin- so!ethin- else. Wou !a, li e to !a e a chec re-ularl, >sa,, once a !in@ for an e)ent to occur >sa, ,for a file to sprin- into e4istence@ Slee, is a BF+G co!!and that introduces this dela,. E4a!ple * slee, 655 N echo H 655 seconds ha)e (een elapsedI O0P 655 seconds ha)e (een elapsed

• • •

Oracle Center of Excellence %ait& Co$$and
• wait is a shell (uilt-in co!!and that chec s whether all (ac -round process ha)e (een co!pleted Bseful when ,ou ha)e to run a 2o( in the (ac -round, and to !a e sure the co!!and is co!pleted so that ,ou can run ,et another pro-ra!. E4a!ple * J wait J wait 69D M waits for all (ac -round co!!and M waits for co!pletion of process P+D 69D

• • •

Oracle Center of Excellence %!ile & Loopin'
• S,nta4 * while condition is true do e4ecute done

Oracle Center of Excellence Example&
$ cat emp'.s! G Ero!ra$ ( e$,K"sh + + shows the use of while loo, answer2y (!ile )*$ans(er” + “,” G 9ust it to y first to enter the loo, .o echo IEnter the code and descri,tion( McF read code descri,tion G Read both to!ether echo I$code 5 $descri,tionF %% newlist GA,,end a line to file echo IEnter any $ore 3y6n4R McF read any$ore case $any$ore in yS5TS4 answer2y QQ GAlso acce,t yes= TES etc" nS5 OS4answer2n QQ GAlso acce,t no= OO etc" S4 answer2y QQ GAny other re,ly $eans y esac .one

Oracle Center of Excellence $ettin/ p an #nfini)e loop
E7a$,le ( while true Q do df Lt slee, N)) done • This is useful if ,ou want to see the free space a)aila(le on ,our disc e)er, fi)e !in.

Oracle Center of Excellence for & 0oopin/ (it! a 0ist
• • • The for loop is different in structure fro! other one used in other pro-ra!!inlan-ua-e Fo ne4t , no step (ut it uses .list/ instead S,nta4 * for variable in list do e4ecute co!!ands done #dditional para!eters are variable and list. E4ecutes as !an, ti!es as the ite!s in the list.

• •

Oracle Center of Excellence

E4a!ple 6* J for 7 in &AN' do echo IThe value of 7 is $7F done Output * The value of 7 is & The value of 7 is A " A

Oracle Center of Excellence E4a!ple <
J cat test%files.sh
GChec in! for ,articular chan!es in files set +7 cd r$ test_result_A')"lo! echo UChec in! ,attern in files re,laced in code controlU for t$,l in Hcat sa!ar6)))'6data6t$,l_lis_A')"t,lH do echo $t$,l %% test_result_A')"lo! !re,UA0I )A))*= CA0 )NA'A= C6C &)A*)+))U $R_DATA6$t$,l %% test_result_A')"lo! done echo UErocess co$,leted"""U

Sign up to vote on this title
UsefulNot useful