Beruflich Dokumente
Kultur Dokumente
Introduction to UNIX
History of UNIX
GE, Bell Labs, and MIT: Network OS; Multiplex Information and Computing Services
(Multics)
Dennis Ritchie, Ken Thompson, and Brian Kerningham
Unix code development language:
Assembly Code Machine dependant and not portable
B: Ken Thompson
C: Dennis Ritchie
Open source development has lead to the availability of various flavors of UNIX.
Features of UNIX
Multi-user
This feature is facilitated by approaches like Dumb Terminals, Terminal Emulation, and
Dial-up Connection
Discussion on SSH (Terminal Emulation)
>ssh –l informat app360.sea.wamu.net
>ssh informat@app360.sea.wamu.net
Shells in UNIX
Organization of the Unix System
Applications - Shell - Kernel - Hardware
Bourne Shell (sh), C Shell (csh), Korn Shell (ksh), and Bash Shell (bash)
>echo $SHELL
The default shell is set by the admin while creating the user and is given by the above
command.
Every time a user logs in, a shell process is invoked which acts as the parent process for
other processes created and serves as the command interpreter.
-1-
Wipro Technologies; E-Enabling; Data Warehousing
>export $PAGER=more
Navigation is through ‘f’ and ‘b’.
Discussion on search facility, using the forward slash (/) and how to quit (:q).
To get a one line description of the command, use
man –k, apropos, man –f, whatis
Modes of Operation
In the Command mode, all the keys pressed by the user are interpreted as commands.
In the Insert mode, one is permitted to insert new text.
In the ex Command mode, the commands are given at the command line.
Undoing changes.
u/U Latest change.
“[1-9]p If you want to undo changes (deletions) which were done earlier.
Block commands.
All executed in the ex command mode.
Line numbers needed. (se nu, se nonu)
Deletion :m,n d
Move :m,n mo p
Copy :m,n co p
All the above commands make changes within the same file.
To copy lines to another file …
:m,n w filename
:m,n w >> filename
-2-
Wipro Technologies; E-Enabling; Data Warehousing
Search
Mechanism …
Command Function
/pattern Searches forward
?pattern Searches backward
N Repeats the last search command
N Repeats the last search command in the
backward direction.
Note: If any special character is being searched then they must be preceded with a
backslash (\)
Search and Replace
Mechanism …
Command Function
:s/str1/str2 Replace first occurrence of str1 with str2 in
current line
:s/str1/str2/g Replace all occurrences of str1 with str2 in
current line
:m,n s/str1/str2/g Replace all occurrences of str1 with str2
from lines m to n.
:1,$ s/str1/str2/g Replace all occurrences of str1 with str2
-3-
Wipro Technologies; E-Enabling; Data Warehousing
abbr
abbr command in vi stands for abbreviation. It can be used to enhance the user’s
convenience.
(Esc):abbr pr printf pr now becomes the shorthand for printf
(Esc):abbr Display the entire list of macros you have set.
(Esc):una pr The pr abbreviation is forgotten.
Whenever vi is invoked, it searches for the .exrc file in the user’s default working
directory ($HOME). This file is optional, i.e., vi does not give an error if it is not able to
locate this file. However, if the file is present any set commands or abbreviations that are
stored in this file automatically become effective for all vi sessions.
-4-
Wipro Technologies; E-Enabling; Data Warehousing
User defined variables are used most extensively in shell programming. To access the
value of any kind of variable, we need to precede the variable name with a dollar ($) sign.
1. All shell variables are string variables. We cannot carry out arithmetic calculations on
them unless we use a command called expr.
2. While using the assignment operator to assign values to variables, there should be no
spaces on either side of =. During assignment, if the variable does not exist it will be
created.
3. If the variable needs to contain more than one word then the assignment should be
made using double quotes.
4. We can carry out more than one assignment in a line. So also we can echo more than
one variable’s value at a time.
5. All variables defined inside a script die the moment the execution of the script is over.
6. A constant variable can be created by saying $>readonly <variable_name>.
7. Variables can be made to cease existing by using the unset command. $>unset
<variable_name>. This command cannot be used on system variables.
8. The entire user defined variables and system defined variables can be displayed by
using the command set at the command prompt.
-5-
Wipro Technologies; E-Enabling; Data Warehousing
Every UNIX command/script returns an exit status of zero, if it has been successful
executed, otherwise, a value greater than zero is returned.
$>echo $?
$? stores the exit status of the last executed command.
File Tests
Option TRUE if …
-s File exists and has size greater
than zero
-f File exists and is not a directory
-d File exists and is a directory
-r File exists and user has read
permission on it
-w File exists and user has write
permission on it
-x File exists and user has
execute permission on it
String Tests
Operator TRUE if …
string1 = string2 Strings are same
-n string Length of string > 0
-z string Length of string = 0
string String is not a null
string
Logical operators
Operator Meaning
-a AND
-o OR
! case valueNOT
in
choice 1)
The Case Control…..Structure
…..
;;
choice 2)
…..
…..
;;
*)
…..
;; -6-
esac
Wipro Technologies; E-Enabling; Data Warehousing
B. Program Example …
1. The file /etc/passwd contains information about all users. However it is difficult to
decipher the information stored in it. Write a shell script which would receive the
logname during execution, obtain information about it from /etc/passwd and display
this information on the screen in easily understandable format.
2. If the cost price and selling price of an item is input through the keyboard, write a
program to find out whether the seller has made profit or incurred loss. Also
determine how much profit was made or loss incurred.
-7-
Wipro Technologies; E-Enabling; Data Warehousing
Whenever the keyword ‘break’ is encountered inside any loop, control automatically
passes to the first statement after the loop in which it is placed. However, if we have
nested loops and we want to take the control outside any one of the outer loops, then we
use break <loop-number>. The same facility is also provided with the continue
statement. Both ‘break’ and ‘if’ are generally associated with an ‘if’.
B. Program Example …
1. You want to check whether your login is being used by anyone else. Write a shell
script which checks after every one minute whether anyone other than you has
logged in using your login name. Keep on checking until the time exceeds 5 minutes
or the number of such logins exceeds 5. Maintain a log-file for the checking.
2. You are trying to search for the word unix in your files. Your shell script should search
for the pattern in each of the files supplied at the command prompt in turn and stop
at the first file that it encounters which matches the search criteria. The filename
should be displayed on the screen. If no arguments are supplied then all the files in
the current working directory should be assumed.
-8-
Wipro Technologies; E-Enabling; Data Warehousing
Terminal Settings
Setting Action
echo Echoes back every character typed
-echo Does not echo back every character typed
sane Resets all modes to some reasonable values.
-9-
Wipro Technologies; E-Enabling; Data Warehousing
Here document
The HERE document signifies that the data is here instead of in a separate file. Any
command using statndard input can also take input from a here document.
Example (Executing Oracle procedures from Unix scripts)
# ---------------------------------------------------------------
# Calling Database procedures from a UNIX script
# ---------------------------------------------------------------
# Script Name call_sortbydisplayorder.sh
# Usage sh call_sortbydisplayorder.sh
# ---------------------------------------------------------------
(sqlplus –s user/password@hoststring << eof
set timing on
set feedback on
exec sortbydisplayorder
set feedback off
set timing off
eof
)
# ---------------------------------------------------------------
# End of script
# ---------------------------------------------------------------
Debugging scripts
Debugging of scripts can be achieved by setting on the flags –x and –v.
B. Program Example …
1. Accept a file name as input and delete all occurrences of the supplied word (2nd
Argument) in the file.
2. You are trying to search for the word unix in your files. Your shell script should search
for the pattern in each of the files supplied at the command prompt in turn and stop
at the first file that it encounters which matches the search criteria. The filename
should be displayed on the screen. If no arguments are supplied then all the files in
the current working directory should be assumed.
- 10 -
Wipro Technologies; E-Enabling; Data Warehousing
Goals
1. Make the least efforts when moving scripts from one environment to another.
2. Make the least errors when testing scripts.
3. Enable running the same version of scripts simultaneously in different environments
in the same machine (in case the test/dev/prod environments reside in the same
machine).
Standards
1. Script naming & Coding style
A script name should have a suffix indicating the shell on which it has been designed
to be executed (.sh/.ksh/.csh). The script name should have a mixture of cases and
there should be an avoidance of the use of underscore.
The following sections should be a part of the script in the sequence mentioned …
- 11 -
Wipro Technologies; E-Enabling; Data Warehousing
.etl.env). After this is done, put the following line of code in your script before any
processing is done …
. <path>/<envFile>
To differentiate between your various environments (especially if all of them lie on the
same machine), name the envFile in such a manner that it indicates the environment
to which it belongs and just change the above line of code while moving from one
environment to another.
Note:
For any script that will not be directly called but will be called by a parent script, and if
it references any environment variables and its parent script has already invoked the
environment variable file, then the child script does not have to re-invoke the
environment variables file.
g. SQL File
Scripts involving calling a SQL file should pass the parameter to the SQL file instead of
hard coding the value of the parameter in the SQL script.
Example:
Replace the statement below in the SQL file …
spool /wamu/informat/logFiles
with …
spool &1
and pass the value from the Unix script as parameter like below …
sqlplus user/passwd@connectString /wamu/informat/logFiles
3. Script migration
During migration of a script from development to production check the file for
execution privilege. File must have proper execution privilege.
- 12 -