Sei sulla pagina 1di 107

UNIX with Shell

Scripting
Introduction to UNIX
Agenda

• Operating System and its functions


• History of UNIX Operating System
• Features of UNIX Operating System
• Structure of UNIX OS
• Working in UNIX environment
• Files & File systems
• Tools
• Text processing
• Shell scripts
• Signals
• Scheduling
What is an Operating System ?

• A computer system, on its own is just a collection of hardware


connected in such a manner to perform a particular task.
• Application program or the software makes the computer hardware
function.
• Writing programs that keep track of all components of a system and
use them correctly is a complex task
• Some sort of shield or layer of programs is required that will hide the
complexity of the underlying hardware,
• Manage all parts of the system and present the user with an
interface
• This layer or set of programs is the Operating System
A Multi-user Operating System

• A multi-user operating system is one, which can cater to


the needs of a number of users
• time-sharing is a technique OS uses implement this
• The operating system divides the total time into a number
of time-slices and schedules the tasks one after another in
a given priority
• OS acts as a resource a manager managing memory ,
hardware by scheduling access .
Functions of an OS

• Isolating the user from the hardware


• Providing a transparent layer to communicate with
hardware
• Scheduling tasks of various users.
• Managing resources and allocating them to various users.
• Resolving conflicting requests of various users.
• Monitoring and auditing system operations
UNIX Operating System

• UNIX has evolved over the past many years, from its conceptual
state into a powerful and effective OS
• UNIX evolved from a early time-sharing OS called MULTICS.
• Ken Thompson of AT&T of Bell Laboratories originally designed
UNIX OS in 1969
• Originally written in assembly language, the core of the UNIX
system was recoded in ‘C’ in 1973,after Dennis Ritchie came up
with ‘C’ language
• In 1977, Berkeley campus of the University of California made the
first BSD release
• Ideas and code from Berkeley Unix (including the vi(1) editor) were
fed back to Bell Labs
Origins of Unix and Linux

• The first Unix company (the Santa Cruz Operation, SCO)


began operations in 1978,
• In 1982 Sun Microsystems, founded by Bill Joy,
Andreas Bechtolsheim, and Vinod Khosla came up with
a Unix machine having built-in networking capability.
• By 1983 there were no fewer than six Unix-work alike
operating systems for the IBM-PC came up : uNETix,
Venix, Coherent, QNX, Idris, and the port hosted on the
Sritek PC daughtercard
Unix and Linux

• In 1985 Intel shipped the first 386 chip, capable of


addressing 4 gigabytes of memory with a flat address
space which gave the capability to run UNIX comfortably
• POSIX standards were set up in 1985 , an effort backed
by the IEEE.
• All later Unix standards would incorporate POSIX at their
core, and later Unixes would adhere to it closely
• In 1992 BSD was shipped in a 386 box,
• In August 1991 Linus Torvalds, then an university student
from Finland, announced the Linux project
Some Flavors and Distribution

• System V
• BSD Unix FreeBSD
• Solaris Digital Unix
• IRIX AIX
• Linux - distro
– RedHat Slakeware Mandrake Ubuntu
– Debian SuSE Knoppix Fedora
Features of UNIX OS

• Multi-user, Time-sharing OS
• Multi-tasking OS
• Portable
• Modular
• Secure
• Efficient tools
• Strong text processing capability
• Simple
Structure of UNIX system

• The kernel
• The shell
• Utilities/tools and applications
Starting a session

• Users must always be authenticated to use system


resources on UNIX
• Getty is the program that enables one to log in through
a serial device such as a virtual terminal, a text terminal,
or a modem
• Once username is entered , getty hands this over to
login program which asks for a password,
• Login checks user details in /etc/passwd file and
passwords in /etc/shadow
• Once the authentication is completed starts a shell,
which displays a prompt
• Logging out with exit or ctrl+d
Some UNIX basic Commands

• passwd
• pwd
• ls
• cat
• rm
• mkdir
• banner
• date
• env
• whoami
• who
Some commonly used commands

