Fundamentals of UNIX
-
Upload
tapas-ranjan-tripathy -
Category
Documents
-
view
218 -
download
0
Transcript of Fundamentals of UNIX
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 1/156
1
Fundamentals
of
UNIX
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 2/156
2
IndexTopic Page #
UNIX Operating System 3
Unix Commands 25
Files & Directories 34
Unix Utilities 61Process 99
Shell Programming 111
Reference 162Lab 163
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 3/156
3
Chapter 1
UNIX Operating System
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 4/156
4
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 5/156
5
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 6/156
6
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 7/156
7
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 8/156
8
Objectives
In this session, you learn about:• The functions of OS
• The history of Unix
• The features of UNIX• The Unix architecture
• Process management
• CPU scheduling
• Memory management
• File management
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 9/156
9
• OS is a system software
• OS can be defined as an organized collectionof software consisting of procedures for
operating a computer
1.OS provides an environment for execution of
programs
1.OS acts as an interface between the user andthe hardware of the computer system.
Operating System (OS)
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 10/156
10
• Operating system interacts with user in two ways1.Operating system commands
Enables user to interact directly with the
operating system.
• Operating system calls
Provides an interface to a running programand the operating system. System calls inUNIX are written in C.
Operating System
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 11/156
11
• Ken Thompson of AT&T Bell Laboratories designedUNIX in late 1960s
• Two versions of UNIX that emerged are AT&T Unixand BSD Unix
• In 1989, AT&T and Sun Microsystems joined togetherand developed system V release 4 (SVR4)
• Two of the main standards mainly in use are POSIX
(Portable Operating System Interface) and X/open standard. In 1988, MIT formed Xconsortiumdeveloped vendor-neutral Xwindow System.
History of UNIX
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 12/156
12
What is Linux?
1.An open-source UNIX like operating system
1.Initially created by Linus Torvalds for PCarchitecture
1.Ports exist for Alpha and Sparc processors
1.Developer community world-wide contribute
to its enhancement and growth
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 13/156
13
• Multi-user, multitasking, timesharing
• Portability
• Modularity
• File structure
• Security
• Strong networking support & advanced graphics
Features of UNIX
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 14/156
14
hardware
kernel
shell
ld
as
comp
cp
vied
grepwc
date
a.out
who
sh
sort
lsbanner...
Layered Architecture
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 15/156
15
• Unix system follows a layered approach. Ithas four layers
• The innermost layer is the hardware layer
• In the second layer, the kernel is placed
• The utilities and other application programsform the third layer
UNIX System Architecture
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 16/156
16
• Kernel is that part of the OS whichdirectly makes interface with thehardware system.
• Actions:1.Provides mechanism for creating and
deleting processes
2.Provides processor scheduling,memory, and I/O management
3.Provides inter-process
communication.
Kernel
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 17/156
17
• A utility program that comes with the
UNIX system.
• Features of Shell are:
1.Interactive Processing
2.Background Processing
3.I/O Redirection
4.Pipes5.Shell Scripts
6.Shell Variables
7.Programming Constructs
The Shell
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 18/156
18
• A process is a program in execution
• Several processes can be executed simultaneously
in a UNIX system.
• A process is generally created using the “fork( )”system call.
• The process that invokes the “fork( )” system call isthe parent process, and the newly created process iscalled the child process.
Process Management
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 19/156
19
• Unix uses round-robin scheduling tosupport its multi-user and time-sharingfeature.
• Round-robin fashion of scheduling isconsidered to be the oldest, simplest and
widely used algorithm.
• Every process is given a time slice (10-
100 millisec.)
CPU Scheduling
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 20/156
20
• UNIX uses a hierarchical file system with“/” as its root.
• Every non-leaf node of the tree is calledas a directory file.
• Every leaf node can either be a file, or anempty directory
File Management
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 21/156
21
dev bin tmp home etc lib usr
console
lp0
shls passwd
inittab bin
user1
user2
var
srcspool
File System
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 22/156
22
• File system is the structure in which files arestored on disk
• File in UNIX is sequence of bytes organized in the
form of blocks
• The size of each block is 512 bytes (depends on
architecture)
• Block size can be decided while creating the file
system structure
File System
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 23/156
23
Boot Block
Super Block
Inode Block
Data Block
Type of the file
Link counter
Uid, gid, size
Date and time of Creation
Date and time of access
Date and time of modification
:
:
Address of datablock
Address of datablock
::
Address of the addr block
Address of the addr block
Address of the addr block
File System Structure
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 24/156
24
BSD: Berkeley, BSD
Solaris: Sun Microsystems, Sys 5/BSD
Ultrix: Digital Equipment Corporation, BSD
OSF 1: Digital Equipment Corporation, BSD/sys 5
HPUX: Hewlett-Packard, Sys 5
AIX: IBM, Sys 5 / BSD
IRIX: Silicon Graphics, Sys 5
GNU/Linux: GNU, BSD/Posix
Common UNIX Flavours
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 25/156
25
• Broad classification of users
• root (most privileged)
• Non-root (less privileged)
1. Group
1. UNIX allows user IDs to be grouped
2. A single user ID can be member of multiple groups
• Differentiating users w.r.to file access
1. Owner
2. Group
3. Others
Types of UNIX Users
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 26/156
26
• User logs in with a valid user ID• User logs out to terminate the login
session
Working With UNIX
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 27/156
27
Summary
In this session, you learned about … • The functions of OS
• The History of Unix
• The features of UNIX
• The Unix Architecture
• Process management
• CPU Scheduling
• Memory management
• File management
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 28/156
28
Chapter 2
UNIX Commands
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 29/156
29
Objectives
In this session, you will learn to:1.Use the basic Unix commands
1.pwd
2.date
3.who
4.ls5.man
• Use “man” pages
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 30/156
30
• pwd
• Displays the current workingdirectory.
1.date
1.Displays the current date and time
Simple Commands
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 31/156
31
• who
• Displays the names of all the userswho have currently logged in
1.who am i
1.Displays the name of the current
user.
Simple Commands
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 32/156
32
• lsSyntax :ls [options] [file….]
options: -l list in long format
-a list all files including those
beginning with a dot-i list inode no of file in first column
-s reports disk blocks occupied by file
-R recursively list all sub directories
-F mark type of each file
-C display files in columns
Listing the Directory Contents
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 33/156
33
Meta Characters
MetaCharacters Purpose Example
* Match with one or more characters or none $ ls – l *.c file*? Match with any single character $ ls – l file?[…] Match with any single character within the
brackets$ ls – l file[abc]
; Command separator $ cat file1; cat file2
| Pipe two commands $ cat abc | wc( ) Group commandsUseful when the output of thecommand grouphas to be redirected
$ (echo “==== x.c ====”; cat x.c) > out
`command` Execute the command enclosed within back quotes. Useful when the output of a commandinto a variable in a shell script
count=`expr $count + 1`
assuming count has value3, this
increments the value of count „string‟ Quote all characters with no substitution(ex. no special meaning for $)
echo „expr $count + 1‟ displays expr $count + 1
“string” Quote all characters with substitution.The characters $, \ (back slash) and back quotehave special meaning.
echo “expr $count + 1” displays expr 3 + 1assuming the variable count has value 3
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 34/156
34
$ ls –l
total 6
-rwxr-xr-x 1 user1 projA 12373 Dec 15 14:45a.out
drwxr-xr-x 2 user2 projD 4096 Dec 22 14:00awkpro
-rw-r--r-- 1 user1 projA 12831 Dec 12 13:59
c-rw------- 1 user1 projA 61440 Dec 15 11:16
core
-rw-r--r-- 1 user3 projC 255 Dec 20 14:29cs
File type
File accesspermissions
Link countGroup id
Date & time ofmodification
Listing the Directory Contents
User id File sizein bytes
File name
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 35/156
35
• The Unix manual, usually called man pages, is available on-line
to explain the usage of the Unix system and commands.
Syntax:
• man [options] command_name
Common Options
-k keyword list command synopsis line for all keywordmatches
-M path path to man pages
-a show all matching man pages (SVR4)
• info command_name - help for commands
• help - –command_name – gives command synatx
Getting Help on Commands
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 36/156
36
Summary
In this session, you have learned to …
1.use the basic Unix commands like
1.pwd2.date
3.who
4.ls5.man
• use “man” pages
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 37/156
37
Chapter 3
Files & Directories
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 38/156
38
• In this session, you will learn to:1.set file permissions using the chmod
command
2.use directory-related commands namelymkdir, rmdir, cd commands
3.use file-related commands namely cp, mv,rm commands
4.access advanced file permissions usingcommands umask, suid, sgid, linking files,stickybit
5.create and edit files using the vi editor
Objectives
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 39/156
39
• Refers to the permissions associated with a file with
respect to the following
• Permission Levels
• User (owner) (u)
• Group (wheel, staff, daemon, etc.) (g)
• World (guest, anonymous and all other users) (o)
1. Permission Settings
1. Read (r)
2. Write (w)
3. Execute (x)
File Access Permissions
il A i i
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 40/156
40
• No read permission does not allow the user to:
• List the contents of directory
• Remove the directory
1. No Write permission does not allow the user to :
1. copy files to the directory2. remove files from the directory
3. rename files in the directory
4. make a subdirectory
5. remove a subdirectory from the directory6. move files to, and from the directory
File Access Permissions
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 41/156
41
• No execute permission does not allowthe user to:
– display the contents of a directory filefrom within the directory
– change to the directory
– display a file in the directory
File Access Permissions
Ch i P i i h d
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 42/156
42
• chmod u+x file_name
Syntax:chmod <category> <operation> <permission> <filename(s)>
or
chmod <octal number> filename
Octal Number
4 - for read
2 - for write
1 - for execution
$ chmod 744 xyz
this sets read, write and execute permissions for owner,read permission for group and others
Changing Permissions - chmod
Di C i
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 43/156
43
Command Syntax
mkdir [OPTION] DIRECTORY$ mkdir <path>/<directory>
$ mkdir – m <directory>
$ mkdir – p <directory1>/<directory2>/<directory3>
Example:
$ mkdir project1
This creates a directory project1 under current directory
Note: Write and execute permissions are needed for the
directory in which user wants to create a directory
Directory Creation
Di t R l
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 44/156
44
rmdir command removes directory
Syntax
– rmdir <directory name>
Example
Removes project1 directory in the current directory
– rmdir project1Remove multiple directories
rmdir pos1 pos2
Remove the directory recursively
rmdir –p dir1/dir2/dir3
rmdir removes a directory if it is empty and is not thecurrent directory
Directory Removal
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 45/156
45
Command - cd
cd command is used to change thedirectory
• cd - take to the home directory
• cd .. - takes to the parent directory
• cd / - takes to the root directory
Fil R l t d C d
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 46/156
46
File Operation
Copying a file
Moving a file
Removing a file
Displaying a fileand concatenating files
Command
cp
mv
rm
cat
File-Related Commands
C d
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 47/156
47
Used to copy files across directories
Syntax
cp <source file> <new file name>
Example
cp file1 file2
Command - cp
C d
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 48/156
48
Options to cp
1. -p• Copies the file and preserves the following attributes
1.owner id
2.group id
3.permissions4.last modification time
• -r
• recursive copy; copy subdirectories under the
directory if any1. -i
1.interactive; prompts for confirmation beforeoverwriting the target file, if it already exists
Command - cp
C d m
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 49/156
49
Used to move a file, or rename a file
Preserves the following details
• owner id
• group id• permissions
• Last modification time
-f suppresses all prompting (forces overwriting of target)
-i prompts before overwriting destination file
Command - mv
Command rm
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 50/156
50
Used to remove a file
1.Syntax : rm file(s)
-f suppresses all prompting
-i prompts before deleting destinationfile
-r will recursively remove the file from adirectory (can be used to delete adirectory along with the content )
Command - rm
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 51/156
51
$ ls –l-rwxr-xr-x 1 user1 training 12373 Dec 15 14:45 a.out-rwxr-xr-x 3 user1 faculty 4096 Dec 24 11:56 awkpro
$chown user2 a.out
$ls –l-rwxr-xr-x 1 user2 training 12373 Dec 15 14:45 a.out-rwxr-xr-x 3 user1 faculty 4096 Dec 24 11:56 awkpro
$ chgrp training awkpro
$ls –l-rwxr-xr-x 1 user2 training 12373 Dec 15 14:45 a.out-rwxr-xr-x 3 user1 training 4096 Dec 24 11:56 awkpro
Command – chown & chgrp
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 52/156
52
umask value is used to set the default permission of a file and
directory while creating
umask command is used to see the default mask for the filepermission
Default umask value will be set in the system environment filelike /etc/profile
umask 022 will set a mask of 022 for the current session
– The file permission after setting this umask value will be644
– And the directory permission will be 755
Command - umask
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 53/156
53
Linking files
1.Hard Link (in the same filesystem)
1.$ ln /usr/bin/clear /usr/bin/cls
1.Hard link uses the same inodenumber
• Soft Link (in different filesystems alsoused to link directories)
• $ ln –s /usr/bin/clear /home/user1/cls
Command - ln
S i l P i i Bit
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 54/156
54
• Set user ID (SUID)1.This means that if the SUID bit is set for any application then
your user ID would be set as that of the owner ofapplication/file rather than the current user, while running thatapplication
1.“set user ID” bit can be set in one of the two ways:
• chmod u+s <filename>
1.chmod 4755 <filename>
1.The leftmost octal number 4 indicates “set user ID” bitto be set, other octal digits indicate regular filepermissions. This is meaningful for executable files
only.
Special Permission Bits
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 55/156
55
• Set group id (SGID)1.Just like SUID, setting the SGID bit for a file sets your group
ID to the file's group while the file is executing
1.“set group ID” bit can be set in one of the two ways: • chmod g+s <filename>
1.chmod 2755 <filename>
1.The leftmost octal number 2 indicates “set group ID”bit to be set, other octal digits indicate regular filepermissions. This is meaningful for executable filesonly.
Special Permission Bits
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 56/156
56
• Sticky bit (SVTX)
1.Typically set to a directory that is shareable
2.Any user can create a file in such sharable directory
3.Only owner of the file or super user (root) can remove a filefrom the directory
1.“sticky” bit can be set in one of the two ways:
• chmod +t <directoryname>
1.chmod 1555 <directoryname>• The leftmost octal number 1 indicates “sticky” bit to be
set, other octal digits indicate regular file permissions.
Special Permission Bits
Vi Editor
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 57/156
57
• vi is a visual editor used to create and edit text files.
• A screen-oriented text editor
• Included with most UNIX system distributions
• Command driven
1. Categories of commands include
1.Cursor movement2.Editing commands
3.Search and replace commands
1. The vi editor is invoked by the following command:
$ vi filename
Vi Editor
Navigation
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 58/156
58
Backspace
h
Space
l j k
the quick brown fox
ww w
the quick brown fox
2 w
the quick brown fox
bb b
the quick brown fox
$
the quick brown fox
^
Navigation
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 59/156
59
• Text insertion / replacement
1.i - inserts text to the left of the cursor
2.a - inserts text to the right of the cursor
3.I - inserts text at the beginning of the line
4.A - appends text at end of the line5.o - opens line below
6.O - opens line above
7.R - replaces text from cursor to right
8.s - replaces a single character with any number ofcharacters
• S - replaces entire line
Editing Commands
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 60/156
60
• Deletion
1.x - to delete character at cursor position
2.3x - to delete 3 characters at cursor position3.dw - to delete word
4.2dw - to delete 2 word
5.dd - to delete a line
6.2dd - to delete 2 lines
Editing Commands
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 61/156
61
• Yanking
• Y - copy line into buffer
• 3Y - copy 3 lines into buffer
• p - copy buffer below cursor
• P - copy buffer above cursor
1. Save and quit
1. :w - to save
2.:w! - to name a file (:w! filename -> save as)3.:x - save and quit
4.:q - cancel changes
5.:q! - cancel and quit
Editing Commands
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 62/156
62
The following commands are applicable for vi
editor in Linux
/pat searches for the pattern pat
and places cursorwhere pattern occurs.
/ repeat last search
:%s/old/new/g to change every
occurrence in the whole file.
Search & Replace Commands
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 63/156
63
Summary
• In this session, you have learned how to … 1.use file permissions using the chmod
command
2.use directory-related commands namelymkdir, rmdir, cd commands
3.use file-related commands namely cp, mv,rm commands
4.access advanced file permissions usingcommands umask, suid, sgid, linking thefiles, stickybit
5.create and edit files using the vi editor
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 64/156
64
Chapter 4
UNIX Utilities
Obj ti
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 65/156
65
In this session, you will learn how to:
1.use the Unix utilities such as
1.cat, echo, touch, more, file, wc, cmp, comm, find
• employ redirection operators• use filters such as
• sort, grep, cut, head, tail, tr, and paste
1.use communication commands
1.telnet, ftp
2.use backup commands
1.zip/gzip and tar
Objectives
cat
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 66/156
66
• cat command takes the input from the keyboard, and
sends the output to the monitor
1. We can redirect the input and output using theredirection operators
$ cat > file1
Type the content here
press <ctrl d>
$ cat file1Displays the content of the file
$cat >> file1
This will append standard input to the content of file1
cat
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 67/156
67
• touch is used to change the time stamp of the file
Syntax:
touch [options] file
• Options:
• -a to change the access time
• -m to change the modification time
• -c no create if not exists
• touch <file> will change the time of change of the file if thefile exists
• If the file does not exist, it will create a file of zero byte size.
touch
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 68/156
68
• echo command is used to print output to the screenecho “This is an example”
This is an example
x=10
echo $x
10
• read command allows to read input from user and assign it
to the variable specified.
read x
echo & read
General Purpose Utilities
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 69/156
69
• more• Allows user to view one page-full of
information at a time.
1. file
• Used to display the type of the file
2. tty
1.Prints the terminal‟s name
General Purpose Utilities
General Purpose Utilities
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 70/156
70
• wc1.A filter used to count the number of lines,
words, and characters in a disk file or fromthe standard input.
2.-l - displays the number of lines3.-w - displays the number of words
4.-c - displays the number of characters
General Purpose Utilities
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 71/156
71
• Lets user to search set of files and directories based on various
criteria• Syntax: find [path...] [expression]
• [path]
• where to search
• [expression]
– What type of file to search (specified with –type option)
– What action to be applied ( –exec, –print, etc.)
– Name of the files (specified as part of –name option,enclosed in “ “)
1. Example
find . –name “*.c” -print
lists all files with .c extension from the current dir & itssubdirectories
find
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 72/156
72
• Finding files on the basis of file size
1. – size [+ –]n[bc]
n represents size in bytes (c) or blocks (b) of 512
bytes
find . –size 1000c lists all files that are exactly
1000 bytes in size
find . –size +1000c lists all files that are more than1000 bytes in size
find . –size –1000c lists all files that are less than
1000 bytes in size
find
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 73/156
73
• Finding files on the basis of access time (atime) or modified time
(mtime)
1. – atime [+-]n
2. – mtime [+-]n
n represents number of days ( actually 24 * n hours)
find . –atime 2 lists files accessed exactly 2 days ago
find . –atime +2 lists files accessed more than
2 days ago
find / –mtime –2 lists files modified less than 2
days ago
find
find
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 74/156
74
• Applying a command on files matching the criteria with –exec and –okoptions
1. – exec command {} \;
command is command to be applied on the matching files (does
not prompt user)
find . -name “*.dat” –exec ls –l {} \;
Long listing of all files with .dat extension in the current and its
subdirectories
– -ok command {} \;
Functionality is similar to –exec, but prompts user before applying
the command on the file matching the criteria.
find
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 75/156
75
• Standard Input file
• Keyboard, file descriptor is 0
1.Standard Output file
1.Monitor, file descriptor is 1
• Standard Error file
–Monitor, file descriptor is 2
Standard Files
I/O Redirection
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 76/156
76
< file redirect standard input from
file> file redirect standard output to file
2> file redirect standard error to file
2>&1 merge standard error with
standard output
$ cat > abc
$ ls –l > outfile
I/O Redirection
Filters
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 77/156
77
• Filters are programs that takes its input from thestandard input file, process it, and sends it to thestandard output file.
• Commonly used filter commands1.sort
2.grep
3.cut
4.head5.tail
6.paste
Filters
sort
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 78/156
78
Sorts the contents of the given file based on the first char of each line.
-n numeric sort (comparison made
according to strings numeric value)
-r reverse sort
-t specify delimiter for fields
+num specify sorting field numbers
+num [-num] to specify the range
sort
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 79/156
79
• grep -Global Regular Expression Printeris used for searching regular expressions
• Syntax
1.grep <options> <pattern><filename(s)>
grep
grep options
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 80/156
80
-c displays count of the number of occurrences
-n displays line numbers along with the lines
-v displays all lines except lines matching pattern
-i Ignores case for matching
grep options
P
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 81/156
81
* - matches 0 or more characters
[^pqr] - Matches a single character which is not p ,q or r
^pqr -Matches pqr at the beginning of the line
pqr$ -Matches pqr at the end of the line
“.” - Matches any one character
\ - ignores the special meaning. grep “New \[abc\ ]” filename
Patterns
Filt C d h d
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 82/156
82
Displays the first n lines of the file
$ head -3 file1
Filter Command - head
Filt C d t il
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 83/156
83
Displays the last n lines of a file
$ tail -3 file1
Can also specify the line number from which
the data has to be displayed till the end of file
$ tail +5 file1
Filter Command - tail
Filt d t
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 84/156
84
tr - translate filter used to translate a given set of characters
Example :
tr [a-z] [A-Z] < filename
This converts standard input read from lower case to uppercase.
option -s can be used to squeeze the repeated characters.
Filter command - tr
Filt d t
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 85/156
85
Useful options for tr
• -s char
Squeeze multiple contiguous occurrences of thecharacter into single char
• -d charRemove the character
Filter command - tr
C d Pi i
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 86/156
86
• Allows the output (only the standardoutput) of a command to be sent as inputto another command.
• Multiple pipes may appear in onecommand line.
Example:
$ cat * | wc
Command Piping
Filt C d t
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 87/156
87
• tee command allows the normal output tothe standard output, as well as to a file
• Useful to capture intermediate output of a
long command pipeline for furtherprocessing, or debugging purpose.
• Example• who | tee userlist
• cat - | tee file1 | wc -l
Filter Command – tee
Filt C d t
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 88/156
88
Used to extract specified columns of a text
Option remark
-c used to extract characters
-d Delimiter for fields
-f Field no.
Examples
$ cut -c2-5 file1
$ cut -d “|” -f2,3 file1
Filter Command – cut
ftp
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 89/156
89
• Ftp is a file transfer program
• Provides necessary user interface to the standard File Transfer
Protocol
• Allows users to transfer files to and from a remote host
• Syntax
$ ftp hostname
p
ftp - commands
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 90/156
90
• Ftp program supports the following commands
• get receive file from host
• mget receive multiple files from host
•
put send file to host• mput send multiple files from host
p
ftp - commands
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 91/156
91
• Ftp program supports the following commands as well
• ls list directory of host
• cd change directory on the host
•
lcd change directory on the local machine
• To set transfer format
• ascii set to ascii mode
• binary set to binary mode
p
ftp - commands
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 92/156
92
• Progress indication of transfer
• hash command
• Quitting ftp session
• byecommand
Tape Archive - tar
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 93/156
93
• Tar is an archiving utility to store and retrieve files from anarchive, known as tarfile.
• Though archives are created on a tape, it is common to havethem as disk files as well.
1. tar c|t|x [vf destination] source...
Tape Archive - tar
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 94/156
94
Examples:Create a new tar file containing all .dat files (assuming a.dat, b.dat and c.dat
exist)
$ tar – cf mytar *.dat
Compression Utilities
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 95/156
95
gzip,Usage is very similar to compress and pack utilities in Unix:
gzip [-vc] filename
where -v displays the compression ratio.
-c sends the compressed output to standard output and leaves
the original file intact.
gunzip
gunzip can uncompress files originally compressed with compress.
Summary
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 96/156
96
Summary• In this session, you have learned to:
1.use the Unix Utilities like1.cat, echo, touch, more, file, wc,
find
• employ redirection operators• use filters like
•sort, grep, cut, head, tail, tr, ftp
1.backup commands1.tar and zip/gzip
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 97/156
97
Chapter 5
Process
Objectives
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 98/156
98
In this session, you will learn to:
1.Use process-related commands like
1.ps, kill, sleep
• Start a background process
• Use background and foreground-relatedcommands like
• bg, fg, jobs , nice , nohup
j
Processes
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 99/156
99
• Process - a program in execution
• When program is executed, a new process is
created
• The process is alive till the execution of theprogram is complete
• Each process is identified by a number calledpid
Login shell
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 100/156
100
As soon as the user logs in, a process is createdwhich executes the login shell.
Login shell is set for each login in /etc/passwd
file.
ps
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 101/156
101
• The ps command is used to display thecharacteristics of a process
• It fetches the pid, tty, time, and the command
which has started the process.1.-f lists the pid of the parent process also.
2.-u lists the processes of a given user
3.-a lists the processes of all the users4.-e lists all the processes including the
system
processes
Background Process
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 102/156
102
• Enables the user to do more than one task at atime.
• If the command terminates with an ampersand
(&), UNIX executes the command in the
background
• Shell returns by displaying the process ID
(PID) and job id of the process
Background Process
Controlling Background
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 103/156
103
• jobs
• List the background process
1. fg % <job id>
• Runs a process in the foreground
2. bg %<job id>
1.Runs a process in the background
Processes
The kill Command
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 104/156
104
• kill: Kills or terminates a process
• kill command send a signal to the process
• The default signal is 15 ( SIGTERM)
1. kill -9 (SIGKILL)
1. Terminates the process abruptly
The kill Command
Summary
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 105/156
105
Summary• In this session, you learned to:
1.Define a process
2.Use process-related commands like
1.ps, kill, sleep
3.Start a background process
4.Use background and foreground-related commands like
1.bg, fg, jobs
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 106/156
106
Chapter 6
UNIX Shell Programming
Objectives
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 107/156
107
• In this session, you will learn to:
1.Use Shell variables2.Write scripts to process positional parameters
3.Use “test” command
4.Use “if” construct 5.Use “for” loop
6.Use “while” loop
7.Use “case” construct8.Define and use functions
9.Debug shell scripts
Flavours of the Unix shell
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 108/156
108
• Bourne shell sh
• C shell csh
• Korn shell ksh
• Bourne again shell bash
(shell distributed with linux)
Command processing
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 109/156
109
1.Displays the shell prompt and reads the commandtyped by the user.
1.Interprets the command and classifies it as aninternal (built-in), or an external command.
1.If it is NOT a built-in command, searches for thecommand in the PATH-specified directories, andexecutes that command if it is found.
Shell Features
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 110/156
110
Parent shell process
(bash)
User
Mode
Kernel
Mode
$ vi test.c
command typed by
user
Child shell process(bash)
fork
Exec of “vi test.c”
Additional Shell Features
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 111/156
111
1.Each shell, apart from the basic features,provides additional features such as:
–Maintaining command history (C, kornand bash)
–Renaming (aliasing) a command (C,korn, bash)
History
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 112/156
112
• some UNIX shells support command history
• facility to keeps track of commands that were executed
• facility to rerun previously executed commands
• bash shell supports the following
!! recall the last command and execute it.
!num execute nth command where n is the
the num specified after !
alias
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 113/156
113
• alias can be used to give new name to an existing command• A better name that represents a single command or a sequence of
commands to be executed, often with appropriate options
• alias is an internal command
alias newname=command
$ alias l=„ls –l‟
The unalias command cancels previously defined alias.
Shell Programming
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 114/156
114
• Allows
1.Defining and referencing variables2.Logic control structures such as if,
for, while, case
3.Input and output
Shell Programming
Shell Variables
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 115/156
115
• A variable is a name associated with a data value,
and it offers a symbolic way to represent andmanipulate data variables in the shell. They areclassified as follows
1.user-defined variables2.environment variables
3.predefined variables
• value assigned to the variable can then be referredto by preceding the variable name with a $ sign.
Shell Variables
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 116/156
116
• The shell provides the facility to definenormal, and environment variables.
• A normal variable can be only used in theshell where it is defined.
• An environment variable can be used inthe shell where it is defined, plus any childshells invoked from that shell.
Using Normal Variables
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 117/156
117
To define a normal variable, use the following syntax:
• variable_name=value
Examples:
x=10
textline_1=„This line was entered by $USER‟
textline_2=“This line was entered by $USER”
allusers=`who`
usercount=`who | wc –l`
Using Normal Variables
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 118/156
118
• Once variables are defined, one canuse the echo command to display thevalue of each variable:
$ echo $x$ echo $textline_1
$ echo $textline_2
$ echo $allusers$ echo $usercount
Using Environment Variables
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 119/156
119
• To define an environment variable, usefollowing syntax:
variable_name=valueexport variable_name
• Examples:1.$ x=10; export x
2.$ allusers=`who` ; export allusers
Built-in environment variables
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 120/156
120
•PATH
1.BASH_E
NV
2.HOME
3.PWD
4.SHELL5.TERM
1.USER
2.LOGNAME
3.PS1
4.PS2
Sample Shell Script
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 121/156
121
#! /bin/bash# The above line has a special meaning. It
must be the
# first line of the script. It says that thecommands in
# this shell script should be executed by the
bash# shell (/bin/bash).
# ---------------------------------------------------------
------
Executing Shell Scripts
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 122/156
122
There are two ways of executing a shellscript:
By passing the shell script name as an
argument to the shell. For example:
sh script1.sh
If the shell script is assigned executepermission, it can be executed using it‟sname. For example:
Passing Parameters to Scripts
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 123/156
123
• parameter can be passed to a shell script
• parameters are specified after the name of the
shell script when invoking the script.
• Within the shell script, parameters arereferenced using the predefined variables $1through $9.
• In case of more than 9 parameters, otherparameters can be accessed by shifting.
Built-in variables
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 124/156
124
• Following are built-in variables supported
1.$0, $1…$9 - positional arguments
2.$* - all arguments
3.$@ - all arguments
4.$? - exit status of previous command
executed
5.$$ - PID of the current process
6.$! - PID of the last background
process
Passing Parameters to Scripts
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 125/156
125
• Consider following shell script:
----------------------script2.sh--------------------------
echo “Total parameters entered: $#”
echo “First parameter is : $1”
echo “The parameters are: $*”
shift
echo “First parameter is : $1”
------------------------------------------------------------
• Execute the above script using the “script2.shthese are the parameters” command.
ass g a a ete s to Sc pts
Passing Parameters to Scripts
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 126/156
126
• The shell parameters are passed as strings.
• to pass a string containing multiple words as asingle parameter, it must be enclosed within
quotes.
• For example,
$ ./script2.sh “this string is a singleparameter”
g p
Doing Arithmetic Operations
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 127/156
127
• Arithmetic operations within a shell script can
be performed using expr command.
• Example,
x=10
y=5
number_1 = `expr $x + $y`
number_2 = `expr $x - $y`
number_3 = `expr $x / $y`
number_4 = `expr $x \* $y`
number_5 = `expr $x % $y`
g p
Using the test Command
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 128/156
128
The general syntax of test command is:
test expression
• The expression can be formed using acombination of shell variables and theoperators supported by the test command.These operators provide facility to comparenumbers, string and logical values, file typesand file access modes.
Using the test Command
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 129/156
129
To compare two integers using test following operators are available:
-eq (equal to)
-ne (not equal to)
-lt (less than)
-le (less than or equal to)-gt (greater than)
-ge (greater than or equal to)
Using the test Command
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 130/156
130
• General syntax
test expression
or[ expression ]
test integer1 operator integer2OR
[ integer1 operator integer2 ]
Using the test Command
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 131/156
131
To compare two strings using the test command,following operators are available:
1.string1 = string2 (equal to, please note it is asingle =)
2.string1 != string2 (not equal to)
3.string1 (string is not NULL)4.-n string1 (string is not NULL and exists)
5.-z string1 (string is NULL and exists)
Using the test Command
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 132/156
132
The syntax for this string comparison is:
test string1 operator string2
OR
[ string1 operator string2 ]
OR
test operator string
OR
[ operator string ]
Using the test Command
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 133/156
133
To check a file type/access permissions
using the test command, followingoperators are available:
–-s file (file is not empty and exists) –-f file (Ordinary file and exists)
–-d file (file is a directory and exists)
–-r file (file is readable and exists)
–-w file (file is write-able and exists)
–-x file (file is executable and exists)
Using the test Command
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 134/156
134
To check a file type/access permissions using
the test command, following operators areavailable:
– -b file (file is a block device and exists) – -c file (file is a character device and exists)
– -p file (file is a named pipe and exists)
– -g file (file has sticky bit set)
– -u file (file has setuid bit set)
– -t file_des (file descriptor is standardoutput)
Combining Conditions
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 135/156
135
• It is possible to combine conditions byusing following operators:
1.-a (logical AND operator)2.-o (logical OR operator)
3.! (logical NOT operator)
Combining Conditions
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 136/156
136
The syntax for this is:
test expression_1 –a expression _2,OR
[ expression _1 –a expression _2 ]
test expression_1 –o expression _2,OR
[ expression_1 –o expression_2 ]
test ! expression _1OR
[ ! expression _1 ]
Condition Checking in Scripts
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 137/156
137
Bash shell provides the if command to test if a condition istrue. The general format of this command is:
if condition
thencommand
fi
The condition is typically formed using the test command.
Example
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 138/156
138
# to check if the current directory is the same as
your home directorycurdir=`pwd`
if test “$curdir” != “$HOME”
thenecho your home dir is not the same as yourpesent working directory
else
echo $HOME is your current directory
fi
Checking Multiple Conditions
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 139/156
139
The complex form of if statement is as follows:
if condition_1
then
commandelif condition_2
then
commandelse
command
fi
Using for Loop
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 140/156
140
Example:
for i in 1 2 3 4 5
doecho -n $i \* $i
= " "
echo `expr $i \* $i `
done
The Bash shell provides a for loop.
The syntax of this loop is:
for variable in listdo
command
…
commanddone
Example
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 141/156
141
----------------------script.sh--------------------------
#! /bin/sh
usernames=`who | cut –d “ “ –f1`
#
for user in ${usernames}do
echo $user
done-----------------------------------------------------------
-
Using while Loop
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 142/156
142
The Bash shell provides a while loop. The syntaxof this loop is:
while conditiondo
command
… command
done
Example
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 143/156
143
Shell script checks for a blank/non blankstring
eg: read nam
while [ -z “$nam” ]
do
read nam
done
echo the string is $nam
Shell script to compute factorial of a given number
Example
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 144/156
144
Shell script to compute factorial of a given number
#!/bin/bashn=$1if [ $n -eq 0 ]; thenfact=0
elsefact=1while [ $n –ne 0 ]do
fact=`expr $fact \* $n`
n=`expr $n – 1`done
fiecho $fact
The case Statement
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 145/156
145
The structure of case statement
case value in
pattern1)
command
command;;
pattern2)
command
command;;
patternn)command;;
esac
Example
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 146/156
146
#!/bin/bash
echo enter 2 nos
read num1
read num2
echo “enter 1 (for
addition) or 2 (for
subtraction)
read choice
(contd.)
case $choice in
1) res=`expr $num1 + $num2`
echo result is $res;;
1) res=`expr $num1 - $num2`
echo result is $res;;
*) echo invalid input;;
esac
#!/bi /b h
Example
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 147/156
147
#!/bin/bash
read numbercase $number
1) echo 1st
break;;
2) echo 2ndbreak;;
3) echo 3rd
break;;
*) echo ${number}th
break;;
esac
Functions
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 148/156
148
Shell functions are a way to groupcommands for later execution using a singlename for the group. They are executed just
like a "regular" command.
Shell functions are executed in the currentshell context; no new process is created tointerpret them. Functions are declared usingthis syntax:
Functions
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 149/156
149
• Shell functions can accept arguments
• Arguments are passed in the same way asgiven to commands
• Functions refer to arguments using $1, $2etc., similar to the way shell scripts refer tocommand line arguments
Functions
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 150/156
150
Function to convert standard input intoupper case
toupper(){
tr a-z A-Z
}
This function can be used as
Debugging Shell Scripts
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 151/156
151
• Two options help in debugging shell scripts
– “-v” (verbose) option:
causes shell to print the lines of the script as theyare read.
$ bash –v script-file
– “-x” (verbose) option:
prints commands and their arguments as they areexecuted.
$ bash –x script-file
Programming in C vs Shell
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 152/156
152
Comparison between
• A solution in C
• A shell solution written like a Cprogram
• A proper “shell/unix” solution
e.g:
The problem is to count the no of lines in afile ( the file is called the_file)
A solution in C A shell solution
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 153/156
153
#include <stdio.h>
void main(void){
int lcount=0;
FILE *infile;
char line[500];
infile=fopen("the_file","r");
while(!feof(infile))
{
fgets(line,500,infile);
lcount ++;}
printf("no of lines is%d\n",lcount);
}
count=0
while read linedo
count=`expr $count + 1`
done < the_file
echo Number of lines is $count
Solution using existing
commands
$ wc – l the_file
Summary
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 154/156
154
• In this session, you have learned to:
1.Use Shell variables
2.Write scripts to process positional parameters
3.Use “test” command
4.Use “if” construct
5.Use “for” loop
6.Use “while” loop
7.Use “case” construct
8.Define and use functions
9.Debug shell scripts
UNIX Bibliography
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 155/156
155
UNIX in a Nutshell for BSD 4.3: A Desktop QuickReference For Berkeley (O'Reilly & Associates,Inc., 1990, ISBN 0-937175-20-X).
UNIX in a Nutshell: A Desktop Quick Reference forSystem V & Solaris 2.0 (O'Reilly & Associates,Inc., 1992, ISBN 0-56592-001-5).
The UNIX Programming Environment, Brian W.Kernighan & Rob Pike (Prentice Hall, 1984).
8/2/2019 Fundamentals of UNIX
http://slidepdf.com/reader/full/fundamentals-of-unix 156/156
Thank You