UNIX
UNIX
Types of OS
These are simple operating system designed to manage one task at a time, for use
by a single user for a standalone single computer for performing a single task.
Multi-programming OS:
These OS allow more than one programs to run at the same time.
Embedded OS:
Embedded in a device in the ROM. They are specific to a device and are less
resource intensive.
Function Of OS
Introduction to UNIX OS
Supports C, Fortran, Basic, Pascal, COBOL, Lisp, Prolog, Java, Ada compilers
• Shell
• Utilities
Functions of Kernel:
It allocates time and memory to programs and handles the file store and
communications
Interacts directly with the hardware through device drivers
Provides sets of services to programs
Manages memory, controls access, maintains file system, handles interrupts,
allocates resources of the computer
System calls
The system calls are functions used in the kernel itself. UNIX system calls are used
to manage the file system, control processes, and to provide interpocess
communication.
Ordinary Files
Directory Files
Special Files (device, pipe, fifo, socket).
Absolute path name - file location is determined with respect to the root.
Relative path name - file location is determined with respect to the current
directory.
/ home - It holds user's home directories. In other UNIX systems, this can be / usr
directory.
/ bin - It holds many of the basic Linux programs; bin stands for binaries, files that
are executable.
/dev – It holds device files. All info sent to /dev/null is thrown into trash. Your
terminal is one of the /dev/tty files.
a) boot block : This block is located in the first few sectors of a file
system. It contains the initial bootstrap program used to
load the operating system.Typically, the first sector contains a bootstrap program
that reads in a larger bootstrap program from the next
few sectors, and so forth.
b) super block : This block describes the state of the file system such
as the total size of the partition, the block size, pointers to
a list of free blocks, the inode number of the root directory, magic number or file
signature (the first few bytes of a file which are unique to a
particular file type), etc.
For each file created in the system, an inode is also created. Inode is a disk file
record of 64 bytes that maintains the permanent attributes of a file.
An inode is permanent and it exists until the corresponding file is removed from
the system.
Whenever a file is opened, its inode is brought into main memory. The active
inode is kept there until the file is closed and is used to locate the beginning of an
open file on disk and to verify that every I/O request comes from a valid user as
per specified access permissions.
Objective
This module is to have an understanding of different user types in UNIX and their
access rights.
Course Content
User types
File access permissions
Changing file access permissions using symbolic mode
Changing file access permissions using octal mode
Read
Write
Executed
These three modes or permissions have different meaning for a file and a directory;
Symbolic mode
Absolute fashion
chmod command options:
Example:
Let us assume there is a file name "abc" , whose initial permissions are only read
permission to user and group and others. We will provide full permissions to all
three types of users.
Let us verify the existing file permissions using ls -l command as below and
observe the next steps, like changing the permissions and verifying the changes.
In the above example,
chmod u+wx,g+wx abc , adds write and execute permission to user and group to
the file abc
chmod g-x abc, removes execute permission from group from the file abc.
chmod o=r-- abc, provides read permission and removes write and execute
permission from others for file abc
The second way to modify permissions with the chmod command is to use a
number to specify each set of permissions for the file. Each permission is assigned
a value, as the following table shows, and the total of each set of permissions
provides a number for that set.
If we observe the output, initially owner have read and write permissions and
group and others have only read permission
2. Let us remove all the permission from all users for abc file, as below
chmod 0 abc
Let us verify the changed permissions
ls -l abc
---------- 1 735873 oinstall 0 Feb 7 12:37 abc
Yes all permissions are taken away from all users on the file abc
3. To provide all permissions to the user, read permission to group and write and
execute permissions for Others,
Editors available unix are ed, node, NEdit, emacs and vi .
One of the first end-user programs hosted on the system and standardized in
UNIX-based systems ever since is the vi editor. ed is a line editor for the UNIX
operating system.
vi Editor:
vi (pronounced "vee-eye") is short for "vi" sual editor. It is a visual editor used to
create and edit text files.
It displays a window into the file being edited that shows 24 lines of text. vi lets
you add, change and delete text, but does not provide formatting capabilities like
centering lines or indenting paragraphs.
vi []
For cursor movement within the vi editor, we can not use the mouse. We have to
use key commands. To use a key command, user has to be in the command
mode. To go to command mode, press the Esc key.
The new or modified file is saved when user exits from vi. But it is also possible to
exit without saving the file.
To use a command to exit from vi, after pressing the Esc key, colon: is to be
pressed.
The cursor moves to bottom of screen whenever a colon (:) is typed. This type of
command is completed by hitting the (gold ) key.
: wq quit vi, writing out modified file to file named in original invocation
: q! quit vi even though latest changes have not been saved for this vi call
i
insert text before the cursor, until is hit
I
insert text at beginning of current line, until hit
Ncc change ( replace) the next N lines, starting with the current line,
or stopping when is hit
cNc
D delete the remainder of the line, starting with current cursor position
Nyy copy (yank, cut) the next N lines, including the current line, into the buffer
p put (paste) the line(s) in the buffer into the text after the current line
touch
cat
cat> (single redirection)
cat >> (double redirect)
cp
mv
cmp
comm
Diff
touch command
Touch command is used in Unix to create an empty file.
Touch command is used to change the timestamps (access time and modification
time of a file).
cat command
Use of cat command:
'cat' command is used to display a file content.
Syntax:
$ cat data
This is an unix file
This is line two
$
cat with single redirection (cat> filename), creates a new file if no file with the
given file name already exists and overrites the file if a file with given file name
already exists.
cat with double redirection (cat >> filename) appends the content given through
standard input to the file.
cp command
cp command is used to copy one file's content to another file.
Syntax: $ cp [source filename] [destination filename]
Switches:
1. cp -i [sourcefilename] [destination filename] This command copies the content
of a file interactively.
mv command
mv command is used for:
1. for renaming a file in same directory.
2. for moving a file to a different directory.
Syntax:
$ mv [sourcefilename] [destinationfilename]
$ mv [source_directory] [destination_directory]
Directory Commands:
Every item in the UNIX filesystem tree is either a file, or a directory. A directory is
like a file folder. A directory can contain files, and other directories. A directory
contained within another is called the child of the other. A directory in the
filesystem tree may have many children, but it can only have one parent. A file can
hold information, but cannot contain other files, or directories.
The place in the file system tree where an user is located is called the current
working directory (which can be listed using pwd command).
As a user logs in, user is put in his/her home directory.
ls Command
Lists the contents of any directory (current directory if no directory specified).
Sorts entries without option
$ ls [option(s)] [filename]
Syntax :
Syntax:
Example: To show only lines common between file1 and file2, command is
diff - This command is used to compare two files line by line. The output indicates
how the lines in each file are different, and the steps that will be involved in
changing file1 to file2.
The change commands are in the format [range][acd][range]. The range on the left
may be a line number or a comma-separated range of line numbers referring to
file1, and the range on the right similarly refers to file2. The character in the
middle indicates the action i.e. add, change or delete.
‘LaR’ – Add lines in range ‘R’ from file2 after line ‘L’ in file1.
‘FcT’ – Change lines in range ‘F’ of file1 to lines in range ‘T’ of file2.
‘RdL’ – Delete lines in range ‘R’ from file1 that would have appeared at
line ‘L’ in file2
USAGE OF WinSCP
Introduction to WinSCP
WinSCP (Windows Secure CoPy) is a popular, free and open-source Secure File
Transfer application (SFTP) for Windows computers. It is used to transfer files
securely between remote server and local computer and vice verse. It includes
support for SCP file transfers, and has the ability to create secure tunnels via
SSH. Secure FTP (SFTP) is similar to FTP, but with SFTP the entire session is
encrypted, so that passwords are never sent in the clear text, and are therefore
much less vulnerable to interception. That is, for transferring files securely, we can
use Secure copy or SCP protocol, it uses Secure Shell (SSH), which encrypts the
content before transferring data over network. It runs over TCP port 22 by default.
Following are the Steps to be performed for Downloading file / Directory from
Server
Step - A1: Select the file to transfer in the Remote pane,
Step - A2: Right click → Select Copy or move option
Step - A3: Select the destination folder in the local machine (can be changed by
selecting Browse button)
Step - B3 : Select the desired location from drop down list and select copy
Saving sessions for future use:
All the session information can be saved for future use and faster access.
The information can be saved in following ways :
>> Server IP
>> User name,
>> Password(Not recommended)
>> Communication protocol
Filters
Filter is a programm in unix. It takes its input from another program, performs
some operation on that input, and writes the result to the standard output. Thus the
common use of filters is to modify or restructure output.
head
This command list the beginning of a file to standard output. The default is 10
lines, but a different number can be specified. The command has a number of
options.
Syntax:
Options:
-c Prints the first N bytes of file; with leading -, prints all but the last N bytes of the
file.
-n Prints first N lines; with leading - print all but the last N lines of each file.
Example: To display the first 10 lines of the file myfile.txt. $ head myfile.txt
tail
List the (tail) end of a file to stdout. The default is 10 lines, but this can be changed
with the -n option. Commonly used to keep track of changes to a system log-file,
using the -f option, which outputs lines appended to the file.
Syntax:
Example:
$ tail myfile
more
more command allows to view text files or other output in a scrollable
manner. When can command is used to view a very long file, all the output scrolls
off the top of your screen and only the last page can be viewed. more command
solves this problem by allowing the output of cat command one screenful of data at
a time.
Syntax:
Options:
-num This option specifies an integer which is the screen size (in lines).
-d more will prompt the user with the message "[Press space to continue, 'q' to
quit.]" and will display "[Press 'h' for instructions.]" instead of ringing the bell
when an illegal key is pressed .
-l more usually treats ^ L (form feed) as a special character, and will pause after
any line that contains a form feed. The -l option will prevent this behavior.
-p Do not scroll. Instead, clear the whole screen and then display the text.
tr
tr command automatically translates or substitute characters.
Syntax:
Example: To replace any occurrence of a by x, b by y and c by z in a given string
Output: aeaaaa
In the above example, except the character “e” other characters are replaced by a
fate
sort command reorders the lines of a file in ascending or descending order.
Syntax:
Options:
-n sort numerically
Below examples will help you to understand sort used with different options:
Example 1:
To sort on the 2 nd field of file named “list” we have to use the below command:
Output:
19
20
5
49
00
If we sort it as below:
$ sort list
Output is:
19
20
200
49
$ sort –n list
Output:
19
20
49
200
200
49
20
19
Syntax:
$ sort –u filename
Unix
Unix
Linux
Linux
Solaris
Axis
Axis
$ sort –u list
Output:
Unix
Linux
Solaris
Axis
uniq
uniq command is used to suppress the duplicate lines from a file. It discards all the
successive identical lines except one from the input and writes the output.
Syntax:
Options:
The default behavior of the uniq command is to suppress the duplicate line. Note
that, you have to pass sorted input to the uniq, as it compares only successive lines.
If the lines in the file are not in sorted order, then use the sort command and then
pipe the output to the uniq command.
Count of lines:
The -c option is used to find how many times each line occurs in the file. It
prefixes each line with the count.
You can print only the lines that occur more than once in a file using the -d
option. The -D option prints all the duplicate lines.
the -f option is used to skip the first N columns in comparison. Here the fields are
delimited by the space character.
Cut
This command is used for text processing. You can use this command to extract
portion of text from a file by selecting columns.
Syntax:
The following example displays 2nd character from each line of a file test.txt.
Range of characters can also be extracted from a file by specifying start and end
position delimited with -.
The following example extracts first 3 characters of each line from a file called
test.txt
Either start position or end position can be passed to cut command with -c option.
Following example extracts from 3rd character to end of each line from test.txt file.
The option -d specifies what delimiter that is used in the input file.
The following example displays only first field of each lines from / etc / passwd
file using the field delimiter : (colon). In this case, the 1st field is the username.
paste
This is the command for merging together different files into a single, multi-
column file. In combination with cut, useful for creating system log files.
Syntax:
join
This utility allows merging two files in a meaningful fashion, which essentially
creates a simple version of a relational database.
The command join operates on exactly two files, but pastes together only those
lines with a common tagged field (usually a numerical label), and writes the result
to standard output.
The files to be joined should be sorted according to the tagged field for the
matchups to work properly.
Example:
100 Shoes
200 Laces
300 Socks
$ cat file2
$ 100 40.0
200 $ 1.00
$ 300 2.00
Pipe
In unix, you can connect two commands together so that the output from one
program becomes the input of the next program. Two or more commands
connected in this way form a pipe. In shell the symbol '|' is used to represent pipe.
Purpose of Pipes :
Using pipe you can construct powerful unix command lines by combining basic
unix commands. UNIX commands are powerful; however by using pipe you can
combine them together, to accomplish complex tasks with ease.
Through the standard output of one command (the command to the left of the pipe)
gets sent as standard input to another command (the command to the right of the
pipe). Pipe functions in a similar manner like the output redirection in UNIX
(using> symbol to redirect the standard output of a command to a file. However,
the pipe is different because it is used to pass the output of a command to another
command, not a file.
Example:
You can combine many commands with pipes on a single command line. Here's an
example where the characters, words, and lines of the file apple.txt is sent to wc
and then the output of wc mailed to nobody@december.com with the subject line
"The count."
awk
awk is a scripting language which is used for processing or analyzing text files.
awk is used for grouping of data based on either a column or field, or on a set of
columns.
It derives its name from the first letter of the last name of its three authors namely
Alfred V. Aho, Peter J. Weinberger and Brian W. Kernighan.
awk can be used for reporting data in a useful manner. It searches one or more
files to see if they contain lines that match specified patterns and then perform
associated actions. awk is an advanced filter.
Simple awk Filtering
Syntax of awk:
BEGIN and END blocks are helpfull in displaying information before and after
executing actual awk script.
BEGIN block is evaluated before awk starts processing the actual awk script; it’s
an excellent place to initialize the FS (field separator) variable, print a heading, or
initialize other global variables.
Declaring variables.
Initialization variables for doing increment/decrements operations in main
AWK code.
Printing Headings/info before actual AWK code output.
END block is evaluated after all the lines in the awk script have been processed.
Typically, the END block is used to perform final calculations or print summaries
that should appear at the end of the output stream.
END block Usages:
awk tool is mainly used for reporting some data in useful manner. Without these
BEGIN and END
blocks the output will be meaningless.
Consider db.txt which contains below data:
Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122234 38 37
This is a block of code which is executed before executing actual awk script.
BEGIN block Syntax
awk ‘BEGIN{awk initializing code}{actual AWK code}’ filename.txt
Example: Print a meaning full info before actual AWK output.
~$ awk ‘BEGIN{print “########################\nThis is the output of
filtered
data\n########################”}{print $0}’ db.txt
Output:
##########################
This is the output of filtered data
##########################
Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122234 38 37
Edwin 253734 87 97 95
Dayan 24155 30 47
This is the block which is executed after executing all the awk code.
Example:
Jones 21 78 84 77
Gondrol 23 56 58 45
RinRao 25 21 38 37
Edwin 25 87 97 95
Dayan 24 55 30 47
#########################
Completed printing filtered data
#########################
Example:
Output:
#########################
This is the output of filtered data
#########################
Jones 21 78 84 77
Gondrol 23 56 58 45
RinRao 25 21 38 37
Edwin 25 87 97 95
Dayan 24 55 30 47
########################
Completed printing filtered data
awk is supplied with good number of built-in variables which comes in handy
when working with data files. We will see usages of awk built-in variables with
one or two examples . These variable are used to format the output of an awk
command.
This variable is useful in storing the input field separator. By default AWK can
understand only spaces, tabs as input and output separators. But if your file
contains some other character as separator other than these mention one’s, awk
cannot understand them.
Output:
John,29,MS,IBM,M,Married
Barbi,45,MD,JHH,F,Single
Mitch,33,BS,BofA,M,Single
Tim,39,Phd,DELL,M,Married
Lisa,22,BS,SmartDrive,F,Married
OFS (Output Field Separator)
This variable is useful for defining the output field separator for the expected
output data.
Example:
Display only 1st and 4th column and with $ as field separator for the output .
~$ awk ‘BEGIN{FS=”,”;OFS=” $ “}{print $1,$4}’ db.txt
Output:
John $ IBM
Barbi $ JHH
Mitch $ BofA
Tim $ DELL
Lisa $ SmartDrive
Note: Space is give before and after $ in OFS variable to show better output.
RS (Row separator)
Row Separator is helpful in defining separator between rows in a file. By default
awk takes row separator as new line. We can change this by using RS built-in
variable.
Example:
Convert a sentence to a word per line. We can use RS variable for doing it.
~$ echo “This is how it works” | awk ‘BEGIN{RS=” ”}{print $0}’
Output:
This
is
how
it
Works
Example:
Print all the company names in single line which are in 4th column.
~$ awk -F’,’ ‘BEGIN{ORS=” “}{print $4}’ db.txt
Output:
IBM JHH BofA DELL SmartDrive
NF
This variable keeps information about total fields in a given row. The final
value of a row can be represented with $NF.
Output:
5
5
4
5
4
NR
This variable keeps the value of present line number. This will come handy when
you want to print line numbers in a file.
Example:
Print line number for each line in a given file.
~$ awk ‘{print NR, $0}’ abc.txt
Output:
1 Jones 2143 78 84 77
2 Gondrol 2321 56 58 45
3 RinRao 2122234 38 37
4 Edwin 253734 87 97 95
5 Dayan 24155 30 47
This can be treated as cat command -n option for displaying line number for a file
FNR
This variable keeps count of number of lines present in a given file/data. This will
come handy when
you want to print no of line present in a given file. This command is equivalent to
wc -l command.
Example:
Print total number of lines in a given file.
~$ awk ‘END{print FNR}’ abc.txt
Output:
5
FILENAME
This variable contain file awk command is processing.
Example:
Print filename for each line in a given file.
~$ awk ‘{print FILENAME, NR, $0}’ abc.txt
Output:
abc.txt 1 Jones 2143 78 84 77
abc.txt 2 Gondrol 2321 56 58 45
abc.txt 3 RinRao 2122234 38 37
abc.txt 4 Edwin 253734 87 97 95
abc.txt 5 Dayan 24155 30 47
Arithmetic Functions
Nine of the built-in functions can be classified as arithmetic functions. Most of
them take a numeric argument and return a numeric value. Below table
summarizes these arithmetic functions with some Examples.
Examples:
~$ awk 'BEGIN{
print sqrt(16);
print sqrt(0);
print sqrt(-12);
}'
Output:
4
0
nan
Here nan stands for not a valid number.
String Functions
The built-in string functions are much more significant and interesting than the
numeric functions. Because awk is essentially designed as a string-processing
language, a lot of its power derives from these functions. Below table lists the
string functions found in awk. awk's Built-In String Functions
Shell Programming
The UNIX shell program interprets user commands, which are either directly
entered by the user, or which can be read from a file called the shell script or shell
program. Shell scripts are interpreted, not compiled. The shell reads commands
from the script line by line and searches for those commands on the system.
Shell script
In a script we can use conditional tests, such as value A is greater than value B,
loops or iterative statements to execute some steps repetitively or to navigate
through a list of data or records and do processing. We can use files to read and
store data. Can use variables to read and store data. A script may include functions
also.
When a script is executed, the shell reads the commands one by one and executes
them.
We can create the simple shell script file by using vi editor or cat command like,
$ vi test.sh
$ cat> test.sh
#! / bin / sh
Consider the shell script with just two commands pwd & ls.
$ cat test.sh
#!/bin/bash
pwd
ls
Shell scripts are basically used for automating processes that we repeat at the
prompt .
Find out all logged in users and what they are doing.
Find out if all necessary network services are running or not. For example if
web server failed then send an alert to system administrator via a pager or an
email.
Find out all failed login attemp. If login attempt are continued repeatedly
from same network IP, automatically block all those IPs accessing your
network/service via firewall.
$ sh filename
Or
$ ./filename
In this case we have to modify the file access permissions of the shell script before
execution.
Relational Operators.
Boolean Operators.
String Operators.
Example:
#!/bin/sh
val=`expr 2 + 2`
Output:
$ Total value : 4
There must be spaces between operators and expressions for example 2+2 is
not correct, where as it should be written as 2 + 2.
Arithmetic Operators
It is very important to note here that all the conditional expressions would be put
inside square braces with one spaces around them, for example [ $a == $b ] is
correct where as [$a==$b] is incorrect.
Relational Operators
Below are relational operators which are specific to numeric values. These
operators would not work for string values unless their value is numeric.
For example, following operators would work to check a relation between 10 and
20 as well as in between "10" and "20" but not in between "ten" and "twenty".
-eq Check if the values of 2 operands are equal or not, [ $a -eq $b ] is false
if yes then condition becomes true.
-ne Check if the values of 2 operands are equal or not, [ $a -eq $b ] is true
if values are not equal then condition becomes true.
-gt Check if the value of left operand is greater than the [ $a -gt $b ] is false
value of right operand, if yes then condition
becomes true.
-lt Check if the value of left operand is less than the [ $a -lt $b ] is true
value of right operand, if yes then condition
becomes true.
-ge Check if the value of left operand is greater than or [ $a -ge $b ] is false
equal to the value of right operand, if yes then
condition becomes true.
-le Check if the value of left operand is less than or [ $a -le $b ] is true
equal to the value of right operand, if yes then
condition becomes true.
It is very important to note here that all the conditional expressions would be put
inside square braces with one spaces around them, for example [ $a <= $b ] is
correct where as [$a <= $b] is incorrect.
Boolean Operators
String Operators
These are string operators. Assume variable a holds "abc" and variable b holds
"efg" then:
Assume a variable file holds an existing file name "test" whose size is 100 bytes
and has read, write and execute permission on:
Wild
card Meaning Examples
/Shorthand
Matches any string or $ ls * will show all files
group of characters. $ ls a* will show all files whose first name is
starting with letter 'a'
*
$ ls *.c will show all files having extension .c
$ ls ut*.c Will show all files having extension .c
but file name must begin with 'ut'.
? Matches any single $ ls ? will show all files whose names are 1
character. character long
$ ls fo? will show all files whose names are 3
character long and file name begin with
fo
[...] Matches any one of the $ ls Will show all files beginning with
enclosed characters [abc]* letters a,b,c
Example:
$ ls /bin/[a-c]*
Output:
The Metacharacters
Unix Shell provides various metacharacters which have special meaning while
using them in any Shell Script and causes termination of a word unless quoted.
Example:
? Matches with a single character while listing files in a directory and an * would
match more than one characters.
Here is a list of most of the shell special characters (also called metacharacters):
A character may be quoted (i.e., made to stand for itself) by preceding it with a \.
Example:
Hello ./test.sh: line 2: Word: command not found shell returned 127
Now let us try using a quoted character:
Hello; Word
I have $1200
Quote Description
Single quote All special characters between these quotes lose their special meaning.
Double quote Most special characters between these quotes lose their special meaning
with these exceptions:
\$
\'
\"
\\
Backslash Any character immediately following the backslash loses its special
meaning.
Back Quote Anything in between back quotes would be treated as a command and
would be executed.
There is an easy way to quote a large group of characters. Put a single quote ( ') at
the beginning and at the end of the string:
Any characters within single quotes are quoted just as if a backslash is in front of
each character. So now this echo command displays properly.
If a single quote appears within a string to be output, you should not put the whole
string within single quotes instead you would precede that using a backslash (\) as
follows:
Try to execute the following shell script. This shell script makes use of single
quote:
VAR=ZARA
So this is not what you wanted to display. It is obvious that single quotes prevent
variable substitution. If you want to substitute variable values and to make invert
commas work as expected then you would need to put your commands in double
quotes as follows:
VAR=ZARA
Putting any Shell command in between back quotes would execute the command
Syntax: var=`command`
Example:
Following would execute date command and produced result would be stored in
DATA variable.
While writing a shell script, there may be situations when you need to adopt one
path out of many available paths. In such cases you need to make use of
conditional statements that allow your program to make correct decisions and
perform right actions.
Unix Shell supports conditional statements, which are used to perform different
actions based on different conditions.
If..else statements
We can use “if..else” statement which can be used as decision making statement to
select an option from a given set of options.
if...fi statement
if...else...fi statement
if...elif...else...fi statement
Syntax:
if...fi statement
if [condition]
then
command(s)
fi
if...else...fi statement
if [ condition(s) ] then
command(s)
else
command(s)
fi
if...elif...else...fi statement
if [ condition(s) ]
then
command(s)
then
command(s)
else
command(s)
fi
We can use test command as condition of if condition as used in the below script.
$cat if_test.sh
#!/bin/sh
read ans
then
exit
else
fi
case...esac Statement
We can use multiple if...elif statements to perform a multiway branch. However,
this is not always the best solution, especially when all of the branches depend on
the value of a single variable.
Unix Shell supports case...esac statement which handles exactly this situation, and
it does so more efficiently than repeated if...elif statements.
The interpreter checks each case against the value of the expression until it finds a
match. If nothing matches, goes with the default condition.
Syntax:
case word in pattern1)
;;
pattern2)
;;
pattern3)
;;
esac
Here string word is compared against every pattern until a match is found and the
statement(s) following the match pattern executes. If shell cannot find any match,
the case statement exits without performing any action. When statement(s) part is
executed. The command ;; indicates program flow should jump to the end of the
entire case statement.
Example:
#!/bin/sh
COURSE=”DB”
case “$COURSE” in
;;
;;
;;
esac
Output:
Oracle is a DB
Iterative Statements/Loop :
Loops are a powerful programming tool that enables you to execute a set of
commands repeatedly.
while loop
for loop
until loop
while loop
Syntax:
while condition
do # executed as long as the condition is true
command(s)
done
Example:
a=0
while [ $a -lt 3 ]
do
echo $a
a=`expr $a + 1`
done
Output:
until loop
Syntax:
do
command(s)
done
Example:
a=0
until [ ! $a -lt 3 ] do
echo $a a=`expr $a + 1`
done
Output:
for loop
For loop operate on lists of items. It repeats a set of commands for every item in a
list.
Syntax:
do
done
Example:
for var in 0 1 2 3 4 5
do
echo $var
done
Output:
0
1
Example:
do
cat ${filename}
done
String Handling
String handling with expr command
The expr is quite handy for finding the length of a string and extracting a sub-
string:
$ str=”abcdefghijk” ;
$ echo $n
11
expr gave how many times any character (.*) occurs. This feature is very useful in
validating data entry.
Extracting a sub-string:
$ str=”abcdefghijk” ;
gh
Note that there are 6 dots preceding the sequence \(..\). This advanced regular
expression signifies that the first six characters of the string are to be ignored and
extraction should start from the 7th character. Two dots inside \(..\) suggests that
this extraction is limited to two characters only (backslashes override the usual
interpretation of ‘()’).
$ str="abcdefghijk"
cdefghijk
Below will give the last occurrence of character 'a' from string str.
9.
6. Command Line Arguments
To make a shell script a generalized script, we should avoid hard coding. User
should be able to provide the values required for processing as input while running
the shell script. To facilitate this we have to use command line arguments.
The statement we write in the command prompt starting with the command
followed by list of arguments is the command line argument.
Example:
A set of shell variables are there to represent the command line arguments.
$0 – represents the command name (first word on command line). For above
example “ls”
$1 - the first argument of command (second word/symbol ).For the above example
dir1
$2 – the second argument to the command. For the above example, dir2
$cat test.sh
#!/bin/sh
Let’s execute the above shell script “test.sh”. Remember to change File access
permission of script to execute
To get input from the keyboard, you use the read command. The read command
takes input from the keyboard and assigns it to a variable.
Example: read.sh
#!/bin/sh
read text
Note that “-n” given to the echo command causes it to keep the cursor on the same
line; i.e., it does not output a carriage return at the end of the prompt.
Next, we invoke the read command with “text” as its argument. What this does is
wait for the user ro type something followed by a carriage return (the Enter key)
and then assign whatever was typed to the variable text.
Execution:
$sh read.sh
Let us assume, we have one shell script which requires exactly 2 arguments to
execute. Shell script should throw proper error message in case user has not given
exactly two arguments.
Let us have a small shell script "test.sh" to show how to implement validations.
#!/bin/sh
if [ $# -ne 2 ]
then
echo " Not sufficient arguments, please give exact 2 arguments"
else
$ ./test.sh
$ sh test.sh abc
For scripts where input expected is a file , validations can be incorporated for file
tests like whether the file given as input exists or not, whether it is empty or not,
whether it is readable or not and so on as per requirements.
Environment Variables:
Values of some of these variables are set by the system, some can be set by the
user, some of them by the shell, or any program that loads another program.
When a user logs in to the system, the shell undergoes a twostep process
called initialization to set up the environment. This process involves the shell
reading two files - / etc / profile and .profile in the home directory of the user.
The file / etc / profile is maintained by the system administrator of the Unix
machine and contains shell initialization information required by all users on a
system.
The file .profile is under the control of the user. User can add as much shell
customization information as he / she wants to this file. The minimum set of
information that a user need to configure includes -
When a process is created, UNIX opens 3 streams stdin / stdout / stderr for basic
communication with respect to the process control terminal. In addition, it knows
the current working directory for performing file I / O.
The shell does not directly use this table, but it creates a child process and calls
exec() system call to execute a command program that uses the table inherited
from the shell parent. Children inherit the entire execution environment from the
parent.
Some examples of the environment variables are the USER, LOGNAME, HOME,
PATH, PS1, PS2, TERM MAIL, etc.
The HOME Variable
It specifies an associated directory with every user in a UNIX system. If the
HOME variable for the user Sita contains /usr/sita/stores, every time Sita logs in,
she is taken to the directory stores.
The variable HOME is referenced the same way:
$ echo ${HOME}
The PATH Variable
contains a list of all full path-names (separated by a colon) of directories that are to
be searched for an executable program. For example, the command
$PATH=.:/usr/bin:/bin specifies directories to be searched for any executable file
or a command file (current directory, /usr/bin and /bin, in that order).
The PS1 Variable
The system prompt may be changed by setting the value of this variable to the
desired prompt:
$ PS1=“Hello>”
Hello> #can be changed only at the UNIX command line, not within a shell
script.
The PS2 Variable: prompt string for continued command line (default ‘> ‘).
The LOGNAME Variable
contains user’s login name. Its contents cannot be changed by the user, but can be
displayed:
echo “${LOGNAME}”
The TERM Variable
Names the kind of terminal you are using; setting it helps to manage your screen
more effectively, say,
$ TERM=vt100
The PWD Variable
The current working directory can be displayed:
echo “${PWD}”
In fact the whole environment table can be displayed.
$IFS: String of characters which are used as word separators in command line
( space, tab, newline chars).
The MAIL Variable
Names the standard file where your mail is kept
Process state:- The state may be new, ready, running, waiting, halted, and so on.
Program counter:- The counter indicates the address of the next instruction to be
executed for this process.
CPU registers:- The registers vary in number and type, depending on the
computer
architecture. They include accumulators, index registers, stack pointers, and
general-purpose registers, plus any condition-code information.
CPU-scheduling information:- This information includes a process priority,
pointers to scheduling queues, and any other scheduling parameters.
Memory-management information:- This information may include such
information as the value of the base and limit registers, the page tables, or the
segment tables, depending on the memory system used by the OS.
Accounting information:- This information includes the amount of CPU and real
time used, time limits, account numbers, job or process numbers, and so on.
I/O status information:- This information includes the list of I/O devices
allocated to the process, a list of open files, and so on.
Process state:
A process may present in one of the following state.
New:- The process is being created or just created.
Ready:- The process is waiting to be assigned/allocated to a processor time.
Running:- Instructions of the process are being executed by processor.
Waiting:- The process is waiting for some other event/process to execute.
Terminated:- The process has finished its execution.
ps command:
The ps (i.e., process status) command is used to provide information about the
currently running processes in the system.
When ps is used without any option four columns of information labeled PID,TTY,
TIME and CMD for at least two processes, the shell and ps will be visible.
PID :- The process are identified by a 5 digit number known as PID (Process
Identification Number).
TTY :- Is the name of the console or terminal that the user logged into (Stands for
terminal type now
but originally stood for teletype).
TIME :- Is the amount of CPU time in minutes and seconds that the process has
been running.
CMD :- is the name of the command that launched the process.
The information that ps -aux command provides about each process are :
Types of process:
Orphan Process
When a child process is killed, parent process gets the notification via a signal.
Parent then, can continue other task. However if the parent process is killed before,
its child, is called an orphan process.
Zombie Process
When a process finished its execution and exit status not received by the parent ( or
parent did not read the exit status till now), the process state becomes zombie.
The process is dead (not to be scheduled for further execution) but cannot
becompletely removed from process table, until it has been determined that exit
status is no longer needed.
Daemon Process
Some programs are not designed to be run with continuous user input and
disconnect from the terminal when task completed. For example, a web server
responds to web requests, rather than user input. Mail servers are another example
of this type of application. These types of programs are known as daemons.
Starting a Process:
Process running in the background can be taken into foreground using the
following steps:
>> Find the job id of the process by the command jobs
>> Use fg %to get the job to foreground
Stopping/Killing a Process
Use the ps command to find out the process-id for the process
Use the command kill to terminate it
Use the command kill -9 to kill a process forcefully.
While a process is executing/running , if the owner tries to log off the process
,process will get killed. Sometimes a job or command takes a long time to
complete and it is required the job to be completed without interruption. This
situation can be handled in two different ways:
Does not allow the use to log off
Continue execution in background even after the user logged off
Crontab
The word 'crontab' in UNIX stand for chron (chronograph) table, or time table for
Unix system.
The crontab is a special table where it is possible to specify commands and time or
interval to execute the command once or repeatedly. Commands can be any
executable programs, for example, a script can be written to take regular backup of
all the required tables in database. A crontab instruction can be created to execute
the script on the specified time. The cron daemon reads the table and executes the
commands at the times specified.
Syntax
Components of Crontab
The basic format of a crontab schedule consists of 6 fields, placed on a single line
and separated by spaces, formatted as follows:
The various fields and their utility are explained in the image below:
Example:
To schedule the same job for every minute, the cron entry will be as
follows:
* * * * * ~ / takeBackup.sh