• passwd
– Changes login password
• pwd
– Stands for Present Working Directory
– Displays the current directory
• List files – ls
– Lists the files in the current directory
-l gives a long listing

-a displays all files including hidden files


• Cat
– The cat command displays the contents of a file.
Some commonly used commands

• rm
– To delete a file
– Using –i option, prompts the user before deleting the
file
• cd
– command is used to change directories
– ‘.’ refers to the current directory
– ‘..’ refers to the parent directory
– cd when used without arguments changes to home
directory
• Mkdir
– used to create directories
Predefined Shell Variables

• Variables applying to current instance of the shell


• Can be local or predefined

Some Predefined variables:


• HOME user’s home directory
• PATH the directories that the system searches to find commands
• PS1 the system prompt
• LOGNAME displays the user’s login name
• Set or env would give all the variables

Note: Some of the variables can be modified


Displaying variable contents

• echo command can be used to display


variable contents
$echo $HOME
/usr/guest
Pathnames

• Relative pathname
$ cat ./info

• Absolute pathname
$ cat /usr/guest/intro/sample
Wild-card characters

• Special characters called wild-card characters,or metacharacters may be


used to specify multiple filenames.
• These characters can substitute filenames or parts of filenames
• ‘*’ character is used to indicate any character’s.
• ‘?’ character replaces any one character in the filename

$ cat Ap*
Displays the contents of all files having a name starting with Ap.
$ ls ?st
Lists all file starting with any character followed by st.
The [ ]

• [] is used to specify a range of characters

$ ls [a-z]pple

Lists all files having names starting with


any character from a to z
• ^ indicates beginning with
• [^ ] indicates not with
Environment variables

• Are a set of dynamic values , that influences the behavior of a process

• The environment variables are normally initialized during system


startup by the system scripts, and inherited by all other processes in
the system.

• Environment variable that is changed in a script or program will only


affect that process and possibly child processes
• printenv or env command prints out all the environment variables
(bash)
• Some environment variable
– USER,HOME
– USER
– OSTYPE
UNIX with Shell
Scripting
UNIX Basic Commands
Agenda

• Unix files and Filesystems


• Interacting with FileSystems
• Filters
File Systems representation
Unix FileSystems

• File system is a system of files with some arrangement for


organization and storage.
• Unix looks at all disks and storage devices as part of one file
system
• The top of the file system is called the "root" directory and is
represented by the forward slash /.
• Each directory, starting with the root directory needs to have a
storage device associated with it. A whole disk, or just a
partition of the disk can be assigned to the directory
• Any subdirectories that are created will use the storage space
assigned to their parent directory, unless they are assigned
their own storage space.(for example /usr , /proc etc)
Unix FileSystems

• The standard Unix file system, generically referred to as the


"ufs" file system, is arranged on a disk partition using a "linked-
list" of pointers to data
• Each partition has a superblock , which is a structure
containing information on the file system and the location of the
“root directory”
• Files are stored in the file system in two pieces:
– acutal data of the file in some blocks
– a data structure which contains information about the file
called inode.
• The directory is a special file which contains the name/inode
pair mapping for files “contained” in the directory
• The root directory “contains” all the other directories.
Mounting

• Mount / Umount
– A process by which a specific file system (on a device/partition)
can be accessible
– Mounting involves specifying the device file representing the
partition and the directory under which the device files would be
available (mount point)
– This concept works on the basic principle of unix filesystems
that there is only one directory hierarchy , which can span
multiple devices /partitions
– Mount information is available in /etc/mtab
– Mounting can be done automatically by adding an entry in the
/etc/fstab
Filenames

File names may be :


• Up to 256 characters long
• Consist of letters,digits, underscore (_) and ‘.’
• Period (.) used at the beginning of a filename indicates that it
is a hidden file
• Everything is treated as a file
Some File System commands

• fsck
• mkfs
• du
• man hier
• mount
• umount
• df
File Types

• Ordinary Files
– text, executable programs, images, etc.

