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 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 at command 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.
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 -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.
Oracle Center of Excellence Scheduling jobs in Unix at : run a particular job/command at a particular time at 14:08 test.sh at -l --> shows the list of jobs submitted by current user cron : It is a daemon running continuously and after every minute checks into /usr/spool/cron/crontabs for submitted jobs A user can also place a file named as his user-id in this directory. Each crontab file has a list of commands along with the schedule of execution 00-10 17 * 3,6,9,12 1 find / -n .last_time -print > backuplist The five fields separated by blanks represent date and time.
Oracle Center of Excellence Cron (Contd.) • • • • •
First Field specifies the no. of minutes after the hour when the command is to be executed (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 be executed 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
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 logs out nohup sort emp.lst &
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
Oracle Center of Excellence Backing up operating system files: dd : Used mainly for copying media like floppy and tape dd 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
Oracle Center of Excellence Backing up operating system files (contd.): tar : creates archives on tapes and floppies. Older cousin of cpio tar 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
Oracle Center of Excellence What is Mounting ? Mounting is the process of attaching a filesystem to the root filesystem. The point at which 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 the mount point is the argument. Dismounting/unmounting makes the filesystem inaccessible. umount /oracle : mount point umount /dev/charlie : device name of filesystem
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 to owner(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
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, which means all the users belonging to the group dba will have same rights to the file as the group permissions.
Oracle Center of Excellence Can I log in as a different user without disconnecting the current session? su -u oracle : will attempt to log you in as user oracle
Oracle Center of Excellence Miscellaneous rlogin: Unlike telnet does not prompt for username. Logs into remote machine as the user of the current machine.So entry for this user must be 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
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.
Oracle Center of Excellence
Shell Programming Basics
Oracle Center of Excellence Shell Programming
•
•
when a group of UNIX command to be executed regularly ,then they are stored 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.
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 the command prompt.
Oracle Center of Excellence read : Making Shell Interactive The “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 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 the command prompt.
Oracle Center of Excellence read : Making Shell Interactive The “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 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 Command Line Arguments: Positional Parameters
• • • •
Shell program accepts arguments in another situation too – when specified 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 assigned to certain “special variables” or rather “positional-parameters” First argument to $1, second to $2 ,third to $3 and so on….
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 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 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 the previous command fails. Example : $ grep ‘manager’ emp2.lst || echo “pattern not found”
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 The “if” Condition •
•
In the shell, the statement uses the following forms, much like the one used in other languages if (condition) is true then Execute command else Execute 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 The “if” Condition ( Contd.. . ) $ 9876 | jai sharma | director | production | 12/03/80|7000 Pattern 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 The “if” Condition ( Contd.. . ) $ 9876 | jai sharma | director | production | 12/03/80|7000 Pattern 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 The “Case” Condition •
Syntax : case expression in pattern1) execute commands ;; pattern2) execute commands ;; pattern3) execute commands ;; esac
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\n 5. 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 expr : Computation It perform arithmetic operation on integer, and also manipulate strings to a limited extent.
Output: Output: Output: Output: Output:
For $ x=3 y=5 $ expr $8 $ expr $-2 $ expr $15 $ expr $1 $ expr $3
3+5 $x - $y 3 \* 5 $y / $x 13 % 5
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 the script 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 Wait: Command •
wait is a shell built-in command that checks whether all background process have been completed
•
Useful when you have to run a job in the background, and to make sure the command is completed so that you can run yet another program.
• • •
Example : $ wait $ wait 138
# waits for all background command # waits for completion of process PID 138
Oracle Center of Excellence While : Looping •
Syntax : while condition is true do execute done
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 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 five min.
Oracle Center of Excellence for : Looping with a List • • •
• •
The for loop is different in structure from other one used in other programming language 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
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 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..."