Академический Документы
Профессиональный Документы
Культура Документы
by Imran
HISSAR
INTRODUCTION
HISSAR
Objectives
In this session, you learn about:
The OS The history of Unix The features of UNIX The Unix architecture Process management CPU scheduling Memory management Unix Connectivity
3
HISSAR
OS is a system software OS can be defined as an organized collection of software consisting of procedures for operating a computer
OS acts as an interface between the user and the hardware of the computer system.
HISSAR
Operating System
Operating system commands Enables user to interact directly with the operating system.
Operating system calls Provides an interface to a running program and the operating system. System calls in UNIX are written in C.
HISSAR
History of UNIX
Ken Thompson of AT&T Bell Laboratories designed UNIX in late 1960s Two versions of UNIX that emerged are AT&T Unix and BSD Unix In 1989, AT&T and Sun Microsystems joined together and 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 Xconsortium developed vendor-neutral Xwindow System.
HISSAR
What is UNIX?
UNICS?
Power of Unix?
HISSAR
What is Linux?
An open-source UNIX like operating system
HISSAR
Features of UNIX
Multi-user, multitasking, timesharing Portability Modularity File structure Security Strong networking support & advanced graphics
HISSAR
Layered Architecture
banner ls
kernel hardware
sort
HISSAR
10
The innermost layer is the hardware layer In the second layer, the kernel is placed The utilities and other application programs form the third layer Fourth layer is the one with which the user actually interacts.
HISSAR
11
Kernel
Kernel is that part of the OS which directly makes interface with the hardware system.
Actions:
Provides mechanism for creating and deleting processes Provides processor scheduling, memory, and I/O management Provides inter-process communication.
HISSAR
12
The Shell
Interactive Processing Background Processing I/O Redirection Pipes Shell Scripts Shell Variables Programming Constructs
13
HISSAR
Process Management
The process that invokes the fork( ) system call is the parent process, and the newly created process is called the child process.
HISSAR
14
CPU Scheduling
Unix uses round-robin scheduling to support its multi-user and time-sharing feature.
Round-robin fashion of scheduling is considered to be the oldest, simplest and widely used algorithm.
HISSAR
15
Memory Management
Virtual memory
HISSAR
16
A file will have properties like block special, character special devices etc.
HISSAR
HISSAR
18
dev
bin sh
tmp home
etc inittab
var spool
lib bin
usr src
console lp0
ls user1
passwd user2
HISSAR
19
Block size can be decided while creating the file system structure
HISSAR
20
Address of the addr block Address of the addr block Address of the addr block
HISSAR
21
HISSAR
22
cont..
Operating System Handles files, running other programs, input/output Just like DOS or Windows
Networked Designed for server use Networking is an intrinsic part of the system
Multi-user Every user has different settings and permissions Multiple users can be logged in simultaneous
23
HISSAR
Unix connectivity
Terminal Programs:
Ssh telnet
ftp
24
HISSAR
Group
UNIX allows user IDs to be grouped A single user ID can be member of multiple groups
HISSAR
User logs in with a valid user ID User logs out to terminate the login session
HISSAR
26
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 Unix Connectivity
27
HISSAR
UNIX Commands
HISSAR
28
Objectives
In this session, you will learn to:
Command Structure
HISSAR
Command Structure
General syntax
Parse the command line and execute it with the given parameters as a child process
HISSAR
30
Types of Commands
Internal commands
External Commands
Priority
HISSAR
Built-in External
31
if so, executes it
2.
Eg:
1.
3.
If the command is neither built-in, nor specified with an absolute pathname, the shell looks in its search path for an executable program or script with the given name.
HISSAR
32
Absolute Path
Eg:
cd /root/home/SLS
Relative path
Eg:
cd SLS/imran
HISSAR
33
Using . and ..
Current directory
Eg:
..
Parent Directory
Eg:
HISSAR
34
Simple Commands
pwd
date
HISSAR
35
Simple Commands
who
Displays the names of all the users who have currently logged in
who am i
HISSAR
36
ls
Syntax :ls [options] [file.] options: -l list in long format -a list all files including those
beginning with a dot -i -s -R -F list inode no of file in first column reports disk blocks occupied by file recursively list all sub directories mark type of each file display files in columns
37
HISSAR
-C
Meta Characters
Meta Characters * ? [] Purpose Example $ ls l *.c file* $ ls l file? $ ls l file[abc] $ cat file1; cat file2 $ cat abc | wc $ (echo ==== x.c ====; cat x.c) > out count=`expr $count + 1` Match with one or more characters or none Match with any single character Match with any single character within the brackets ; Command separator | Pipe two commands () Group commands Useful when the output of the ommand group c has to be redirected `command` Execute the command enclosed within back quotes. Useful when the output of a command into a variable in a shell script string string
assuming count has value this 3, increments the value of count Quote all characters with no substitution echo expr $count + 1 (ex. no special meaning for ) $ displays expr $count + 1 Quote all cha racters with substitution. echo expr $count + 1 The characters $,\ (back slas and back quote displays expr 3 + 1 h) have special meaning. assuming the variable count has value 3
HISSAR
38
1 2 1 1
a.out
c core cs
user1
projA projC
File name
255
Dec 20 14:29
HISSAR
39
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 -M path -a show list command synopsis line for all keyword matches path to man pages all matching man pages (SVR4)
info command_name - help for commands help -command_name gives command synatx
40
HISSAR
Summary
HISSAR
41
Utilities
HISSAR
42
Objectives
In this session, you will learn how to:
gzip
HISSAR
43
cat
cat command takes the input from the keyboard, and sends the output to the monitor We can redirect the input and output using the redirection operators $ cat > file1 Type the content here press <ctrl d> $ cat file1 Displays the content of the file $cat >> file1 This will append standard input to the content of file1
HISSAR
44
touch
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 the file exists
If the file does not exist, it will create a file of zero byte size.
45
HISSAR
echo command is used to print output to the screen echo 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
HISSAR
46
more
file
tty
HISSAR
47
Wc
A filter used to count the number of lines, words, and characters in a disk file or from the standard input. -l - displays the number of lines
HISSAR
48
cmp
Returns the offset and the line number of the first position where the two files differ.
comm
col1 - unique lines of first file col2 - unique lines of second file col3 - common lines
HISSAR
49
find
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 )
Example find . name *.c -print lists all files with .c extension from the current dir & its subdirectories
HISSAR
50
find
size [+ ]n[bc] n represents size in bytes (c) or blocks (b) of 512 bytes find . size 1000c find . size +1000c find . size 1000c lists all files that are exactly 1000 bytes in size lists all files that are more than 1000 bytes in size lists all files that are less than 1000 bytes in size
HISSAR
51
find
Finding files on the basis of access time (atime) or modified time (mtime)
atime [+-]n mtime [+-]n n represents number of days ( actually 24 * n hours) find . atime 2 find . atime +2 find / mtime 2 lists files accessed exactly 2 days ago lists files accessed more than 2 days ago lists files modified less than 2 days ago
HISSAR
52
find
Applying a command on files matching the criteria with exec and ok options
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.
HISSAR
53
pr
pr
Used to display a file in a format to be printed. Breaks up a file into pages with a header, text and footer area
Options
-l -h -t -n
to alter the length of the file to set the header to suppress the header and the footer to set the line number
HISSAR
54
Standard Files
HISSAR
55
I/O Redirection
< file > file 2> file 2>&1 $ cat > abc redirect standard input from file redirect standard output to file redirect standard error to file merge standard error with standard output
$ ls l > outfile $ cat xyz abc > outfile 2> errfile $ cat xyz abc > outfile 2>&1
HISSAR
56
Filters
Filters are programs that takes its input from the standard input file, process it, and sends it to the standard output file.
HISSAR
57
sort
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 -o -u +num[-num] example: reverse sort Result stored in specified file To have only unique lines To merge input with the file for sorting To sort according to the field number sort file1
58
HISSAR
grep
grep -Global Regular Expression Printer is used for searching regular expressions
Syntax
-v
-c -n -i
Displays only those lines which does not match with the pattern specifies
Displays only the count no of lines which match the pattern specified Displays matching lines with the line numbers at start Matches the pattern specified ignoring the case
Example:
grep v EDS file1
HISSAR
59
Patterns
* - matches 0 or more characters
HISSAR
60
$ head -3 file1
HISSAR
61
$ tail -3 file1
Can also specify the line number from which the data has to be displayed till the end of file
Filter command - tr
tr - translate filter used to translate a given set of characters
This converts standard input read from lower case to upper case.
63
Filter command - tr
Useful options for tr
-s char Squeeze multiple contiguous occurrences of the character into single char
HISSAR
64
Command Piping
Allows the output (only the standard output) of a command to be sent as input to another command. Multiple pipes may appear in one command line.
65
tee command allows the normal output to the standard output, as well as to a file Useful to capture intermediate output of a long command pipeline for further processing, or debugging purpose. Example
HISSAR
66
Option remark -c -d -f Examples $ cut -c2-5 file1 $ cut -d | -f2,3 file1 used to extract characters Delimiter for fields Field no.
HISSAR
67
HISSAR
68
Compression Utilities
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.
HISSAR
69
Summary
use the Unix Utilities like cat, echo, touch, more, file, wc, cmp, comm, find employ redirection operators use filters like sort, grep, cut, head, tail, tr, and paste backup commands gzip
70
HISSAR
Chapter 3
Files & Directories
HISSAR
71
Objectives
set file permissions using the chmod command use directory-related commands namely mkdir, rmdir, cd commands use file-related commands namely cp, mv, rm commands access advanced file permissions using commands umask create and edit files using the vi editor
HISSAR
72
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)
Permission Settings
copy files to the directory remove files from the directory rename files in the directory make a subdirectory remove a subdirectory from the directory
HISSAR
Syntax: or chmod <octal number> filename Octal Number 4 - for read 2 - for write 1 - for execution $ chmod 744 xyz HISSAR this sets read, write and execute permissions for owner, read permission for group and others
76
Directory Creation
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 HISSAR
77
Directory Removal
rmdir command removes directory Syntax
rmdir project1
Remove multiple directories rmdir pos1 pos2 Remove the directory recursively rmdir p dir1/dir2/dir3
HISSAR removes a directory if it is empty and is not the current directory rmdir
78
Command - cd
cd cd .. cd /
- take to the home directory - takes to the parent directory - takes to the root directory
HISSAR
79
File-Related Commands
File Operation Copying a file Moving a file Removing a file Displaying a file and concatenating files Command cp mv rm cat
HISSAR
80
Command - cp
Used to copy files across directories Syntax cp <source file> <new file name>
HISSAR
81
Command - mv
HISSAR
Command - rm
Used to remove a file
Syntax : rm file(s) suppresses all prompting prompts before deleting destination file
-f -i -r
HISSAR
will recursively remove the file from a directory (can be used to delete a directory along with the content )
83
Command - umask
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 file permission
Default umask value will be set in the system environment file like /etc/profile
umask 022 will set a mask of 022 for the current session
The file permission after setting this umask value will be 644 And the directory permission will be 755
85
HISSAR
- ln
Soft Link (in different filesystems also used to link directories) $ ln s /usr/bin/clear /home/user1/cls
86
HISSAR
Vi Editor
A screen-oriented text editor Included with most UNIX system distributions Command driven
Cursor movement Editing commands Search and replace commands vi editor is invoked by the following command:
87
The HISSAR
Navigation
Backspace h j k Space l
brown fox w w
the quick
brown fox $
brown fox
the quick
brown fox ^
brown fox b b
HISSAR
88
Editing Commands
Text insertion / replacement i a I A o O R s S - inserts text to the left of the cursor - inserts text to the right of the cursor - inserts text at the beginning of the line - appends text at end of the line - opens line below - opens line above - replaces text from cursor to right - replaces a single character with any number of characters
HISSAR
Editing Commands
Deletion
x 3x dw 2dw dd 2dd
- to delete character at cursor position - to delete 3 characters at cursor position - to delete word - to delete 2 word - to delete a line - to delete 2 lines
HISSAR
90
Editing Commands
Yanking
Y 3Y p P
- copy line into buffer - copy 3 lines into buffer - copy buffer below cursor - copy buffer above cursor
:w :w! :x :q
- to save - to name a file (:w! filename -> save as) - save and quit - cancel changes - cancel and quit
91
HISSAR :q!
HISSAR
92
Summary
In this session, you have learned how to use file permissions using the chmod command use directory-related commands namely mkdir, rmdir, cd commands use file-related commands namely cp, mv, rm commands access advanced file permissions using commands umask create and edit files using the vi editor
HISSAR
93
Process
HISSAR
94
Objectives
In this session, you will learn to:
HISSAR
95
Processes
Process - a program in execution When program is executed, a new process is created The process is alive till the execution of the program is complete Each process is identified by a number called pid
96
HISSAR
Login shell
As soon as the user logs in, a process is created which executes the login shell.
HISSAR
97
ps
It fetches the pid, tty, time and the command which has started the process.
-f -u -a -e
lists the pid of the parent process also. lists the processes of a given user lists the processes of all the users lists all the processes including the system processes
98
HISSAR
Background Process
Enables the user to do more than one task at a time. 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
HISSAR
99
Background Process
nohup
Lets processes to continue to run even after logout The output of the command is sent to nohup.out if not redirected
$ nohup sort emp.lst & [1] 21356 nohup: appending output to `nohup.out' HISSAR
100
Background Process
wait command
can be used when a process has to wait for the output of a background process The wait command, can be used to let the shell wait for all background processes terminate. $ wait
HISSAR
101
jobs
fg % <job id>
bg %<job id>
HISSAR
102
kill -9 (SIGKILL)
HISSAR
Summary
HISSAR
104
HISSAR
105
Introduction to Shell
HISSAR
106
Introduction to Shell
content Shell Shell Basic Logging in to the Shell Basics Shell Principles Command execution
HISSAR
107
The Shell
Provides a powerful interface to the UNIX Operating System.
The 'command-line' interface, Comes under different flavours, but all of them do the same thing in slightly different ways.
sh csh bash
Original Unix Bourne Shell BSD Unix C Shell, tcsh Enhanced C Shell Bourne-Again Shell
108
HISSAR
Shell Basics
Depending on the type of shell you use, the prompt may look different.
Echo $SHELL
To change Shell
Eg. If I wanted to use tcsh, just type in tcsh and then enter at the command line
109
HISSAR
In order to be able to use the UNIX shell, you will have to authenticate yourself (tell the system who you are).
This process is commonly called the 'login' process, and it involves two steps. 1. Know your username password.
-
2.
HISSAR
110
Bash
Bash Bourne Again shell is a sh-compatible command language interpreter.
It executes commands and read from standard input device as well as from file.
HISSAR
111
HISSAR
112
All UNIX shells are case sensitive with regards to both the commands and their arguments, in contrast to versions of Windows/DOS systems. This means that typing: cd /mydirectory/programs is not the same as typing: or even: CD /MYDIRECTORY/PROGRAMS cd /MyDirectory/Programs
HISSAR
113
Command processing
Displays the shell prompt and reads the command typed by the user.
If it is NOT a built-in command, searches for the command in the PATH-specified directories, and executes that command if it is found.
HISSAR
114
Conclusion
Summary Shell is an interface between a user and the OS The user name and password has to be entered for log in Shell is case sensitive. The command line includes command and arguments Different flavours of shells are Bourne Shell C shell Bash Shell Korn Shell
HISSAR
115
Shell Programming
HISSAR
116
HISSAR
117
HISSAR
118
Bash
Bash (Bourne Again shell) is a sh-compatible command language interpreter. It executes commands and read from standard input device as well as from file. Bash is conformant implementation of IEEE POSIX Shell and Tools specification
HISSAR
119
#! /bin/bash # The above line has a special meaning. It must be the # first line of the script. It says that the commands in # this shell script should be executed by the bash # shell (/bin/bash). # ------------------------------------------------------------echo Hello $USER. echo Welcome to programming shell scripts.. # ------------------------------------------------------------120
2.
3.
4.
5.
6.
7.
8.
9.
HISSAR
#!/bin/bash
2.
echo "hello, $USER. I wish to list some files of yours" echo "listing files in the current directory, $PWD" ls # list files
3.
4.
HISSAR
121
By passing the shell script name as an argument to the shell. For example:
sh script1.sh If the shell script is assigned execute permission, it can be executed using its name. For example:
./script1.sh
HISSAR
122
Shell Programming
Allows
Defining and referencing variables Logic control structures such as if, for, while, case Input and output
HISSAR
123
Shell Variables
A variable is a name associated with a data value, and it offers a symbolic way to represent and manipulate data variables in the shell.
They are an integral part of shell programming Provide ability to store and manipulate data Fully under the control of the shell Can create and destroy the variables as the user wants
HISSAR
124
HISSAR
125
NOTE: values Vijay & /usr2/bin is assigned to variables without giving space. Instead results in error
HISSAR
126
Shell Variables
value assigned to the variable can then be referred to by preceding the variable name with a $ sign.
HISSAR
127
Shell Variables
The shell provides the facility to define normal, and environment variables.
An environment variable can be used in the shell where it is defined, plus any child shells invoked from that shell.
HISSAR
128
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`
HISSAR
129
HISSAR
130
Examples:
HISSAR
131
Working Example
parent.sh #!/usr/bin/bash environ=SYSTEM LOGIC SOLUTIONS; local=Helios matheson; echo From parent: $environ; echo From child: $local; sh child.sh
child.sh #!/usr/bin/bash echo ENVIRONMENT VARIABLE: $environ; echo HISSAR NOT AN ENVIRONMENT VARIABLE: $local;
132
Built-in variables
$0, $1$9 $* $@ $? $$ $!
- positional arguments - all arguments - all arguments - exit status of previous command executed - PID of the current process - PID of the last background process
HISSAR
133
HISSAR
134
135
The \ symbols makes the shell to understand ? as an ordinary character rather than special character Double quotes () may be used to display a string
HISSAR
136
HISSAR
137
If 1 2 3 entered, x will have 1, 2 to y & 3 to z If 1 2 3 4 are entered, then z will have 3 4 If only 1, 2 are entered, then z will be assigned null value If more arguments entered, last variable will be assigned rest. If less, null will be assigned
138
HISSAR
HISSAR
139
To make the cursor at the end of the echoed line echo Enter your Name :\c Enter your Name :_ (Cursor waits here) echo \007 gives bell
HISSAR
140
All the variables are string variables $a=20. 20 is stored as a string not as number. No arithmetic can be carried on this Use double quotes if the value contains more than one word
$name=Vijay Suri
$name=Vijay age=20
HISSAR
141
Can display more than one variable in a line All variables in a shell script are automatic variable. i.e. they will be created as soon as the shell script execution starts and dies as soon as the execution is over
HISSAR
142
A null variable can be created by Shell ignore if any shell variable is having null value
HISSAR
143
Basically, variable names are expanded within double quotes, but not single quotes. If you do not need to refer to variables, single quotes are good to use as the results are more predictable.
An example
#!/bin/bash echo -n '$USER=' # -n option stops echo from breaking the line echo "$USER" echo "\$USER=$USER" # this does the same thing as the first two lines
$USER=elflord $USER=elflord
144
HISSAR
HISSAR
145
A variable can be removed from the shell by using unset command Unset can not be used for system variables $unset PS1 is not allowed
$unset age
HISSAR
146
Positional Parameters
Many occasions, a program expects the variables in a certain fashion. This is achieved through positional parameters from $0 through $9. $0 is the program itself. Thus
$abc par1 par2 par3 par4 assigns abc to $0, par1 to $1 par4 to $4
HISSAR
147
echo Copying $1 to $2 cp $1 $2
By executing sh02
$sh02 file1 file2 Copying file1 to file2. Positional parameters can not assigned values like $1=100 HISSAR
148
Shell Variables
To know the number of variables given for the shell through $# $abc file1 file The above shell displays 3
echo $#
HISSAR
149
Shell can handle only 9 variables at a time. To access more than 9, the shift command is used $set You have the capacity to learn the shell programming in a very easy way $echo $10 displays You0 as shell interprets $10 as $1 with 0. $shift 5 : makes the 5th argument as $1 argument. Thus $echo $1 displays learn $* handles all positional parameters
150
HISSAR
Arithmetic in Shell
Write the following in a file and execute: a=10 b=4
echo expr $a + $b echo expr $a - $b echo expr $a \* $b echo expr $a / $b echo expr $a % $b
HISSAR
151
#modulus
Arithmetic in Shell
14 6 40 2 2
HISSAR
152
Arithmetic in Shell
Anything after # sign will be treated as comment expr is the key word for doing arithmetic A multiplication symbol (*) should be preceded by \ Terms in expr should be separated by space Parenthesis may be used for clarity of expression
HISSAR
153
Arithmetic in Shell
expr can handle only integers. Use bc to handle real numbers. A=1.5 b=2.5 echo expr $a + $b | bc
HISSAR
154
There are 4 decision making loops if then fi if then else fi if then elif else fi case - esac
HISSAR
155
UNIX - if then fi
Key word if and the delimiter is fi if <command>
UNIX - test
if depends upon the exit status of the command given test verbs translates the result into success or failure There are three tests namely Numerical test String test File test
160
HISSAR
-gt = greater than -lt = less than -ge = greater than or equal to -le = less than or equal to -ne = not equal -eq = equal
HISSAR
161
string1 = string2 (equal to, please note it is a single =) string1 != string2 (not equal to) string1 (string is not NULL) -n string1 (string is not NULL and exists)
163
HISSAR
HISSAR
-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)
164
-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)
HISSAR
165
UNIX - test
Use square braces to avoid writing test Provide a space after [ Provide a space before ]
HISSAR
166
The following are the file related flags -s returns True if the file exists and size > 0 -f returns True if the file exists and not directory -d return True if the file exists and is a directory
s1 = s2 returns true if both are same -n returns true if string length > 0
HISSAR
-a stands for AND condition -o stands for OR condition -! Is negation if <Condition-1> -a <Condition-2> Returns true if both the conditions are true
statements...
HISSAR
169
UNIX case
To handle multiple choices case value in choice 1) statements;; choice 2) statements;; *) statements;; esac
HISSAR
170
UNIX case
esac is the delimiter of case Used for menus *) is the default choice All choice statements should be terminated by double semicolon(;;)
HISSAR
171
HISSAR
172
UNIX case
The choices many be in any order case statement may be a shell variable or shell argument or output of a command Need not be numbers - may be strings too
173
UNIX case
e.g.:
case $1 in banana | orange) echo Fruit;; dog | pig) echo Animal;; lion) echo Wild animal;; esac
HISSAR
174
UNIX case
HISSAR
175
Provided 3 loop constructs namely: while loop for loop until loop
HISSAR
176
HISSAR
177
HISSAR
178
UNIX - while
The loop will continue so long as the condition is TRUE When the condition is false, the next command after done will be executed The condition can be any valid UNIX command The while condition can be simple condition or complex condition The condition should have an exit status. Otherwise, it may go into infinite loop
179
HISSAR
until continues its loop so long as the condition is false except this, while & until are identical
HISSAR
180
do statements done
HISSAR
181
HISSAR
182
Used to break the current loop and comes out of the loop
then I = 2 break fi
HISSAR
183
Functions
Shell functions are a way to group commands for later execution using a single name for the group. They are executed just like a "regular" command. Shell functions are executed in the current shell context; no new process is created to interpret them. Functions are declared using this syntax: [ function ] name () { command-list; }
185
HISSAR
Functions
Shell functions can accept arguments Arguments are passed in the same way as given to commands Functions refer to arguments using $1, $2 etc., similar to the way shell scripts refer to command line arguments
HISSAR
186
Functions
Function to convert standard input into upper case toupper() { tr [a-z] [A-Z] } This function can be used as
HISSAR
-v (verbose) option:
causes shell to print the lines of the script as they are read. $ bash v script-file
-x (verbose) option:
prints commands and their arguments as they are executed. $ bash x script-file
HISSAR
188
Example
Shell script checks for a blank/non blank string
read nam while [ -z $nam ] do read nam done echo the string is $nam
the above piece of code keeps accepting string variable nam until it is non zero in length.
HISSAR
189
Exercise
Write a shell script to accept the user and password Note: It should keep on asking for the username and password until the user gives the correct one.
HISSAR
190
Example
----------------------script.sh-------------------------#! /usr/bin/bash usernames=`who | cut d f1` echo Total users logged in = ${#usernames[@]} # for user in ${usernames[*]} do echo $user done -----------------------------------------------------------HISSAR
191
Example
# to check if the current directory is the same as your home directory
curdir=`pwd` if test $curdir != $HOME then echo your home dir is not the same as your pesent working directory else echo $HOME is your current directory HISSAR fi
192
AWK
By Imran Hissar Ahmed
HISSAR
193
Objectives
In this session, you will learn to:
Awk introduction Knowing which awk Command line of awk and options Patterns and Procedures
HISSAR
194
Introduction
awk is a pattern-matching program for processing files, especially when they are databases.
The new version of awk, called nawk, Every modern Unix system comes with a version of new awk, and its use is recommended over old awk.
Some have oawk and awk, for the old and new versions, respectively. Others have awk and nawk. Still others only have awk, which is the new version.
195
HISSAR
Which awk?
This example shows what happens if your awk is the old one: $ awk 1 /dev/null awk: syntax error near line 1 awk: bailing out near line 1 awk exits silently if it is the new version.
HISSAR
196
Command-Line Syntax
You can specify a script directly on the command line, or you can store a script in a scriptfile and specify it with -f.
HISSAR
197
Recognized options
-Ffs Set the field separator to fs. Fields are referred to by the variables $1, $2,..., $n. $0 refers to the entire record. -v var=value Assign a value to variable var. -v var=12 F {print $1 var} -v v1=LT f a1.awk Example: awk F { print $1; print $2; print $3 } filename cat >awk1 -F { print $1; print $2; }
HISSAR
198
If pattern is missing, { procedure } is applied to all lines; if { procedure } is missing, the matched line is printed.
HISSAR
199
Patterns
A pattern can be any of the following:
/regular expression/
/and/ /^h/ NF>2 $1~/pattern/ BEGIN{ print START} {print $0} END {print END}
relational expression
pattern-matching expression
BEGIN END
Expressions can be composed of quoted strings, numbers, operators, functions, defined variables, etc..
HISSAR
200
Procedures
Procedures consist of one or more commands, functions, or variable assignments, separated by newlines or semicolons, and contained within curly braces.
HISSAR
Pattern-Procedures Examples
{ print $1 }
/pattern/
/pattern/ { print $1 }
HISSAR
NF > 2
202
Pattern-Procedures Examples
Interpret input records as a group of lines up to a blank line. Each line is a single field:
Print fields 2 and 3 in switched order, but only on lines whose first field matches the string "URGENT":
HISSAR
Pattern-Procedures Examples
Add numbers in second column and print total:
length($0) < 20
HISSAR
204
Pattern-Procedures Examples
Print each line that begins with Name: and that contains exactly seven fields:
NF == 7 && /^Name:/
Print the fields of each input record in reverse order, one per line:
HISSAR
205
Built-in Variables
Variable
Description
FILENAME Current filename FS NF NR OFS Field separator (a space) Number of fields in current record Number of the current record Output field separator (a space)
HISSAR
206
Built-in Variables
Variable Description
ORS
RS
$0
$n
HISSAR
Operators
Symbol
Meaning
C conditional expression Logical OR Logical AND Match regular expression and negation Relational operators Concatenation
HISSAR
Operators
Meaning Addition, subtraction Multiplication, division, and modulus (remainder) Unary plus and minus, and logical negation Exponentiation Increment and decrement, either prefix or postfix Field reference
HISSAR
User-Defined Functions
function capitalize(input, result, words, n, i, w) { result = "" n = split(input, words, " ") for (i = 1; i <= n; i++) { w = words[i] w = toupper(substr(w, 1, 1)) substr(w, 2) if (i > 1) result = result " " result = result w } return result } # main program, for testing { print capitalize($0) }
HISSAR
210
SED by
Imran Hissar Ahmed
HISSAR
211
INTRODUCTION
HISSAR
212
Objectives
In this session, you learn about:
Introdution to Sed Options with sed Substitution Exaples with shell script
HISSAR
213
Introduction
General Syntax
Sed [options] [stream-commands/stream command file] [Input filename]
HISSAR
214
Sed options
-e:
-f:
-i:
Which is used to store the output file as the extension using the same filename
-n:
HISSAR
Sed Command s
s:
s/day/night/
s|day|night|
s/day/night/g
HISSAR
216
Deleting d:
Sed -e 1d file
Specifying a range
Printing p:
Writing to a file w
HISSAR
217
#!/bin/sh echo -n 'what is the value? ' read value sed s/XXX/$value/ <<EOF The value is XXX EOF
HISSAR
218
Summary
In this session, you learned about Stream Editor Options of Sed Substitutions Working sed with Shell The Here Document
HISSAR
219
UNIX Bibliography
UNIX in a Nutshell for BSD 4.3: A Desktop Quick Reference For Berkeley (O'Reilly & Associates, Inc., 1990, ISBN 0-937175-20-X). UNIX in a Nutshell: A Desktop Quick Reference for System 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). HISSAR
220
Lab Exercises
HISSAR
221
Thank you
HISSAR
222