• Directories
– Branching points in the hierarchical tree
– Used to organize groups of files
– May contain ordinary files, special files or other directories

• Special Files
– Used to represent a real physical device such as a printer, tape
drive or terminal, used for Input/Output (I/O) operations
– Two types of I/O: character and block
– Usually only found under directories named /dev
Files

• Pipes
– UNIX allows commands to be linked together using a pipe.
The pipe acts a temporary file which only exists to hold
data from one command until it is read by another
– For process communication
• Links
– a system to make a file appear in more than one location
on a filesystem

• ls –l displays the file type as its first character in the output


– - :Regular file
– d :Directory
– l :Link
– b,c,p :Special file
Working in the File Systems

• cp
• mkdir
• rmdir
• mv
• cd
• rm
• ln
• cat
• ls
• file
• find
• diff
• touch
Redirections

• A file descriptor is a number associated with a file of data


0 standard input
1 standard output
2 standard error

• > , <, >> are metacharacters used for redirection


• Redirecting to /dev/null discards the output

• $ cat file 1> newfile 2> errmesg


• “-” metacharacter is used for stdin

• Cat abcd - concatenates contents of abcd and standard


input
File Access Permissions

• Each file and directory in the system has access permissions


• read
– allows a user to view the contents of a file
– Read permission for a directory allows a user to view the
contents of a directory.
– To view the contents of files in the directory the respective
files must have read permission
• write
– A user who has write permission on a file can change the
contents of a file
– Write permission on a directory allows a user to change the
contents of a directory.
– The user may add files or delete existing files.
– Changing the contents of a file in the directory depends on
write permissions for respective files
File Access Permissions

• execute
– allows a user to use the filename as a system command.
– On a directory it allows the user ‘search’ permissions on
the directory.
– The user can copy files into the directory

• ls –l will give a detailed list of permissions for each entry in the


directory
chmod

• The chmod command is used to change the file modes of the


specified files and directories
• The file mode specifies read, write and execute permissions
for a file.
• File permissions can be changed only by the owner.
• File access permissions can be changed in two ways
– using the symbolic mode
– or the absolute mode
The mode

• Absolute
– The absolute mode indicates the exact settings for all
permissions
– In the absolute mode octal numbers are used to represent the
three kinds of access permissions.
– The octal values are:
4 read permission
2 write permission
1 execute permission
– Chmod [nnn] [filename]
• Symbolic
– The symbolic mode indicates particular permissions to be set
– The symbolic mode consists of three parts. Who is affected, the
operator indicating action taken, and the permission.
– chmod [ugo] [+/-] [rwx] [filename]
Examples

• $ chmod 777 abc


– This gives all users read, write and execute permission the
file abc
• $ chmod 760 abc
– This gives the owner read, write and execute permissions;
– the group read and write permission;
– other users are not allowed to access the file.
• chmod +x aa
This changes file permissions on file aa for all users.

• $ chmod uo +x ac
This changes file permissions on file ac for specified users
Changing Owner chown

• The chown (change owner) command is used to change the


individual ownership of a file
chown [user-id] [filename]

• Only the owner or superuser can change file ownership


Changing Group chgrp

• The chgrp command changes the group ownership of a file


• Chgrp is possible if the user is a member of more than one
group and is working in directories with different access
permissions

chgrp [group-id] [filename]


umask

• umask – set permissions globally


• With umask one can specify the default permission for
files
• umask takes a number nnn which indicates what to
subtract from the full permission value 777.
• Umask 022 indicates 0,2,2 to be subtracted from 7,7,7,
thus the default permission of the file would be
rwx,r-x,r-x
Filter

• A filter is program that gets most of its data from standard


input device and writes its main results to standard output
device
• filters are often used as elements of pipeling, i,e chaining of
process such that output of one becomes the input for the
other
• Through the concept of pipelining , complex tasks can be
achieved
• Some UNIX filters
– cat
– cut
– grep
– head
Filters..contd

– sort
– split
– tail
– tr
– uniq
– wc
head & tail

• head command displays the first few lines of a file.


