Professional Documents
Culture Documents
Unix For DBA
Unix For DBA
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
This variable will identify the Oracle instance you are currently working in.
Oracle Center of Excellence
Perform Searches in Unix:
• 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)
crontab <commands>
Oracle Center of Excellence
Running a process in the background
nohup : will continue running the background process even if the user logs
out
nohup sort emp.lst &
Oracle Center of Excellence
Monitoring Disk usage
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.
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 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.)
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
.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.
Shell Programming
Basics
Oracle Center of Excellence
Shell Programming
• $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…)
$ 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
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
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
• 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.. . )
• 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
• 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)
• 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 # waits for all background command
• $ wait 138 # waits for completion of process PID 138
Oracle Center of Excellence
While : Looping
• Syntax :
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