unix for dba
TRANSCRIPT
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 1/41
Oracle Center of Excellence
Unix For DBA’s
Oracle CoE
TCS - GDC , SEEPZ
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 2/41
Oracle Center of Excellence
Variables we need in .profile
PATH Variable : • set in .profile
• Sequence of directories that the shell will search to look for a command
• The ORACLE_HOME must be added to PATH
ORACLE_SID variable : Must be set in the shell, can be set in .profile or atcommand prompt.Identifies the oracle instance you are currently working on.
$echo ORACLE_SID
This variable will identify the Oracle instance you are currently working in.
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 3/41
Oracle Center of Excellence
Perform Searches in Unix:
grep : Searches for a pattern in a file• grep -i ‘pattern’ filename
find : to search for files on the filesystem
find / -name <filename> -print
Monitoring Background Processes running on Unix
ps : Displays the processes on the system
• ps -ef • ps -ef | grep ora : Shows the oracle background processes running. Can be used
to determine whether an instance is running or not.
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 4/41
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 5/41
Oracle Center of Excellence
Cron (Contd.)
• First Field specifies the no. of minutes after the hour when the command is to beexecuted (legal values 00 to 59)
• Second filed specifies the hour in 24 hour format (1 to 24)
• Third field is day of the month (1 to 31) .The * on this field means it is to beexecuted daily
• Fourth field specifies the month (1-12)
• Fifth field is the day of the week (0-6 Sunday being 0)
A user can add a command to his file by
crontab <commands>
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 6/41
Oracle Center of Excellence
Running a process in the background
sort -o emp.lst emp.lst & - runs the sort as a background process
nohup : will continue running the background process even if the user logsout
nohup sort emp.lst &
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 7/41
Oracle Center of Excellence
Monitoring Disk usage
df : reports free space available on disk df -k : will show all the mount points and their sizes in Kilo Bytes
du : reports the disk usage
compress : compresses file and generates the filename.Z
compress filename : give a compressed file as filename.Z
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 8/41
Oracle Center of Excellence
Backing up operating system files:
dd : Used mainly for copying media like floppy and tapedd if=/dev/rdsk/f0q18t of=temp bs=147456
if=(input filename)
of=(output filename)
bs=(blocksize)
cpio : used to copy files to and from a backup device
ls | cpio -ov >/dev/rmt/0m
ls generates the list of files (find too can be used for this)
-o creates an archive which is to be redirected to the backup device
cpio -iv < /dev/rmt/0m restores the files from backup device
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 9/41
Oracle Center of Excellence
Backing up operating system files (contd.):
tar : creates archives on tapes and floppies. Older cousin of cpiotar cvf /dev/rmt/0m /Oracle/dbs/initTest.ora
tar cvfkb /dev/rmt/0m 1440 18 index.sql : multi-volume backup
tar xvf /dev/rmt/0m
tar uvf /dev/rmt/0m index.sql
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 10/41
Oracle Center of Excellence
What is Mounting ?
Mounting is the process of attaching a filesystem to the root filesystem. The point atwhich a directory/filesystem is attached is called the mount point.
Mount /dev/charlie /oracle :mounts the ‘/dev/charlie’ at mount point ‘/oracle’.
Mount -f HS, lower /dev/cd0 /cdrom
-f is used to specify the type of filesystem to be mounted
HS is the type of CD-ROM format
lower keyword ensures lowercase files are displayed in lowercase only
-r option if used will mount the filesystem in read-only mode
Umount can be used to dismounting the file system, either the filesystem or themount point is the argument. Dismounting/unmounting makes the filesysteminaccessible.
umount /oracle : mount point
umount /dev/charlie : device name of filesystem
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 11/41
Oracle Center of Excellence
How do I change the access rights to a file?
Chmod is used to set the permissions for all three categories of users (owner,group, others)
chmod u+x initTEST.ora : adds(+) executable(x) permission on initTEST.ora toowner(u)
chmod category operation permission filename(s)
category : u(owner), g(group), o(others), a(all, is the default)
permission : r(read), w(write), x(executable)
chmod 764 initTEST.ora : gives rwx(7) to owner, rw(6) to group, r(4) to others
r(read) = 4, w(write) = 2, x(executable) = 1
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 12/41
Oracle Center of Excellence
How do I change the access rights to a file? (contd.)
Chown is used to change the ownership of a file.
Chown oracle initTEST.ora : now oracle is the owner of the file
Chgrp changes the group ownership of the file
chgrp dba initTEST.ora : the group dba has the group ownership of the file, whichmeans all the users belonging to the group dba will have same rights to the file asthe group permissions.
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 13/41
Oracle Center of Excellence
Can I log in as a different user without disconnecting the currentsession?
su -u oracle : will attempt to log you in as user oracle
O l C f E ll
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 14/41
Oracle Center of Excellence
Miscellaneous
rlogin: Unlike telnet does not prompt for username. Logs into remotemachine as the user of the current machine.So entry for this user mustbe in /etc/passwd in remote m/c.
rlogin 192.168.0.2
lpstat : status of the printer lpstat -p pr1 : status of print job on printer pr1
.exrc file : All sets, maps and abbreviations for vi can be stored. At startup
vi looks for the commands in this file.Set ignorecase
/etc/passwd file : Has all the users in the system with their passwords
O l C t f E ll
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 15/41
Oracle Center of Excellence
Miscellaneous (contd.)
Umask : The value of this variable decides the default permissions which a file will
have when created by a user. Is normally declared in .profile.
Umask 000 : will create files with 777 permissions.
ln is used to give files several filenames. Its like a shortcut in Windows.
ln emp.sql employee : now emp.sql can also be accessed as employee.
O l C t f E ll
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 16/41
Shell Programming
Basics
Oracle Center of Excellence
O l C t f E ll
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 17/41
Oracle Center of Excellence
Shell Programming
• when a group of UNIX command to be executed regularly ,then they arestored in a file , such files are called Shell Script , Shell Programs or Shell Procedure
• no restriction on the extension but it is a common practice to use “.sh” extension for shell script program.
O l C t f E ll
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 18/41
Oracle Center of Excellence
Example of Simple Shell Script
• $cat script.sh
# This is a comment line
date # command
cal ̀date “+%m 20%y” ̀ # command echo ‘Calendar for the current month shown above’ # command
• “vi” editor is use to create file and have to make executable by using:
chmod +x filename ( or) chmod 755 filename
• to execute the shell program just have to type the name of the file at thecommand prompt.
O l C t f E ll
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 19/41
Oracle Center of Excellence
read : Making Shell InteractiveThe “read ” statement is shell’s internal tool for taking input from the user.
• $ cat emp1.sh
# Script: emp1.sh – Interactive version
# The pattern and filename is to be supplied by the user
echo “\n Enter the pattern to be searched : \c “
read pname
echo “ Enter the file to be used: \c”
read flname
echo “\n Searching for $pname for file $filename\n”
grep “$pname” $flname
echo “\n Selected record shown above\n” (contd…)
O l C t f E ll
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 20/41
Oracle Center of Excellence
Example of Simple Shell Script
• $cat script.sh
# This is a comment line
date # command
cal ̀date “+%m 20%y” ̀ # command echo ‘Calendar for the current month shown above’ # command
• “vi” editor is use to create file and have to make executable by using:
chmod +x filename ( or) chmod 755 filename
• to execute the shell program just have to type the name of the file at thecommand prompt.
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 21/41
Oracle Center of Excellence
read : Making Shell InteractiveThe “read ” statement is shell’s internal tool for taking input from the user.
• $ cat emp1.sh
# Script: emp1.sh – Interactive version
# The pattern and filename is to be supplied by the user
echo “\n Enter the pattern to be searched : \c “
read pname
echo “ Enter the file to be used: \c”
read flname
echo “\n Searching for $pname for file $filename\n”
grep “$pname” $flname
echo “\n Selected record shown above\n” (contd…)
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 22/41
Oracle Center of Excellence
read : Making Shell Interactive (Contd…)
• The script pauses at two points –
• First ask for pattern to be entered . Inputing the string for ‘ pname’
• And assign its value to ‘ pname’ .
• Next it ask for filename ,entry the string assigns the value for it.
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 23/41
Oracle Center of Excellence
Command Line Arguments:
Positional Parameters
• Shell program accepts arguments in another situation too – whenspecified in the command line itself.
• This non-interactive method of specifying arguments is quite useful for script requiring few inputs.
• When arguments are specified with shell procedure, they are assignedto certain “special variables” or rather “ positional-parameters”
• First argument to $1, second to $2 ,third to $3 and so on….
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 24/41
Oracle Center of Excellence
Example
$ cat emp2.sh
echo “Program: $0
The number of argument specified is $#
The arguments are $* ” grep “ $1” $2
echo “\n job is over”
where $* : stores the complete set of positional parameter
$# : numbers of arguments
$0 : program name.Example:
$ emp2.sh director emp2.lst
Maximum upto 9 arguments can be passed.
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 25/41
Oracle Center of Excellence
Exit Status of a Command
$? - gives the status of the latest command
If the output is :
- 0 , if the command is successful- non-zero if it fails ,
- 2 if the file is unreadable (in dealing with files)
Example :
$ grep director emp.lst > /dev/null ; echo $?Output: $ 0
$ grep manager emp.lst > /dev/null ; echo $?
Output: $ 1 .
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 26/41
Oracle Center of Excellence
The Logical Operators $$ and ||
Conditional Execution
&& operator is used in the same sense as C.
Example :
$ grep ‘director’ emp1.lst && echo “pattern found in file “
Output :
$ 1006|chanchal singhvi| director|sales|7/09/73|6700
pattern found in file
The ‘||’ operator is used to execute the command following only if when theprevious command fails.
Example :
$ grep ‘manager’ emp2.lst || echo “pattern not found”
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 27/41
Oracle Center of Excellence
exit : Script Termination
• The exit statement is used to permanently terminates the program.• When this statement is encountered in a script, execution is halted and control is
returned to the calling program in most cases the shell.
• Need not to be included , shell understand the end of the program.
• Quite often used with a command when it fails.
• Example :$ grep “$1” $2 || exit 2
echo “pattern found – job over”
• The default argument is zero which implies success
• Non-zero values are used to indicates varies errors.
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 28/41
Oracle Center of Excellence
The “if” Condition
• In the shell, the statement uses the following forms, much like the one used inother languages
if (condition) is true
then
Execute command
elseExecute command
fi
• Example :
$ if grep “director” emp.lst
then echo “Pattern found – Job Over”
else echo “Pattern not found”
fi
( contd..)
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 29/41
Oracle Center of Excellence
The “if” Condition ( Contd.. . )
$ 9876 | jai sharma | director | production | 12/03/80|7000Pattern found – Job Over
• Relational operator used by “if” :
Operator Meaning
-eq Equal to
-ne Not equal to
-gt Greater than
-ge Greater than or equal to
-lt Less than
-le Less than or equal to
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 30/41
Oracle Center of Excellence
The “if” Condition ( Contd.. . )
$ 9876 | jai sharma | director | production | 12/03/80|7000Pattern found – Job Over
• Relational operator used by “if” :
Operator Meaning
-eq Equal to
-ne Not equal to
-gt Greater than
-ge Greater than or equal to
-lt Less than
-le Less than or equal to
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 31/41
Oracle Center of Excellence
The “Case” Condition
• Syntax :
case expression in
pattern1) execute commands ;;
pattern2 ) execute commands ;;
pattern3) execute commands ;;esac
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 32/41
Oracle Center of Excellence
The “Case” Condition
$ cat menu.sh
echo “ MENU\n
1.List of files\n 2.Process of user\n 3. Today’s Date 4.Uses of the system\n5. Quit to Unix\n Enter your option: \c”
read choice
case “$choice” in
1)ls –l ;;
2 ) ps –f ;;
3 )date ;;
4)who ;;
5 )exit ;;
esac
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 33/41
Oracle Center of Excellence
expr : Computation
It perform arithmetic operation on integer, and also manipulate strings to a limitedextent.
For $ x=3 y=5
$ expr 3 + 5
Output: $8
$ expr $x - $y
Output: $-2
$ expr 3 \* 5
Output: $15
$ expr $y / $x
Output: $1
$ expr 13 % 5
Output: $3
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 34/41
Oracle Center of Excellence
Sleep And Wait
• Is used in order to introduce some delay in a shell script
For example: If the user needs to see some messages on screen before thescript starts doing something else.
• You may like to make a check regularly (say, once a min) for an event to occur
(say ,for a file to spring into existence)
• Sleep is a UNIX command that introduces this delay.
• Example : sleep 100 ; echo “ 100 seconds have been elapsed”
• O/P 100 seconds have been elapsed
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 35/41
Oracle Center of Excellence
Wait: Command
• wait is a shell built-in command that checks whether all background processhave been completed
• Useful when you have to run a job in the background, and to make sure thecommand is completed so that you can run yet another program.
• Example :
• $ wait # waits for all background command
• $ wait 138 # waits for completion of process PID 138
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 36/41
Oracle Center of Excellence
While : Looping
• Syntax :
while condition is true
do
executedone
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 37/41
Oracle Center of Excellence
Example:$ cat emp5.sh
# Program : emp5.sh - - shows the use of while loop
answer=y
while [‘$answer” = “y” ] # Must it to y first to enter the loop
do
echo “Enter the code and description: \c”
read code description # Read both together echo “$code | $description” >> newlist #Append a line to file
echo “Enter any more (y/n)? \c”
read anymore
case $anymore in
y*|Y*) answer=y ;; #Also accept yes, YES etc.n*| N*)answer=n ;; #Also accept no, NO etc.
*) answer=y ;; #Any other reply means y
esac
done
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 38/41
Oracle Center of Excellence
Setting up an Infinite loop
Example :
while true ; do
df –t
sleep 300
done &
• This is useful if you want to see the free space available on your disc every fivemin.
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 39/41
O ac e Ce te o ce e ce
for : Looping with a List
• The for loop is different in structure from other one used in other programminglanguage
• No next , no step but it uses ‘list ’ instead
• Syntax :
for variable in list
do
execute commands
done
• Additional parameters are variable and list .
• Executes as many times as the items in the list .
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 40/41
Example 1:$ for x in 1234
do
echo “The value of x is $x”
done
Output :
The value of x is 1
The value of x is 2
.
4
Oracle Center of Excellence
8/14/2019 Unix for DBA
http://slidepdf.com/reader/full/unix-for-dba 41/41
Example 2
$ cat test_files.sh
#Checking for particular changes in files
set -x
cd
rm test_result_240.log echo "Checking pattern in files replaced in code control"
for tmpl in `cat ksagar/0004/data/tmpl_lis_240.tpl`
do
echo $tmpl >> test_result_240.log
grep"ABI 02008, CAB 03242, C/C 10280-00" $R_DATA/$tmpl >>test_result_240.log
done
echo "Process completed..."