• If a number is not specified, the first 10 lines of the file are
displayed by default
head [ -n ] file
• The tail command may be used to view the last n lines end of a
file, 10 by default .
• $ tail [filename]
• The available options are:
-n displays the last n number of lines of the file
+n displays the contents of the file starting from n number till
the end of the file
-nc displays the last n number of characters of the file
-nb counts and displays the last n number of blocks of the file
More & less

• The more command is used to display data one screen


at a time
– more [filename]
– more [option] [filename])
-n specifies the size of the window
+/pattern starts display 2 lines before the pattern
-c clear each screen and display text at the top
of the screen
wc

• The wc command can be used to count


the number of lines, words and characters
in a file.
$ wc [filename]

• The available options are :


wc -[options] [filename]
-I counts the number of lines in the file
-wcounts the number of words (group of characters delimited by a
newline or whitespace)
-c counts the number of characters
sort

• sort is a utility program that can be used to sort text files


• Sorting is done in alphabetical or numeric order
• It can take a list o files as arguments or take input from
standard input
• Output is displayed on standard output
• By default it sorts the lines on the first word or field.
• A field is a group of characters separated by a delimiter. The
entire line is treated as a record.

sort [filename]
Sort Options

• The available options are:


– -f folds lowercase letters into upper case characters in a string
– -n sorts numeric data in the correct order for numbers
– -d ignores punctuation marks and other special
characters while sorting text
– -r sorts data in reverse order
– -M sort considers the first three characters of the line as
months
– -o redirects output to another file
– -b ignores blank spaces while sorting
– -t specifies the delimiter used in the file
– +n tells sort to start the comparison from n field number
uniq

• The uniq command is used to display the uniq(ue) lines


in a sorted file
• It takes input from standard input or a list of file names
as arguments
• The available options are:
– -c counts the number of occurrences
– -d displays only the lines that are duplicated
cmp
• The cmp command compares two files (text or binary) byte-by-byte
and displays the first occurrence where the files differ
• If there are no differences the cmp command does not display
anything

cmp [filename1] [filename2]


cat first
“waves represent a continual dance between ocean and air”

$ cat second
“waves can banish a beach overnight, then build a new one the next
spring”

$ cmp first second


first second differ: char 8, line 1
diff

• The diff command compares text files


• It gives an index of all lines that differ in the two files
along with the line numbers
• It also displays what needs to be changed
diff filename1 filename2
diff – filename2

• Using the –b option causes the diff command to ignore


trailing blank or tab spaces as differences.
$ diff first second
comm

• The comm command compares two sorted files and


displays the instances that are common.
• The display is separated into 3 columns.
comm filename1 filename2

firstdisplays what occurs in the first files but not in the second
second displays what occurs in the second file but not in the first
Third displays what is common in both files

• Output can be displayed in the desired format by


suppressing the columns that should not be displayed.
comm. –1 filename1 filename2

• Displays the second and third column, i.e. what is only in the second file and what is common to
both files.
tr

• The tr command is used to translate


characters
• The command takes only standard input
and no filenames
• If a file is used it must be redirected to
standard input
tr [-option] [string1] [string2]

$ tr “(a-z)” “(A-z)” < file1


cut

• The cut command can be used to cut out parts of a file.


• It takes filenames as command line arguments or input
from standard input.
• The command can cut columns as well as fields in a file.
• cut recognizes only tab character as a delimiter. Any
other character used as a delimiter must be specified to
the cut command.
It however does not delete the selected parts of the file
– $ cut –c 1,2 data
– cut –c3-5 filename
– cut –d”:” –f1,2,3 filename
where –d indicates a delimiter specified within “ “.
paste

• the paste command joins lines from two files and displays the
output.
• It can take a number of filenames as command line
arguments.
• A ‘-‘ takes input from standard input.
• The command takes each file from left-to-right and joins the
lines. If there is no corresponding field the command puts the
delimiter and leaves it blank.
paste file1 file2
• A number of delimiters may be specified after the –d option to
separate different fields.
– paste –d$:* file1 file2 file3
– $ paste –d$: file1 file2 file3
split

• Sometimes a file may be difficult to edit.

• Using split a large file may be split into smaller files. After
editing, the file may be concatenated into a single file.
split filename

• The file is split into lines of 1000. Each file is named. Xaa,
xab, xac, … xba until xzz.

• The number of lines in each output the can be controlled.


split –2 filename
grep

• The grep (global regular expression and


print) command can be used as a filter to
search for strings matching a pattern in
files.
• The pattern may be either a fixed
character string or a regular expression.
grep “string” filename
Regular Expressions

Symbol Meaning

. Matches any single character


* Matches 0 or more occurrences of preceding
character
[] Matches any of the characters enclosed in
brackets
[a-z] Matches any character in the specified
range
^ Matches at the beginning of line
$ Matches at the end of the line
Egrep

• Egrep
– Provides a set of regular expressions which is more powerful
than grep
+ one or more repetitions of a character.
? Matches zero or one instance of a character.
| Search for several targets.
() Treats enclosed text as a group.
$ egrep “marian|ron|bruc” phone_list
Searches entries for marian , ron and bruc.
grep options

- c Displays count of number of occurrences

- l Displays list of filenames only

-n Displays line numbers along with lines

- v Displays all but lines matching expression

- i Ignores case for matching

- h Omits filename when handling multiple files

- e exp Specifies expression with this option

- Nn Displays n lines containing pattern


find

• The find command searches through directories for


files that match the specified criteria.
• It can take full pathnames and relative pathnames
on the command line.
find pathname search-expression
• To display the output on screen the –print option
must be specified
find

The available options are:


-name searches for the specified filename
-atime the last time the file was accessed +n greater than n number
of days
– n less than n number of days n number of days

-mtime the last time the file was modified +n greater than n number of
days -n less than n number of days

-exec the command is successfully executed.


{} indicates the current pathname. The command is terminated with
an escaped semi-colon \:

-ok same as –exec it prompts the user before executing command

$ find . -name “prg*” -print


xargs

• Build and executes a command , taking arguments


from the standard input or from a redirected file

• For example
xargs cat
/etc/passwd /etc/group [The arguments are given in the
standard input]
Sed – Changing information in Files

• Sed edits a character stream, and is thus able to handle very large
files. It is useful over vi in a way that vi cannot edit files exceeding a
certain size.
• It doesn’t change the original content of the file rather it writes to
standard output

Syntax:
sed <option> ‘address action’ <file (s)>
Internal Sed Commands-Options

i Inserts before line


a Appends after line
c Changes line (s)
d Deletes line (s)
p Prints line (s) on standard output
q Quits after reading up to addressed line
= Prints line number addressed
s/s1/s2/ Substitutes string s1 by string s2

r filename Places the contents of file filename after line

w filename Writes addressed lines to file filename

y/s1/s2/ Transforms characters in line mapping each


character in string s1 with its counterpart in string
s2
Sed examples

$ sed ‘3q’ /etc/passwd

$ sed ‘1,3p’ /etc/passwd

$ sed –n ‘1,3p’ /etc/paswd


awk

• AWK is a general purpose language that is designed for


processing text-based data, either in files or data streams.
• An AWK program is a series of pattern , action statements
• Each line of input is tested against all the patterns in turn and
the action executed if the expression is true.
• BEGIN or END specifies action to be done in the beginning or
at the end of input lines
• Provides facility to define variables and user defined functions
• In AWK, lines are broken down into fields, and these can be
displayed separately as $1,$2 etc..
• NF and NR represent Field No and Row No respectively
• FS represent the Column separator
Awk examples

• Print first column


– awk '{print $1}' file
• No of words
– { w += NF c += length + 1 } END { print NR, w,
c}
• Sum up all the fields in the last column
and display
– { s += $NF } END { print s }
Text Editing with vi

vi – Visual Editor

•It works in two modes: command mode and insert mode.


•Specified a filename to vi, it opens it if exists or creates a new file.

$ vi +67 list.c
Puts the cursor on line if exists otherwise vi will
position the cursor at the beginning of the last line.
Vi’s Input Mode Commands

Command Significance
a Begins appending typed text after the cursor position

i Begins inserting typed text right at the cursor position

A Appends material at the end of the current line

I Inserts material at the beginning of the current line

o Opens a line below the current line and puts vi in insert


mode

O Opens a line above the current line and puts vi in input


mode
Vi’s Exit Commands

Command Significance
:w Will write the contents of editing buffer into the file

:wq Will write and quit

ZZ Equivalent to :wq

:x Also equivalent to :wq

:q Quit

:q! Quit without saving


Vi’s Move Commands

Command Significance

l or Move right one character

h or Move left one character

j or Move down one line


k or Move up one line

0 Moves the beginning of the line

$ Moves the end of the current line


+ Moves the beginning of the next line

- Moves the beginning of previous line


Vi’s Move Commands Cont…

Command Significance
Moves to the next word or punctuation mark
w
W Moves to the next word

e Moves to end of this word or punctuation mark


E Moves to the next end of word

b Moves back to the beginning of word or punctuation

B Moves back to the beginning of the word

) Moves to the start of the next sentence

( Moves back to the start of the sentence


Vi’s Move Commands Cont…

Command Significance

} Moves to the start of next paragraph

{ Moves back to the last start of paragraph

]] Moves to the start of the next section


[[ Moves back to the last start of section

CTRL-F Moves forward one full screen

CTRL-D Moves forward one half screen

CTRL-U Moves back one half screen

CTRL-B Moves back one full screen


Vi’s Modify Commands

Command Significance

dw Deletes from the cursor to the end of the word

3dw Deletes three words

d$ Deletes to the end of the line


D Same as d$

3d$ Deletes to the end of the third line ahead

d) Deletes to the beginning of the next line

d} Deletes to the beginning of the next paragraph

dd Deletes the current line


Vi’s Modify Commands

Command Significance

d]] Deletes to the beginning of the next section

2dd Deletes two lines

dRETURN Deletes two lines

dG Deletes from the cursor to the end of the file

u Undoes the most recent change

U Undoes all of the changes in a line since last moved

p Puts the contests to the right of the cursor position

P Puts the contest to the left of the cursor position


Vi’s Modify Commands Cont…

Command Significance

yw Yanks a word

3yw Yanks three words

y$ Yanks to the end of the line


y) Yanks to the end of the sentence

y} Yanks to the end of the paragraph

y]] Yanks to the end of the section

yy Yanks the current line

3Y Yanks three line, starting at the current line


Shell Scripts

• A shell script is an executable file which contains shell commands.


•The script acts as a "program" by sequentially executing each command in
the file.
•To declare variables , the following form is used

$ SIZE=1024
$ MY_ADDRESS=buchanan@phys.ualberta.ca
$ greeting='Welcome to the Bash shell'

•To access a variable, a $ must be placed in front of the variable name


Shell Variables

• System variables are variables that have special meaning to the


shell.
• These variables are used to customize the environment for a
particular user.
• These variables are defined in the special program that is
executed during login called the .bash_profile file.
• Any defined variables would then be set until the end of the
session unless explicitly unset or redefined.
• PATH,HOME , SHELL,IFS etc are some System variables
• Shell or built in variables are defined by the shell for use at any
time. They are mostly used for shell programming.
• Export command is used to make a variable available to sub shells
Few Shell Variables

Variable Description
$$ Process no of the current process
$! Process no of the last background process
$? Exit value of last command
$# Number if command line arguments
$0 Command or program name
$n Positional Parameter number n
$* All command line arguments
“$@” All arguments each in quotes
Testing and Branching

if statement_A
then
statement_B
statement_C
elif statement
statement_D
else
fi
case value in
pattern1) command list;;
pattern2) command list;;
.
.
.
patternN) command list
esac
loops

• For <var> in <list>


do
done;

• While <command>
do
done

• Until <command>
do
done
File Test Operators

Test Format Value Returned


-b file TRUE if file exists and is a block special file
-c file TRUE if file exists and is a character special file
-d file TRUE if file exists and is a directory file
-f file TRUE if file exists and is a regular file
-g file TRUE if file exists and its set-group-ID bit is set
-h file TRUE if file exists and is a symbolic link file
-H file TRUE if file exists and is hidden directory
-k file TRUE if file exists and its sticky bit is set
-p file TRUE if file exists and it’s a named pipe
-r file TRUE if file exists and is readable
-u file TRUE if file exists and its set-user-ID bit is set
-s file TRUE if the file exists and is non-empty
An Example Script:-

#! /bin/bash
# # Move is a script that moves a file if no overwrite will occur
# and will delete the file to be moved if it is empty
# # Usage: move [file] [destination]
#
if [ ! -s $1 ] then
if rm $1 2>/dev/null then
echo "$1 was empty and thus removed"
else
echo "$1 does not exist"
fi
else
mv -i $1 $2
fi
String Comparisons

Strings are considered to be equal if each character in one is matched


exactly in the other, and they have the same length.

All the String tests supported by test are:


Test Format Value Returned
String1 = String2 TRUE if strings are equivalent
String1 != String2 TRUE if strings are not equivalent
-z String TRUE if string has zero length
-n String TRUE if string has nonzero length
Integer Comparisons:

Test Format Description

-eq if they are equal


-ne if they are not equal
-gt if Integer1 is greater than Integer2
-ge if Integer1 is greater than or equal to Integer2
-lt if Integer1 is less than Integer2
-le if Integer1 is less than or greater than Integer2
Mathematical Expressions

Operator Operation
+ Addition of two integers
- Subtraction of two integers
* Multiplication of two integers
/ Integer division
% Modulus of integer division

An Example expr:

$ expr 3 + 2 \* 8
19
Substitution

The shell performs substitution when it encounters an expression that


contains one or more special characters. Different types in
substitution are:-

• Filename substitution (called wildcard substitution or globbing)


• Value-based variable substitution
• Command substitution
• Arithmetic substitution
Filename Substitution (Globbing)

Wildcards Used in Filename Substitution

Wildcard Description

Matches zero or more occurrences of any


*
character
? Matches one occurrence of any character
Matches one occurrence of any of the given
[characters]
characters
Variable Substitution

Variable substitution enables the shell programmer to manipulate


the value of a variable based on its state.
The variable substitution methods are:

Form Description
${parameter:-word} If parameter is null or unset, word is substituted for
parameter. The value of parameter does not change.
${parameter:=word} If parameter is null or unset, parameter is set to the value
of word.
${parameter:?mess If parameter is null or unset, message is printed to
age} standard error. This checks that variables are set
correctly.
${parameter:+word} If parameter is set, word is substituted for parameter. The
value of parameter does not change.
Substituting a Default Value

• The first form enables a default value to be substituted when a variable


is unset or null.

PS1=${HOST:-localhost}"$ " ; export PS1 ;

•Assigning a Default Value

•It enables parameter to be set to the value of word if parameter is null or


unset,.

PS1=${HOST:='uname -n'}"$ " ; export PS1 HOST ;

•After the execution of this statement, both HOST and PS1 are set.
Aborting Due to Variable Errors

•If parameter is null or unset, message is printed to standard error. This


checks that variables are set correctly.

: ${HOME:?"Your home directory is undefined."}

•The final form of variable substitution is used to substitute when a variable


is set.

echo ${DEBUG:+"Debug is active."}


Command Substitution

•Command substitution is the mechanism by which the shell performs a


given set of commands and then substitutes their output in the place of
the commands.

•Command substitution is performed when a command is given as

`command`
• Command substitution is generally used to assign the output of a command
to a variable. Examples:

DATE=`date`
USERS=`who | wc –l`
UP=`date ; uptime`
Quoting

Turning off the special meaning of a character is called quoting, and


it can be done three ways:
• Using the backslash (\)
• Using the single quote (‘)
• Using the double quote (“)
Quoting character Effect
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
Signals

• Signals are software interrupts sent to a program to


indicate that an important event has occurred.
• Some signals, such as the interrupt signal, indicate
that a user has asked the program to do something
that is not in the usual flow of control.
• Each type of event is represented by a separate
signal. Each signal is only a small positive integer.
Important Signals for Shell Scripts

SIGNAL VALUE DESCRIPTION


SIGUP 1 Hangup detected or death of controlling
process
SIGINT 2 Keyboard interrupt

SIGQUIT 3 Quit from keyboard

SIGKILL 9 kill

SIGALRM 14 Alarm clock (timer)

SIGTERM 15 Terminate signal


Trapping Signals

• Trap commands sets or unsets the action taken when a


signal is received
• The syntax is
trap name signals
• Example
CleanUp() {
if [ -f "$OUTFILE" ] ; then
printf "Cleaning Up... ";
rm -f "$OUTFILE" 2> /dev/null ;
echo "Done." ;
fi
}
trap CleanUp 1 2 3 15

• Signals can be ignored using the command


trap ‘’ signals or trap : signals
Kill

• Kill – send a signal to a process


Kill [option] PID
– Default signal is 15

– Option:

-no Specific signal is generated to


terminate the process
0 kills all processes except the
login shell
Nohup – don’t hang up

• Whenever the terminal is disconnected,


the kernel sends the signal SIGHUP to all
processes that were attached to a terminal
to terminate all the processes
• To make a process alive even after a
logoff, we use the nohup command.

$ nohup command
$ nohup myproc
Subshells

• Shell creates a child shell whenever an instruction is typed at the


command prompt, either by typing a group of commands (like ls; pwd;
date), or by executing a script or by running a script/command in
background,
• The child shell is called subshell.
• Since a child shell has its won working area, any change made in the
child shell does not effect to the parent.

Parent Shell
Child Shell
Environment
Environment Copied from Parent
Local
Local Clean, initialized
Exporting ..Revisited

• By default a variable is not available to its child


shell
• To make it available to all its child shells export
command is used .
$ export <variable_list> [all]
• If no variables a specified , export will list all the
exported variables
Process

• Process are programs carry out tasks within an operating


system,
• Unix is a multiprocessing OS, many processes seem to run
simultaneously
• Processes information are maintained in a struct called
task_struct
• Kernel holds all the processes in a circular double linked list
called task list
• The task_struct holds info such as state,scheduling
info,PID,PPID etc
• Process can run is two modes User mode or Kernel mode
• User mode processes have lesser privileges than kernel mode
• Processes can be Ready,Running,Sleeping,Zombie,Orphan
Dealing with Process

• ps – Process Status
– Displays the characteristics of a process

– Option:
-f Prints the full details
-u (user) Prints the activities of the user “user” at any time
-a Prints all the processes running in the system
-l Long listing of the attributes of each processes
Scheduling jobs

• “at” command can be used to schedule the execution of


command
• By default at reads the commands from standard input
• Script names can also be specified via the –f option
$ at 5 pm tomorrow
echo “Don’t forget the meeting with BOB at 1 PM!” | mail guest
CTRL-D
Option
$ at –f scriptfile 6 am tomorrow

$ at –l
Returns the ID number and scheduled time for each of your at jobs.
cron

• A system daemon which performs a specific task at


regular intervals
• The command and schedule information is kept in the
directory /var/spool/cron/crontabs or in
/usr/spool/cron/crontabs.
• Each user has a crontab file.
• ron wakes up periodically and executes any job that are
scheduled for that minute.
Cron

• Only users who are listed in /etc/cron.allow or not listed


in cron.deny can make an entry in the crontab ,
• To make an entry in the crontab file , the command
crontab is used
• Crontab <filename>
where the file contains the commands to execute
MIN HOUR DOM MOY DOW COMMAND
(0-50) (0-23) (1-31) (1-12) (0-6) ---
0 18 * * * /home/gather

Potrebbero piacerti anche