SlideShare a Scribd company logo
1 of 185
* of 70
UNIX Unbounded 5th Edition
Amir Afzal
Chapter 12
Shell Programming
Copyright ©2008 by Pearson Education, Inc.
Upper Saddle River, New Jersey 07458
All rights reserved.
* of 70
Chapter 12
Exploring the Shell
This chapter explains capabilities of the shell as an interpretive
high-level languageIt describes shell programming constructs
and particularsVariablesFlow controlRunningDebugging
*
* of 70
UNDERSTANDING UNIX SHELL PROGRAMMING
LANGUAGE: AN INTRODUCTIONA command language –
interpreted (not compiled)Shell program files are called shell
procedures, shell scripts, or simply scriptsA file that contains a
series of commands for the shell to execute
* of 70
Writing a Simple Script
* of 70
Executing a Script
Making Files Executable: The chmod Command
* of 70
Executing a Script
Making Files Executable: The chmod Command
* of 70
Executing a Script
Making Files Executable: The chmod Command
* of 70
Executing a Script
Making Files Executable: The chmod Command
chmod u=rwx,g=rx,o=r myfile
chmod 754 myfile # 754 = 1 1 1 1 0 1 1 0 0
r w x r - x r - -
* of 70
WRITING MORE SHELL SCRIPTSBuilt in commands
* of 70
WRITING MORE SHELL SCRIPTS
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008 by Pearson Education, Inc
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008 by Pearson Education, Inc
* of 70
WRITING MORE SHELL SCRIPTS
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008 by Pearson Education, Inc
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008 by Pearson Education, Inc
* of 70
WRITING MORE SHELL SCRIPTSUsing Special Characters
When using the echo command on the prompt sign:
make sure to use –e option for the escape characters to work as
specified
$echo –e “HnEnLnLnO”
When using the echo command inside script files, and you are
using the bash command or chmod +x command to run this cript
make sure to use –e option for the escape characters to work as
specified
echo –e “HnEnLnLnO”
* of 70
WRITING MORE SHELL SCRIPTSUsing Special Characters
When using the echo command on the prompt sign:
make sure to use –e option for the escape characters to work as
specified
$echo –e “HnEnLnLnO”
When using the echo command inside script files, and you are
using the bash command or chmod +x command to run this cript
make sure to use –e option for the escape characters to work as
specified
echo –e “HnEnLnLnO”
* of 70
WRITING MORE SHELL SCRIPTSUsing Special Characters
When using the echo command on the prompt sign:
make sure to use –e option for the escape characters to work as
specified
$echo –e “HnEnLnLnO”
* of 70
WRITING MORE SHELL SCRIPTSLogging Off in Style
* of 70
WRITING MORE SHELL SCRIPTSExecuting Commands: The
dot CommandLets you execute a program in the current shell
and prevents the shell from creating the child process
* of 70
WRITING MORE SHELL SCRIPTSPath Modification
Command SubstitutionPlace the output of a command in the
argument string
* of 70
WRITING MORE SHELL SCRIPTSReading Inputs: The read
Command
* of 70
Reading Inputs: The read Command
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008 by Pearson Education, Inc
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008 by Pearson Education, Inc
* of 70
EXPLORING THE SHELL: PROGRAMMING
BASICSCommentsThe shell recognizes # as the comment
symbol; therefore, characters after the # are ignored.
VariablesWrite the name of the variable, followed by the equal
sign and the value you want to store in the variableNo spaces
either side of the = sign
* of 70
EXPLORING THE SHELL: PROGRAMMING
BASICSDisplaying Variablesthe echo command can be used to
display the contents of variables
Command SubstitutionYou can store the output of a command in
a variable
* of 70
EXPLORING THE SHELL: PROGRAMMING
BASICSCommand Line Parameters
* of 70
Command Line Parameters
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008 by Pearson Education, Inc
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008 by Pearson Education, Inc
* of 70
EXPLORING THE SHELL: PROGRAMMING
BASICSAssigning ValuesPossible to assign values to the
positional variables is to use the set command
Accents
Not single quotes
* of 70
EXPLORING THE SHELL: PROGRAMMING BASICSScenario
– write a script file that does the followingStores the specified
file in a directory called keep in your HOME directoryInvoke
the vi editor to edit the specified file
* of 70
EXPLORING THE SHELL: PROGRAMMING BASICSResult
* of 70
EXPLORING THE SHELL: PROGRAMMING
BASICSTerminating Programs: The exit CommandUse to
immediately terminate execution of your shell program
Conditions and TestsThe if-then construct
* of 70
EXPLORING THE SHELL: PROGRAMMING BASICSThe if-
then-else Construct
* of 70
EXPLORING THE SHELL: PROGRAMMING BASICSThe if-
then-else Construct
no #
Notice the spaces before and after
* of 70
EXPLORING THE SHELL: PROGRAMMING BASICSThe if-
then-elif Construct
* of 70
EXPLORING THE SHELL: PROGRAMMING BASICSThe if-
then-elif Construct
hour= `date +%H’
Replace these
two lines (8 and 9)
* of 70
EXPLORING THE SHELL: PROGRAMMING BASICSThe if-
then-elif Construct
if [ “$answer” = Y ]
* of 70
EXPLORING THE SHELL: PROGRAMMING BASICSTesting
Different CategoriesNumeric Valuesuse the test command to
test (compare) two integer numbers algebraically
* of 70
EXPLORING THE SHELL: PROGRAMMING BASICSScenario
– write a script that:Accepts three numbers as input (command
line arguments) and … Displays the largest of the threeOutput
should look like this:
* of 70
EXPLORING THE SHELL: PROGRAMMING BASICS
if [ “$num1” –gt “$num2” -a “$num1” –gt “$num3” ]
* of 70
EXPLORING THE SHELL: PROGRAMMING BASICSString
ValuesCompare (test) strings with the test command
* of 70
EXPLORING THE SHELL: PROGRAMMING BASICSString
ValuesCompare (test) strings with the test command
* of 70
EXPLORING THE SHELL: PROGRAMMING BASICSFiles –
use the test command to test file characteristicsSize, type,
permissions
* of 70
EXPLORING THE SHELL: PROGRAMMING BASICSFiles –
use the test command to test file characteristicsSize, type,
permissions
if [ -r “$FILE” ]
* of 70
EXPLORING THE SHELL: PROGRAMMING BASICSFiles –
use the test command to test file characteristics
* of 70
EXPLORING THE SHELL: PROGRAMMING
BASICSParameter SubstitutionLets you test the value of a
parameter and change its value according to a specified option
If variable has value
return this value
Else
return the string
If variable has value
return the string
Else
nothing
If variable has value
return this value
Else
variable = string
If variable has value
return this value
Else
print string
* of 70
echo ${var:-"Variable is not set"}
echo "1 - Value of var is ${var}"
echo ${var:="Variable is not set"}
echo "2 - Value of var is ${var}"
unset var
echo ${var:+"This is default value"}
echo "3 - Value of var is $var“
var="Prefix"
echo ${var:+"This is default value"}
echo "4 - Value of var is $var"
echo ${var:?"Print this message"}
echo "5 - Value of var is ${var}"
Variable is not set
1 - Value of var is
Variable is not set
2 - Value of var is Variable is not set
3 - Value of var is
This is default value
4 - Value of var is Prefix
Prefix
5 - Value of var is Prefix
* of 70
EXPLORING THE SHELL: PROGRAMMING
BASICSParameter Substitution
Notice the exit from the script
If name is still empty
* of 70
EXPLORING THE SHELL: PROGRAMMING
BASICSParameter Substitution
* of 70
ARITHMETIC OPERATIONSThe expr Command – Arithmetic
OperatorsNote: spaces between the elements of an expression
are necessary, integer only
* of 70
ARITHMETIC OPERATIONSThe expr Command – Arithmetic
Operators
The * (multiplication) and % (remainder) characters have
special meaning to the shell, they must be preceded by a
backslash []
* of 70
ARITHMETIC OPERATIONSThe expr Command – Arithmetic
Operators
The grave accent marks [` and `] surrounding the command are
necessary and cause the output of the command (expr) to be
substituted.
$ x=10
$ y=20
$ z=`expr $x + $y`
* of 70
$ c=`expr $a + $b`
$ echo “the value of addition=$c”
$ d=`expr $a - $b`
$ echo “the value of subtraction=$d”
$ e=`expr $a * $b`
$ echo “the value of multiplication=$e”
$ f=`expr $a / $b`
$ echo “the value of division=$f”
$ g=`expr $a % $b`
$ echo “the value of modulus=$g”
Show the output of each code segment?
* of 70
ARITHMETIC OPERATIONSRelational OperatorsRelational
operators that work on both numeric and nonnumeric
arguments.If both arguments are numeric, the comparison is
numericIf one or both arguments are nonnumeric, the
comparison is nonnumeric and uses the ASCII values.because >
(greater than) and < (less than) characters have special meaning
to the shell, they must be preceded by a backslash []
n1=10
n2=20
if [ “$n1” > “$n2” ]
then
echo “$n1 is greater than $n2”
else
echo ”$n1 is not greater than $n2”
fi
* of 70
ARITHMETIC OPERATIONSArithmetic Operations: The let
CommandSimpler alternative to the expr command and includes
all the basic arithmetic operations
* of 70
THE LOOP CONSTRUCTSThe For Loop: The for-in-done
ConstructUsed to execute a set of commands a specified number
of times
* of 70
THE LOOP CONSTRUCTSThe For Loop: The for-in-done
Construct
* of 70
for var in "[email protected]"
do
echo "$var"
done
for i in {1..5}
do
echo "Welcome $i times"
done
for num in `seq 1 2 10`
do
echo "$num"
done
* of 70
for (( c=1; c<=5; c++ ))
do
echo "Welcome $c times"
done
exit 0
Spaces are not important here
* of 70
THE LOOP CONSTRUCTSThe For Loop: The for-in-done
Construct
* of 70
THE LOOP CONSTRUCTSThe While Loop: The while-do-done
ConstructThe while loop continues as long as the loop condition
is true.
* of 70
THE LOOP CONSTRUCTSThe While Loop: The while-do-done
Construct
* of 70
THE LOOP CONSTRUCTSThe While Loop: The while-do-done
ConstructCommand line version of the carryon program
* of 70
THE LOOP CONSTRUCTSThe While Loop: The while-do-done
Construct
* of 70
THE LOOP CONSTRUCTSThe While Loop: The while-do-done
Construct
* of 70
THE LOOP CONSTRUCTSThe While Loop: The while-do-done
Construct
* of 70
THE LOOP CONSTRUCTSThe Until Loop: The until-do-done
ConstructSimilar to the while loop, except … It continues
executing the body of the loop as long as the condition of the
loop is falseBody of the until loop might never get executed if
the loop condition is true
* of 70
THE LOOP CONSTRUCTSScenario: check whether a specified
user is on the system or, if not, to be informed as soon as the
user logs in
* of 70
DEBUGGING SHELL PROGRAMSThe Shell CommandUse the
sh, ksh, or bash command with one of its options to make the
debugging of your script files easierOptions
* of 70
DEBUGGING SHELL PROGRAMSThe Shell Command
* of 70
DEBUGGING SHELL PROGRAMSThe Shell Command
* of 70
DEBUGGING SHELL PROGRAMSThe Shell CommandRun the
BOX program with the -v option, and specify some command
line arguments.
* of 70
DEBUGGING SHELL PROGRAMSThe Shell Command
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
UNIX Unbounded 5th Edition
Amir Afzal
Chapter 5
Introduction to the
UNIX File System
Copyright ©2008 by Pearson Education, Inc.
Upper Saddle River, New Jersey 07458
All rights reserved.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
5.1 DISK ORGANIZATION UNIX allows you to divide your
hard disk into many units (called directories),
and subunits (called subdirectories), thereby nesting
directories within
directories. UNIX provides commands to create, organize,
and keep track of directories
and files on the disk.
5.2 FILE TYPES UNDER UNIX
UNIX has three categories of files:
Regular Files
Regular files contain sequences of bytes that could be
programming
code, data, text, and so on.
Directory Files
The directory file is a file that contains information (like the
file name) about other files. It consists of a number of such
records in a special format
defined by your operating system.
Special Files
Special files (device files) contain specific information
corresponding to peripheral devices such as printers, disks, and
so on.
*
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
5.3 ALL ABOUT DIRECTORIES
Directories are an essential feature of the UNIX file system
The directory system provides the structure for organizing
files on a disk
In UNIX, the directory structure is organized in levels and is
known as a
hierarchical structure
The highest level directory is called the root and all other
directories
branch directly or indirectly from it
Figure 5.1 shows the root and some other directories
*
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-1 Directory Structure
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-2 Parent and Child Relationship
The terms parent and child describe the relationship between
levels of the hierarchy.
Figure 5.2 shows this relationship. Only the root directory has
no parents. It is the
ancestor of all the other directories.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
5.3.1 Important Directories
Following are summaries of some of the more important
directories on your UNIX System:
/
This is the root directory. It is the highest-level directory and
all other directories
branch from it
/usr
This directory holds users’ home directories. In other UNIX
systems including
Linux, this can be the /home directory
/usr/docs
This directory holds various documents
/usr/man
This directory holds man (online manual) pages
/usr/games
This directory holds game programs
/usr/bin
This directory holds user-oriented UNIX programs
/home/faculty/jomer
/home/STUDENTS/nonmajors/maamci
$ cd /usr/bin
$ ls
Blue: Directory
Green: Executable or recognized data file
Sky Blue: Linked file
Yellow with black background: Device
Pink: Graphic image file
Red: Archive file
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
/usr/spool
This directory has several subdirectories such as mail, which
holds mail files,
and spool, which holds files to be printed
/usr/sbin
This directory holds system administration files
/bin
This directory holds many of the basic UNIX program files
/dev
This directory holds device files. These are special files that
represent the physical
computer components such as printer or disk
/sbin
This directory holds system files that usually are run
automatically by the UNIX
system.
/etc
This directory and its subdirectories hold many of the UNIX
configuration files
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
5.3.2 The Home Directory
The system administrator creates all user accounts on the
system and
associates each user account with a particular directory
This directory is the home directory
The log on process places you into your home directory
From your home directory you can expand your directory
structure according
to your needs
You can add as many subdirectories as you like and dividing
subdirectories
into additional subdirectories
[email protected]:~$ echo $HOME
/home/faculty/jomer
[email protected]:~$ pwd
/home/faculty/jomer
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
5.3.3 The Working Directory
While you are working on the UNIX system, you are always
associated with a directory. The directory you are associated
with or working in is called the working directory
Figure 5.3 shows that the directory called usr has three
subdirectories called david, daniel, and gabriel. The directory
david contains three files, but the other directories are empty.
[email protected]:~$ echo $HOME
/home/faculty/jomer
[email protected]:~$ pwd
/home/faculty/jomerFigure 5.3 is not the standard UNIX file
structure
Your login name and your home directory name are usually the
same
and are assigned by the system administrator
The root directory is present in all UNIX file structures
The name of the root directory is always the forward slash (/)
4
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-3 Directories, Subdirectories, and Files
Figure 5.3 shows that the directory called usr has three
subdirectories called david, daniel, and gabriel. The directory
david contains three files, but the other directories are empty.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
5.3.4 Understanding Paths and Pathnames
Every file has a pathname. The pathname locates the file in
the file system
You determine a file’s pathname by tracing a path from the
root directory to the
file, going through all intermediate directories
Figure 5.4 shows a hierarchy and the pathnames of its
directories and files.
For example, using Figure 5.4, if your current directory is root,
then the path to a
file (say, myfirst) under the david directory is
/usr/david/myfirst.The forward slash (/) at the very beginning of
a pathname stands for the root directory /usr/david/myfirst.The
other slashes serve to separate the names of the other directories
and files /usr/david/myfirst.
The files in your working directory are immediately accessible.
To access files in another directory you need to specify the
particular file by its pathname
4
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-4 Pathnames in a Directory Structure
Figure 5.4 shows a hierarchy and the pathnames of its
directories and files.
For example, if your current directory is root, then the path to a
file (say, myfirst) under the david directory is
/usr/david/myfirst.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Absolute Pathname An absolute pathname (full pathname) traces
a path from the root
to the file.
An absolute pathname always begins with the name of the root
directory, forward slash (/). For example, if your working
directory is usr, the absolute pathname of the file called myfirst
under the directory david is /usr/david/myfirst.
4
1. The absolute pathname specifies exactly where to find a
file. Thus, it can
be used to specify file location in the working directory or
any other
directory.
2. Absolute pathnames always start from the root directory and
therefore
have a forward slash (/) at the beginning of the pathname.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Relative Pathname A relative pathname is a shorter form of
the pathname. It traces a path
from the working directory to a file
Like the absolute pathname, the relative pathname can
describe a path
through many directories.
For example, if your working directory is usr, the relative
pathname to
the file called REPORT under the david directory is
david/REPORT
There is no initial forward slash (/) for a relative pathname. It
always starts from your current directory.
4
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
5.3.5 Using File and Directory Names Every ordinary and
directory file has a filename.
UNIX gives you much freedom in naming your files and
directories.
Most modern Linux and UNIX limit filename to 255 characters.
However, some older version of UNIX system limits filenames
to 14 characters only.
You name a file using a combination of characters and/or
numbers.
Avoid using the following characters in filenames:
< > …………… less than and greater than signs
( ) …………… open and close parentheses
[ ] …………… open and close brackets
{} …………… open and close braces asterisk or star
? …………… question mark
" …………… double quotation mark
' …………… single quotation mark
– …………… minus sign
$ …………… dollar sign
^ …………… caret
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Choose characters for filenames from the following list:
(A–Z) …………… uppercase letters
(a–z) …………… lowercase letters
(0–9) …………… numbers
(_) …………… underscore
(.) …………… dot (period)
Filename Extensions The filename extension helps to further
categorize and describe the contents
of a file Filename extensions are part of the filename
following a period and in most
cases are optional In Figure 5.5, first.c and first.cpp in the
source directory have typical file
extensions (.c and .cpp for the C and C++ programming
languages,
respectively) The following examples show some filenames
with extensions:
report.c report.o
memo.04.10
The use of more than one period in a file extension is
allowed in UNIX.
4
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-5 An Example of a Directory Structure
5.4 DIRECTORY COMMANDS
Figure 5.5 is your directory structure, and your home directory
is david.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
5.4 DIRECTORY COMMANDS
In the following examples, assume that your login name is
david, Figure 5.5 is
your directory structure, and your home directory is david.
5.4.1 Displaying a Directory Pathname: The pwd Command
The pwd (print working directory) command displays the
absolute pathname of your working (current) directory.
Log in and show the pathname of your home directory:
login: david [Return] . . Enter your login name (david)
password:. . . . . . . . . . .Enter your password.
Welcome to UNIX!
$ pwd [Return] . . . . . . Display your HOME directory path.
/usr/david
$_ . . . . . . . . . . . . . . . . .Prompt for next command.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
4
1. /usr/david is your home directory pathname.
2. /usr/david is also your current or working directory
pathname.
3. /usr/david is an absolute pathname because it begins with /,
tracing
the path of your home directory from the root.
4. david is your login name and your home directory name.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
5.4.2 Changing Your Working Directory: The cd Command
To change your working directory to the source directory, use
the following command sequence:
$ pwd [Return] . . . . . . . . . . Check your current directory.
/usr/david
$ cd source [Return] . . . . . .Change to source directory.
$ pwd [Return] . . . . . . . . . . Display your working directory.
/usr/david/source
$_ . . . . . . . . . . . . . . . . . . . . . Prompt for the next command.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Assuming you have permission, you can change your working
directory to /dev
by using the following command sequence:
$ cd /dev [Return] . . . . Change to /dev directory.
$ pwd [Return] . . . . . . Check your working directory.
/dev . . . . . . . . . . . . . . . Your current directory is /dev.
$_ . . . . . . . . . . . . . . . . Prompt for next command.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
5.4.3 Creating Directories
The very first time you log on to the UNIX system, you begin
work from your home
directory, which is also your working directory.
Advantages of Creating Directories
The following lists some of the advantages of using directories:
• Grouping related files in one directory makes it easier to
remember and
access them.
• Displaying a shorter list of your files on the screen enables
you to find a
file more quickly.
• You can use identical filenames for files that are stored in
different
directories.
• Directories make it feasible to share a large-capacity disk
with other users
with a well-defined space for each user.
• You can take advantage of the UNIX commands that
manipulate directories.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-6 Your Directory Structure at the Beginning
Directory Structure
Let’s start with the directory structure presented in Figure 5.6.
Depending on your system configuration and administration
requirements, you might have other files or subdirectories in
your HOME directory.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
5.4.4 Directory Creation: The mkdir Command
The mkdir (make directory) command creates a new
subdirectory under your working
directory or any other directory you specify as part of the
command.
Create a directory called memos under your HOME directory:
$ cd [Return] . . . . . . . . . . . . . . . Make sure you are in your
HOME directory.
$ mkdir memos [Return] . . . . . . . Create a directory called
memos.
$ pwd [Return] . . . . . . . . . . . . . . Check your working
directory.
/usr/david
$ cd memos [Return]. . . . . . . . . .Change to memos directory.
$ pwd [Return] . . . . . . . . . . . . . . Check your working
directory.
/usr/david/memos . . . . . . . . . . . . Your current directory is
memos.
$_ . . . . . . . . . . . . . . . . . . . . . . . Prompt for next command.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-7 Your Directory Structure After Adding the memos
Subdirectories
Before
After
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
While you are in your HOME directory, create a new
subdirectory called important in
the memos directory.
$ cd [Return] . . . . . . . . . . . . . . . Make sure you are in your
HOME directory.
$ mkdir memos/important [Return] . . . Specify the important
directory
pathname.
$ cd memos/important [Return] . . . .Change to important
directory.
$ pwd [Return] . . . . . . . . . . . . . . . .Check your working
directory.
/usr/david/memos/important
$_ . . . . . . . . . . . . . . . . . . . . . . . . . Now your working
directory is important.
Figure 5.8 shows your directory structure after adding memos
and important subdirectories.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-8 Your Directory Structure After Adding the memos
and important Subdirectories
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-9 Creating the source Directory
Figure 5.9 shows how to create a directory called source under
your HOME directory.
Figure 5.10 shows your directory structure after adding the
source subdirectory.
A directory structure can be created according to your specific
needs.
4
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-10 Your Directory Structure After Adding the source
Directory
Before
After
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
The mkdir Command: -p Option
The -p option creates levels of directories under your current
directory.
Create a directory structure three levels deep, starting in the
HOME directory:
$ cd [Return] . . . . . . . . . . . . . . . . . . Make sure you are in
your HOME directory.
$ mkdir -p xx/yy/zz [Return] . . . . . . Create a directory called
xx; in xx create
a directory called yy,
and in yy create a
directory called zz.
$_ . . . . . . . . . . . . . . . . . . . . . . . . . . Ready for next command.
Figure 5.11 depicts the directory structure after this command
sequence has been
applied.
--parents Option The alternative option in Linux. Like the -p
option, --parents
creates levels of directories under your current or the specified
directory.
The command line for using the --parents is:
$ mkdir --parents xx/yy/zz [Return] . . . . . Create a directory
called xx;
in xx,
create a directory called yy;
and in yy,
create a directory called zz.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-11 Your Directory Structure After Adding the Three-
Levels Deep Subdirectories
Before
After
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
5.4.5 Removing Directories: The rmdir Command
The rmdir (remove directory) command removes (deletes) the
specified directory.
However, it removes only empty directories - directories that
contain no subdirectories
Remove the important directory from your memos directory:
$ cd [Return] . . . . . . . . . . . . . . . . Make sure you are in your
HOME directory.
$ cd memos [Return] . . . . . . . . . . Change your working
directory to memos.
$ pwd [Return] . . . . . . . . . . . . . . Make sure you are in
memos.
/usr/david/memos
$_ . . . . . . . . . . . . . . . . . . . . . . . . Yes, you are in memos.
$ rmdir important [Return] . . . . . Remove the important
directory.
$_ . . . . . . . . . . . . . . . . . . . . . . . . Ready for next command.
1. You were able to remove the important subdirectory because
it was an
empty directory.
2. You must be in a parent directory to remove a subdirectory.
4
Omer, Jalal Sheikh (OJS)
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
From the david directory, remove the source subdirectory:
$ cd [Return] . . . . . . . . . . . . . . Change to david directory.
$ rmdir source [Return] . . . . . . Remove the source directory.
rmdir: source: Directory not empty
$ rmdir xyz [Return] . . . . . . . . Remove a directory called xyz.
rmdir: xyz: Directory does not exist
$_ . . . . . . . . . . . . . . . . . . . . . . Ready for next command.
1. You could not remove the source subdirectory because it
was not an
empty directory.
2. rmdir returns an error message if you give a wrong directory
name
or if it cannot locate the directory name in the specified
pathname.
3. You must be in the parent directory or a higher level of
directory
to remove subdirectories (children).
4
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
5.4.6 Listing Directories: The ls Command
The ls (list) command is used to display the contents of a
specified directory.
It lists the information in alphabetical order by filename
The list includes both filenames and directory names
When no directory is specified, the current directory is listed
If a filename is specified, ls shows the filename with any
other information
requested.
Figure 5.12 is used in the examples and command sequences as
the directory
structure, and subsequent figures show the effect of the example
commands on the files and directories. Remember, a directory
listing contains only the names of the files and
subdirectories.
If no directory name is specified, the default is your current
directory.
A filename does not indicate whether it refers to a file or a
directory.
By default, the output is sorted alphabetically.
4
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-12 The Directory Structure Used for Command
Examples
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Assuming your current directory is david, show the contents of
your HOME directory by typing ls [Return].
$ ls
123
Draft_1
REPORT
memos
myfirst
phones
source
xx
$_
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
In some systems, the output of the ls command is not vertical in
one column and the default format is set to display filenames
across the screen.
$ ls
123 Draft_1 REPORT memos myfirst phones source xx
$_
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
You may want to list the contents of directories other than your
current directory.
While in your HOME directory david, list files in the source
directory:
$ cd [Return] . . . . . . . . . . . Make sure you are in the david
directory.
$ ls source [Return] . . . . . .While in david, display list of files
in the
source directory.
first.c
first.cpp
$_ . . . . . . . . . . . . . . . . . . .Ready for next command.
While in your HOME directory, check whether first.c exists in
the source directory:
$ ls source/first.c [Return]. . . . . . Display the first.c filename
in the source
directory to see
whether it
exists. It does exist, so the file-name is
displayed.
source first.c
$ ls xyz [Return] . . . . . . . . . . . . . Display a file called xyz if
it exists. If
it does not exist, you
get the error
message.
xyz: No such file or directory
$_ . . . . . . . . . . . . . . . . . . . . . . . . . .You get the prompt sign
again.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Table 5.1 The ls Command Options
ls Options
When you need more information about your files or you want
the listing in a different Format, use the ls command with
options.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
4
1. Every option letter is preceded by a minus sign.
2. There must be a space between the command name and the
option.
3. You can use pathnames to list files in a directory other than
your
working directory.
4. You can use more than one option in a single command
line.
Let’s use some of these options and observe their outputs on the
screen.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-13 The ls Command and the -l Option
Option: -l
The most informative option is the -l (long format) option. The
listing produced by
the ls command and -l option shows one line for each file or
subdirectory and displays several columns of information for
each file.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-14 The ls Command-Long Format
Figure 5.14 gives you a general idea about what is in each
column. Look at each
column and see what type of information it conveys.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-15 The ls Command and the -r Option
Option: -r
To display the names of the files in your HOME directory in
reverse order, type ls -r and
press [Return]
Notice the option is the lowercase r.
O
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-16 The ls Command and the -C Option
Option: -C
To display the contents of your current directory in column
format, type:
ls -C [Return]
4
The columns are alphabetically sorted down two columns. This
is the default output format.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-17 The ls Command and the -m Option
Option: -m
To display the contents of your current directory separated by
commas, type ls -m and press [Return].
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Using Multiple Options
You can use more than one option in a single command line.
For example:
To list all files, including invisible (-a option) files, in long
format (-l option), and with the filenames in reverse alphabetic
order (-r option), you type:
ls -alr or ls -a -l –r [Return]
1. You can use one hyphen to start options, but there should be
no space
between the option letters.
2. The sequence of the option letters in the command line is not
important.
3. You can use one hyphen for each option, but there must be a
space
between option letters.
4
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Options: -m -p
List your HOME directory across the screen and indicate each
directory name with a
slash (/).
4
Two options are used:
-m to produce filenames across
the screen
-p to place a slash (/) at the end
of the directory filenames
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Options: -amF
Show all filenames, separated by commas, and to indicate the
directory files with
a slash and executable files with an asterisk, do the following:
-F in Unix
In Linux:
ls --classify
4
1. Tree options are used:
-a to show hidden files
-m to produce filenames across the screen separated by
columns
-F to indicate directories and executable files by placing a
slash (/)
or an asterisk at the end of the filenames respectively
2. The two invisible files (. and ..)are directory files, indicated
by the slash
at the end of the filenames
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Options: -arC
List all the files in your HOME directory, in column format, in
reverse order:
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Options: -s -m
List the files in david, separated by commas, and show the size
of each file:
1. The first field (total 11) shows
total size of files, usually in blocks of
512 bytes.
2. The option -s produces the file size;
each file is at least 1 block
(512 bytes), regardless of how small
the file may be.
4
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Options: -a -x -s
List all files (including hidden files) in david in column format
and also show the file sizes:
4
1. The total size is 13 blocks, since the size of
the two hidden files is added.
2. The -x option formats the columns in a slightly
different manner than -C. Each column is
alphabetically sorted across rather than down
the page.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Options: -R -C
Show the directory structure under david in …
ARRAYSArray is a variable which contains multiple values
may be of same type or different type since by default in shell
script everything is treated as a string. An array is zero-based ie
indexing start with 0.
*
HOW TO DECLARE ARRAY IN SHELL SCRIPTING?
1. Indirect Declaration
Assign a value in a particular index of Array Variable. No need
to first declare
ARRAYNAME[INDEXNR]=value
2. Explicit Declaration
declare -a ARRAYNAME
3. Compound Assignment
ARRAYNAME=(value1 value2 .... valueN)
*
ARRAYS IN BASHbash has two types of arrays: one-
dimensional indexed arrays and associative arrays • Any
variable can be used as a 1D array – Identified as var[index]
$x=24
$ echo ${x[0]}Indexed arrays
• Index array need not be declared though they can be using
the command declare -a
• You can also declare arrays of any size by
declare -a color[3]
with the elements indexed from 0 to 2 –
In reality, the index above is ignored
– You can simply add more elements to it by assigning an
element with a new index
*
ACCESSED BY AN INDEX
– Index starts at 0
– Index can be any positive integer, up to 599147937791
– Arithmetic expressions are supported in index
• Values are assigned by
var[index]=value
• Examples
color[1]="red"
color[2]="green"
color[0]="blue"
– Values need not be assigned in any specific order
Another way to assign values, known as compound statement, is
color=([2]=green [1]=red [0]=blue)
*
If you specify the elements in order, you can specify them as
color=(red blue green)
• The above values can be accessed by
for i in 0 1 2
do
echo ${color[$i]}
done
– You must use the curly braces to access array elements;
otherwise, you’ll just get the first array element and the
subscript
*
*
IFS: internal field separator
*
*
*
*
*
TO COUNT LENGTH OF ARRAY
*
COLOR=(RED BLUE GREEN)
*
COLOR=(RED BLUE GREEN)
*
COLOR=(RED BLUE GREEN)
*
The contents of the colors array is not change, only the output
*
BASH ASSOCIATIVE ARRAY EXAMPLES
*
*
(foo=bar baz=quux corge=grault)
*
QUOTING KEYS
*
$ k=plugh
$ MYMAP['$K']=xyzzy
$ echo ${MYMAP[plugh]}
$ echo ${MYMAP['$K']}
xyzzy
*
ASSOCIATIVEARRAYS
*
Use:
printf instead of print
([apple red] [banana yellow] [grape purple])
LOOPING
*
Use double quotation “
*
*
ECHO
*
*
${MYMAP[${KEYS[$I]}]}
KEYS=(“foo a” “baz b)
SCOPE
*
ARRAYSArray is a variable which contains multiple values
may be of same type or different type since by default in shell
script everything is treated as a string. An array is zero-based ie
indexing start with 0.
*
HOW TO DECLARE ARRAY IN SHELL SCRIPTING?
1. Indirect Declaration
Assign a value in a particular index of Array Variable. No need
to first declare
ARRAYNAME[INDEXNR]=value
2. Explicit Declaration
declare -a ARRAYNAME
3. Compound Assignment
ARRAYNAME=(value1 value2 .... valueN)
*
ARRAYS IN BASHbash has two types of arrays: one-
dimensional indexed arrays and associative arrays • Any
variable can be used as a 1D array – Identified as var[index]
$x=24
$ echo ${x[0]}Indexed arrays
• Index array need not be declared though they can be using
the command declare -a
• You can also declare arrays of any size by
declare -a color[3]
with the elements indexed from 0 to 2 –
In reality, the index above is ignored
– You can simply add more elements to it by assigning an
element with a new index
*
ACCESSED BY AN INDEX
– Index starts at 0
– Index can be any positive integer, up to 599147937791
– Arithmetic expressions are supported in index
• Values are assigned by
var[index]=value
• Examples
color[1]="red"
color[2]="green"
color[0]="blue"
– Values need not be assigned in any specific order
Another way to assign values, known as compound statement, is
color=([2]=green [1]=red [0]=blue)
*
If you specify the elements in order, you can specify them as
color=(red blue green)
• The above values can be accessed by
for i in 0 1 2
do
echo ${color[$i]}
done
– You must use the curly braces to access array elements;
otherwise, you’ll just get the first array element and the
subscript
*
*
IFS: internal field separator
*
*
*
*
*
TO COUNT LENGTH OF ARRAY
*
COLOR=(RED BLUE GREEN)
*
COLOR=(RED BLUE GREEN)
*
COLOR=(RED BLUE GREEN)
*
The contents of the colors array is not change, only the output
*
BASH ASSOCIATIVE ARRAY EXAMPLES
*
*
(foo=bar baz=quux corge=grault)
*
QUOTING KEYS
*
$ k=plugh
$ MYMAP['$K']=xyzzy
$ echo ${MYMAP[plugh]}
$ echo ${MYMAP['$K']}
xyzzy
*
ASSOCIATIVEARRAYS
*
Use:
printf instead of print
([apple red] [banana yellow] [grape purple])
LOOPING
*
Use double quotation “
*
*
ECHO
*
*
${MYMAP[${KEYS[$I]}]}
KEYS=(“foo a” “baz b)
SCOPE
*
* of 44
*created by: Aho, Weinberger, and Kernighanscripting language
used for manipulating data and generating reportsversions of
awkawk, nawk, mawk, pgawk, … GNU awk: gawk
* of 44
What can you do with awk?awk operation:scans a file line by
line splits each input line into fieldscompares input line/fields
to patternperforms action(s) on matched linesUseful
for:transform data filesproduce formatted reportsProgramming
constructs:format output linesarithmetic and string
operationsconditionals and loops
*
* of 44
The Command: awk
*
* of 44
Simple awk commandawk ‘Pattern { Command }’ inputFile
$ cat textfile
Line number 1
Line number 2
Line number 3
Line number 4
Line number 5
$ awk ‘/4/ {print }’ textfile
Line number 4
condition
action
* of 44
Basic awk Syntaxawk [options] ‘script’ file(s)
$ awk ‘/4/ {print }’ textfile
awk [options] –f scriptfile file(s)
Options:
-F to change input field separator
-F: or -F,
-f to name script file
Since awk itself can be a complex language, you can store all
the commands in a file and run it with the –f flag
*
The AWK/NAWK Utility
The AWK/NAWK Utility
Copyright Department of Computer Science, Northern Illinois
University, 2004
*
Copyright Department of Computer Science, Northern Illinois
University, 2004
* of 44
Basic awk Programconsists of patterns & actions:
pattern {action}
if pattern is missing, action is applied to all linesif action is
missing, the matched line is printedmust have either pattern or
action
Example:
$ awk '/for/' testfile prints all lines containing string “for” in
testfile
$ awk ‘{print }’ testfile
- print all lines in testfile
*
The AWK/NAWK Utility
The AWK/NAWK Utility
Copyright Department of Computer Science, Northern Illinois
University, 2004
*
Copyright Department of Computer Science, Northern Illinois
University, 2004
* of 44
Basic Terminology: input fileA field is a unit of data in a
lineEach field is separated from the other fields by the field
separatordefault field separator is whitespaceA record is the
collection of fields in a lineA data file is made up of records
*
Example Input File
The AWK/NAWK Utility
The AWK/NAWK Utility
Copyright Department of Computer Science, Northern Illinois
University, 2004
*
Copyright Department of Computer Science, Northern Illinois
University, 2004
* of 44
Buffersawk supports two types of buffers:
record and field
field buffer:one for each fields in the current record.names: $1,
$2, …
record buffer :$0 holds the entire current record
*
* of 44
Some System Variables
FS Field separator (default=whitespace)
RS Record separator (default=n)
NF Number of fields in current record
NR Number of the current record
OFS Output field separator (default=space)
ORS Output record separator (default=n)
FILENAME Current filename
*
* of 44
Example: Records and Fields
$ cat emps
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500
$ awk '{print NR, $0}' emps
1 Tom Jones 4424 5/12/66 543354
2 Mary Adams 5346 11/4/63 28765
3 Sally Chang 1654 7/22/54 650000
4 Billy Black 1683 9/23/44 336500
*
No pattern, just action
on each record (line)
The AWK/NAWK Utility
The AWK/NAWK Utility
Copyright Department of Computer Science, Northern Illinois
University, 2004
*
Copyright Department of Computer Science, Northern Illinois
University, 2004
* of 44
Example: Space as Field Separator
$ cat emps
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500
$ awk '{print NR, $1, $2, $5}' emps
1 Tom Jones 543354
2 Mary Adams 28765
3 Sally Chang 650000
4 Billy Black 336500
*
No pattern, just action
on each record (line)
The AWK/NAWK Utility
The AWK/NAWK Utility
Copyright Department of Computer Science, Northern Illinois
University, 2004
*
Copyright Department of Computer Science, Northern Illinois
University, 2004
* of 44
Example: Colon as Field Separator
$ cat em2
Tom Jones:4424:5/12/66:543354
Mary Adams:5346:11/4/63:28765
Sally Chang:1654:7/22/54:650000
Billy Black:1683:9/23/44:336500
$ awk -F: '/Jones/{print $1, $2}' em2
Tom Jones 4424
*
Pattern and action
on each record (line)
The AWK/NAWK Utility
The AWK/NAWK Utility
Copyright Department of Computer Science, Northern Illinois
University, 2004
*
Copyright Department of Computer Science, Northern Illinois
University, 2004
* of 44
BEGIN And END BlocksTwo special patterns that can be
matchedBEGINCommands are executed before any records are
looked atENDCommands are executed after all records are
processed
* of 44
Example
$cat textfile
Line number 1
Line number 2
Line number 3
Line number 4
Line number 5
$ awk '/4/ {print $0} BEGIN {print "hello"} END {print
"goodbye"}' textfile
Hello
Line number 4
goodbye
$
Step 1
Step 2
Step 3
* of 44
$ ls | awk ' BEGIN {print "List of html files:" } /.html$/
{print} END { print "There you go !" }'
List of html files:
as1.html
as2.html
index.html
There you go !
* of 44
Awk Patterns/regular expression/ Relational expression>, <, >=,
<=, ==Pattern && patternPattern || patternPattern1 ? Pattern2 :
pattern3If Pattern1 is True, then Pattern2, else pattern
3(pattern)! Pattern
* of 44
Example Patterns
$ cat textfile2
Just a text file Nothing to see here
Some lines have
More fields than others
And some
Are blank
$ awk 'NF > 3 {print $0}' textfile2
Just a text file Nothing to see here
More fields than others
$ awk 'NF > 3 || /^$/ {print $0}' textfile2
Just a text file Nothing to see here
More fields than others
$ awk 'NF > 3 ? /file/ : /^And/ {print $0}' textfile2
Just a text file Nothing to see here
And some
* of 44
Awk ActionsEnclosed in { }() Grouping$ Field reference++ --
Increment, decrement^ Exponentiation+ - ! Plus, minus, not* /
% Multiplication, division, and modulus
* of 44
*
* of 44
*
$ awk '{print $1, $2 * $3}' empsrh
John 325
Smith 420
Tom 117
George 756
Sam 132
$cat empsrh
John 13 25
Smith 14 30
Tom 9 13
George 21 36
Sam 11 12
* of 44
*
$ awk '{print "total pay for", $1, " is ", $2 * $3}' empsrh
total pay for John is 325
total pay for Smith is 420
total pay for Tom is 117
total pay for George is 756
total pay for Sam is 132
$ cat empsrh
John 13 25
Smith 14 30
Tom 9 13
George 21 36
Sam 11 12
* of 44
*
* of 44
*
* of 44
Example: Computing with awk
$cat empsrh
John 13 25
Smith 14 30
Tom 9 13
George 21 36
Sam 11 12
$ awk '$3 > 15 {emp = emp +1} END {print emp, “employees
worked more than 15 hours"}' empsrh
3 employees worked more than 15 housr
* of 44
Example: Computing with awk
$cat empsrh
John 13 25
Smith 14 30
Tom 9 13
George 21 36
Sam 11 12
$awk '{pay = pay + $2 * $3} END {print NR, "employees";
print "total pay is", pay; print "average pay is", pay/NR}'
empsrh
5 employees
total pay is 1750
average pay is 350
* of 44
*
$ awk '$2 > maxrate {maxrate = $2; maxemp =$1}
END {print "Highest rate is:", maxrate, "for", maxemp}’
emprate
* of 44
awk
$ cat em2
Tom Jones:4424: 5/12/66:543354
Mary Adam:5346:11/4/63:28765
Sally Chang:1654:7/22/54:650000
Billy Black:1683:9/23/44:336500
$ awk -F: '{names=names $1 " "} END {print names}' em2
Tom Jones Mary Adam Sally Chang Billy Black
*
* of 44
Example: I want to go through and calculate the average score
on the Midterm
$ cat grades
Jason William:Midterm:100
Jane Smith:Quiz 1:45
Tom Ram:Final:78
Sarah George:Midterm:23
Franklin Rob:Midterm:46
$ awk -F: '/Midterm/ {count++; sum= sum + $3}
BEGIN {count=0 ; sum=0} END {print sum/count}' grades
56.3333
$
* of 44
Another Example
Adding 12 points to everyone’s midterm score
$ cat grades
Jason William:Midterm:100
Jane Smith:Quiz 1:45
Tom Ram:Final:78
Sarah George:Midterm:23
Franklin Rob:Midterm:46
$awk -F: '/Midterm/ {$3 =$3 + 12; print $0} /Quiz/ || /Final/
{print $0}' grades
Jason William Midterm 112
Jane Smith:Quiz 1:45
Tom Ram:Final:78
Sarah George Midterm 35
Franklin Rob Midterm 58
* of 44
Example
$ colors=(red blue orange green purple)
$ echo ${colors[@]} | awk '{for (i=NF; i > 0; --i) print $i}’
purple
green
orange
blue
red
* of 44
awk Versus bash $ argumentsAlways enclose everything to awk
in single quotes
$1 to awk means something completely different than $1 to
bash$1 in awk means first field$1 in bash means first command
line argument
* of 44
User Defined Variables
Variable names could be anything, but it can’t begin with a
number.
You can assign a variable as in shell scripting like this:
$ cat script0
BEGIN {
test="This is a test"
print test
}
$ awk -f script0
This is a test
* of 44
Example: script in a file
$ cat testfile
{print $1 "home at " $6}
$ awk -F: -f testfile /etc/passwd
kuskarhome at /home/STUDENTS/majors/kuskar
juswstahome at /home/STUDENTS/majors/juswsta
dusgmoohome at /home/STUDENTS/nonmajors/dusgmoo
jerpcamhome at /home/STUDENTS/majors/jerpcam
pralam6home at /home/STUDENTS/majors/pralam6
jonnrob1home at /home/STUDENTS/majors/jonnrob1
….
$ cat testfile2
{
text = $1 "home at " $6
print text
}
$ head /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
* of 44
$ cat testfile3
BEGIN {
print "users and their corresponding home"
print "UserName t HomePath"
print "_________ t __________"
FS=":"
}
{
print $1 " t " $6
}
END {
print "The end"
}
$ awk -f testfile3 /etc/passwd
users and their corresponding home
UserName HomePath
_________ ________
root /root
daemon /usr/sbin
bin /bin
sys /dev
sync /bin
games /usr/games
$ head /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
* of 44
Sometimes, the fields are distributed without a fixed separator.
In these cases, FIELDWIDTHS variable solves the problem.
$ cat testfile4
1235.96521
927-8.3652
36257.8157
$awk 'BEGIN {FIELDWIDTHS="3 4 3"}{print $1, $2, $3}'
testfile4
123 5.96 521
927 -8.3 652
362 57.8 157
* of 44
Suppose that your data are distributed on different lines
$ cat testfile5
Jalal Omer
123 High Street
(222) 466-1234
James Smith
456 High Street
(333) 456-7890
$ awk 'BEGIN {FS="n"; RS=""} {print $1," ", $2," ", $3}'
testfile5
Jalal Omer 123 High Street (222) 466-1234
James Smith 456 High Street (333) 456-7890
* of 44
$ cat testfile6
{
if ($1 > 30)
{
x = $1 * 3
print x
}
else
{
x = $1 / 2
print x
}
}
$ awk -f testfile6 numbers
5
7.5
3
99
135
10
4
11
$ cat numbers
10
15
6
33
45
20
8
22
* of 44
While Loop
You can use the while loop to iterate over data with a condition.
$ cat testfile7
{
sum = 0
i = 1
while (i < 4)
{
sum += $i
i++
}
average = sum / 3
print "Average: ", average
}
$ awk -f testfile7 nums
Average: 127
Average: 129.667
Average: 192.667
Average: 165.333
$ cat nums
124 127 130
112 142 135
175 158 245
118 231 147
For each input line do
From here
To
Here
* of 44
You can exit the loop using break command like this:
$ cat testfile8
{
sum = 0
i = 1
while (i < 4)
{
sum += $i
i++
if (i == 3)
break
}
average = sum / 3
print "Average: ", average
}
[email protected]:~$ awk -f testfile8 nums
Average: 83.6667
Average: 84.6667
Average: 111
Average: 116.333
$ cat nums
124 127 130
112 142 135
175 158 245
118 231 147
Wrong averages.
Why?
* of 44
The for Loop
$ cat testfile9
{
sum = 0
for (i =1; i < 4; i++)
{
sum += $i
}
average = sum / 3
print "Average: ", average
}
[email protected]:~$ awk -f testfile9 nums
Average: 127
Average: 129.667
Average: 192.667
Average: 165.333
$ cat nums
124 127 130
112 142 135
175 158 245
118 231 147
* of 44
Mathematical Functions
sin(x) cos(x) sqrt(x) exp(x) log(x) rand()
$ awk 'BEGIN{x=rand(); y= sqrt(16); print x, y}'
0.237788 4
String Functions
$ awk 'BEGIN{x="likegeeks"; print toupper(x)}'
LIKEGEEKS
* of 44
User Defined Functions
$ cat testfile10
function myfunc()
{
printf "The user %s has home path at %s n", $1, $6
}
BEGIN {FS=":"}
{
myfunc()
}
$ awk -f testfile10 /etc/passwd
The user root has home path at /root
The user daemon has home path at /usr/sbin
The user bin has home path at /bin
The user sys has home path at /dev
The user sync has home path at /bin
The user games has home path at /usr/games
For each input line do
Call this function
$ head /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
* of 44
Awk Text Split into an array
$ echo "12 23 11" | awk '{split($0, a); print a[3], a[2], a[1]}'
11 23 12
$ echo "12,23,11" | awk '{split($0, a,","); print a[3], a[2], a[1]}'
11 23 12
$ awk ‘{split($0, arr, “:”), print arr[4], arr[1]}’ inputfile
* of 44
$ cat file1
Item1,200
Item2,500
Item3,900
Item2,800
Item1,600
$ awk -F, '{print > $1}' file1
$ cat Item1
Item1,200
Item1,600
$ cat Item3
Item3,900
$ cat Item2
Item2,500
Item2,800
$ awk -F, '{print > $1".txt"}' file1
$ ls *.txt
Item2.txt Item1.txt Item3.txt
Splitting a file into several files
Print the entire line
Into a file named $1
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-5: The Old Password Prompt
3.1.2 Changing Your Password: The passwd Command
The passwd command changes your current password. If you do
not have a password, it creates one. Type passwd and press
[Return]
(UNIX displays the Old password: prompt) Enter your
current password and press [Return]
(UNIX displays the New password: prompt)
Figure 3-6 New Password Prompt
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-7 Re-Enter New Password Prompt Enter your new
password
(UNIX shows the Re-enter new password: prompt)
Retype the new password
(UNIX is verifies that you did not make a mistake)
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-8 Sample of Error Messages
Password Format
Your password must comply with the following criteria:
• The new password must differ from the old one by at least
three characters
• The password must be at least six characters long and must
contain at least two
characters and one number
• The password must differ from your User ID
If UNIX detects anything wrong with your password, it displays
an error message and shows the New password: prompt again.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-9 Changing a Password in the Linux Environment
The wordings of the prompts are slightly different for the Linux
system, but the
command and sequence of prompts are the same.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-10 Sample of Error Messages
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
3.1.3 General Rules for Choosing Passwords
• Do not use a dictionary word (in any language)
• Do not use the name of a person, pet, location, or a
character from a book
• Do not use any variation of your name and ID/account name
• Do not use known information about you, such as your
phone number, birthday,
and so on
• Do not use a simple pattern or easy sequence of keyboard
keys
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
3.1.4 Logging Off
The process of signing off when you have finished with the
system is called
logging off or logging out
To log off, press [Ctrl-d], which means to simultaneously
hold down the key
labeled Ctrl (for Control) and press the letter d
UNIX responds first by displaying logging off messages
Then UNIX shows the system’s standard welcome message
and login: prompt
exit command
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-11: The Logging-In and Logging-Out Process
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-12 The Command Line Format
3.2.2 Basic Command Line Structure
Each command line consists of three fields:
• Command name
• Options
• Arguments
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-13 The date Command
3.2.3 Date and Time Display: The date Command
The date command displays the current date and time on the
screen
The date and time are set by the system administrator and
users cannot
change them
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
3.2.4 Information on Users: The who Command
The who command lists the login names, terminal lines, and
login times of
the users who are currently logged on the system
The who command is used to check the level of activity in the
system or to
find out whether a particular person is on the system
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-14 The who Command
1. The first column shows the login name of the user
2. The second column identifies the terminal being used
3. The tty number gives you some indication about the location
of the terminal
4. The third and fourth columns show the date and time that
each user logged in
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-15: The who Command with am i Argument Type
who am I or who am I
(UNIX displays who the system thinks you are)
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
who Options
Table 3.1 lists some of the who command options Linux also
provides some alternative and new options
Under Linux, some of these options are not available, or some
of the options outputs are slightly different
1. Linux new and alternative options are listed under the Linux
heading in Table 3.1
2. Linux new and alternative options are preceded by two minus
signs (--)
Table 3.1 The who Command OptionsOptionLinuxOperation-q-
-countThe quick who; just displays the name and number of
users-H--headingDisplays heading above each column-
bDisplays the time and date of the last reboot--helpDisplays a
usage message
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-16A The who Command with -H Option
Figure 3-16B The who Command Using Linux --heading
Option
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-17A The who Command with -q Option
Figure 3-17B The who Command Using Linux --count Option
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-18 The who Command with -b Option
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-19 The who Command Using Linux --help Option
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
3.2.5: Display a Calendar: The cal Command
The cal command displays the calendar for the specified year.
If the year and the month are both specified, then the calendar
for just that
month is displayed
The default argument for the cal command is the current
month
Displaying the calendar for the current month (see Figure 3.20).
(Assuming current month, year is November 2005)
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-20 The cal Command with No Option
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-21 The cal Command Output
Notes:Type the specified year in full year value. For example,
type cal 2010 (not cal 10)
Use the month number (1 to 12) and not the month name
The cal command without arguments displays calendar of the
current month
The cal command with year argument but without month
argument displays a calendar for the specified year
Displaying the calendar for November 2010.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-22 Learn Utility Main Menu
3.3 GETTING HELP
3.3.1 Using the learn Command
The learn command brings up a computer-aided instruction
program that is arranged in a series of courses and lessons.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-23 Help Utility Main Menu
3.3.2 Using the help Command
The help command is more popular than the learn command and
is available on many UNIX systems.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
3.3.3 Getting More Information: The UNIX Manual
You can find a detailed description of the UNIX system in a
large document called the User’s Manual. Some installation
may have a printed copy of this manual
The electronic version of the manual is stored on disk and is
called the
online manual
The UNIX user’s manual is tersely written and difficult to read
It is more like a reference guide than a true user’s manual
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
3.3.4 Using the Electronic Manual: The man Command
The man (manual) command shows pages from the online
system documentation.
Type man followed by the name of the command
You can use man to get the details about the commands
For example type:
man cal [Return]
(UNIX responds by showing a page similar to Figure
3.24)
Note:
Nearly all UNIX installations provide the man pages, and the
man command is the most popular way to obtain detailed
information about command usage and options.
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-24 man Utility Display of cal
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
3.4 CORRECTING TYPING MISTAKES
The shell program interprets the command line after you press
the [Return] key.
Erasing Characters Use the [Backspace] key for erasing
characters or use [Ctrl-h] once for each
character you intend to erase
Erasing an Entire Line You can erase an entire line any time
before pressing [Return]
[Ctrl-u] removes the entire command line and the cursor
moves to a blank line
The character that erases the entire line is called the kill
character
Terminating Program Execution The character that
terminates your running program is called the interrupt
character
On most systems, [Del] or [Ctrl-c] is assigned as the interrupt
character
The interrupt character stops the running program and causes
the shell prompt $ to
be displayed
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
* of 45
Chapter 3: Getting Started
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
Figure 3-25 UNIX Error Message
Amir Afzal
UNIX Unbounded, 5th Edition
Copyright ©2008
*
Introduction
to
Unix and Linux
*
What is an Operating System?The operating system (OS) is the
program which starts up when you turn on your computer and
runs underneath all other programs - without it nothing would
happen at all.In simple terms, an operating system is a manager.
It manages all the available resources on a computer, from the
CPU, to memory, to hard disk accesses.Tasks the operating
system must perform:Control Hardware - The operating system
controls all the parts of the computer and attempts to get
everything working together. Run Applications - Another job
the OS does is run application software. This would include
word processors, web browsers, games, etc... Manage Data and
Files - The OS makes it easy for you to organize your computer.
Through the OS you are able to do a number of things to data,
including copy, move, delete, and rename it. This makes it much
easier to find and organize what you have.
*
*
*
Operating System FunctionsInitialize computer
hardwareAllocate system resources to programs Keep track of
multiple programs running at same timeProvide organized
method for all programs to use system devices
UNIX Structure
Introduction to Linux
*
*
Structure
*
Parts of the UNIX OSThe Kernel - handles memory
management, input and output requests, and program
scheduling. Technically speaking, the kernel is the OS. It
provides the basic software connection to the hardware.
The Shell and Graphical User Interfaces (GUIs) - basic UNIX
shells provides a “command line” interface which allows the
user to type in commands. These commands are translated by
the shell into something the kernel can comprehend, and then
executed by the kernel.
The Built-in System Utilities - are programs that allow a user to
perform tasks which involve complex actions. Utilities provide
user interface functions that are basic to an operating system,
but which are too complex to be built into the shell. Examples
of utilities are programs that let us see the contents of a
directory, move & copy files, remove files, etc...
Application Software & Utilities – these are not part of the
operating system, per se. They are additional programs that are
bundled with the OS distribution, or available separately. These
can range from additional or different versions of basic utilities,
to full scale commercial applications.
*
*
UNIX
Unix is a multi-user, multi-tasking operating system.
You can have many users logged into a system simultaneously,
each running many programs.
It's the kernel's job to keep each process and user separate and
to regulate access to system hardware, including cpu, memory,
disk and other I/O devices.
*
*
General Characteristics of UNIX as an Operating System
(OS)Multi-user & Multi-tasking - most versions of UNIX are
capable of allowing multiple users to log onto the system, and
have each run multiple tasks. This is standard for most modern
OSs. Over 40 Years Old - UNIX is over 40 years old and it's
popularity and use is still high. Over these years, many
variations have spawned off and many have died off, but most
modern UNIX systems can be traced back to the original
versions. It has endured the test of time. For reference,
Windows at best is half as old (Windows 1.0 was released in the
mid 80s, but it was not stable or very complete until the 3.x
family, which was released in the early 90s). Large Number of
Applications – there are an enormous amount of applications
available for UNIX operating systems. They range from
commercial applications such as CAD, Maya, WordPerfect, to
many free applications. Free Applications and Even a Free
Operating System - of all of the applications available under
UNIX, many of them are free. The compilers and interpreters
that we use in most of the programming can be downloaded free
of charge. Less Resource Intensive - in general, most UNIX
installations tend to be much less demanding on system
resources. In many cases, the old family computer that can
barely run Windows is more than sufficient to run the latest
version of Linux. Internet Development - Much of the backbone
of the Internet is run by UNIX servers. Many of the more
general web servers run UNIX with the Apache web server -
another free application.
*
*
History of UNIX
First Version was created in Bell Labs in 1969.
Some of the Bell Labs programmers who had worked on this
project, Ken Thompson, Dennis Ritchie, Rudd Canaday, and
Doug McIlroy designed and implemented the first version of the
Unix File System on a PDP-7 along with a few utilities. It was
given the name UNIX by Brian Kernighan.
00:00:00 Hours, Jan 1, 1970 is time zero for UNIX. It is also
called as epoch.
*
*
History of UNIX
1973 Unix is re-written mostly in C, a new language developed
by Dennis Ritchie.
Being written in this high-level language greatly decreased the
effort needed to port it to new machines.
*
*
History of UNIX
Introduction to Linux
1977 There were about 500 Unix sites world-wide.
1980 BSD 4.1 (Berkeley Software Development)
1983 SunOS, BSD 4.2, System V
1988 AT&T and Sun Microsystems jointly develop System V
Release 4 (SVR4). This later developed into UnixWare and
Solaris 2.
1991 Linux was originated.
An interesting and rather up-to-date timeline of these variations
of UNIX can be found at
http://www.levenez.com/unix/history.html.
*
*
Flavors of UNIXThese can be grouped into two categories:
Open Source and TrademarkedTrademarked : (redistribution
and modification prohibited or restricted; not free)Solaris .
IRIXMac OS X . and many others... Open Source: (source code
is readily available and free to modify)FreeBSDLinux
Distributions:RedHat and the Fedora Project (maintained by
RedHat)MandrakeDebianSuSESlackwareUbuntuand many
others...As a side note, Linux is a open source UNIX-based OS
that was originally developed in 1991 by Linus Torvalds, a
Finnish undergraduate student.
*
*
What is Linux?A clone of UnixDeveloped in 1991 by Linus
Torvalds, a Finnish graduate studentInspired by and
replacement of Minix
Minix was a mini-OS originally developed by Andrew
Tanenbaum to teach of the fundamentals of operating system
design Linus' Minix became LinuxConsist ofLinux KernelGNU
(GNU is Not Unix) Software
differs from Unix by being free software and containing no
Unix code.Software Package management
*
The GNU Linux project was created for the development of
a Unix-like operating system that comes with source code that
can be copied, modified, and redistributed
What is LINUX
Introduction to Linux
Linux is a free Unix-type operating system originally created by
Linus Torvalds with the assistance of developers around the
world.
The Kernel version 1.0 was released in 1994 and today the most
recent stable version is 2.6.9
Developed under the GNU General Public License , the source
code for Linux is freely available to everyone.
*
*
What is Linux?Originally developed for 32-bit x86-based
PCPorted to other architectures, eg.Alpha, VAX, PowerPC, IBM
S/390, MIPS, IA-64PS2, TiVo, cellphones, watches, Nokia
N810, NDS, routers, NAS, GPS, …
*
Which Linux Distribution is better?> 300 Linux
DistributionsSlackware (one of the oldest, simple and stable
distro.)RedhatRHEL (commercially support)Fedora
(free)CentOS (free RHEL, based in England)SuSe ( based in
German)Gentoo (Source code based)Debian (one of the few
called GNU/Linux)Ubuntu (based in South Africa)Knoppix
(first LiveCD distro.)…
*
*
The Free Software Foundation
and the GNU ProjectFree software foundation (FSF)Software
itself should not be restricted in distribution by standard
commercial license agreementGNU projectCompletely free
version of UNIXWritten from scratch
*
The Free Software Foundation and the GNU Project
(continued)Software licenseLegal definition of who can use
software and how it can be usedGNU general public license
(GPL) Very different from standard commercial software
licenseAuthor agrees to give away source codeAnyone is
licensed to redistribute it in any form
*
Linux ArrivesLinus TorvaldsDecided to create UNIX-like
operating system kernel for IBM-compatible PCSolicited help
via InternetReleased Linux kernel under GPLLinux development
methodPerson identifies need and begins writing
programDeveloper announces project on Internet
*
Linux Arrives (continued)Linux development method
(continued)Others respond and work on different parts of
projectPerson leading project releases softwarePeople download
source code and try program; send back information about
problems Developers fix bugsForkingCreating new project
based on existing source code
*
Motivating Free Software DevelopersWhy would so many
people devote so much effort to something without expecting
any reward?Fills developer’s specific technical needRespect of
like-minded professionalsSense of contribution and
communityValuable boost to developer’s resume
*
The Strengths Of
LinuxStabilitySecuritySpeedCostMultiprocessing and other
high-end featuresApplications
*
Hardware RequirementsCan run on very minimal
hardwareRecommend that computer have minimum of:1 GB of
free disk space 64 MB of RAMFor Red Hat Enterprise Linux
installations:256 MB of RAM300 MHZ CPU800 MB of free
disk space
*
Version NumberingVersion numbers assigned to:Each release of
Linux kernel Each component of Linux distribution Linux
distributionsMost users select latest available version
*
Linux CertificationIndustry certification programsRed Hat
Certified TechnicianRed Hat Certified EngineerLPI
Certification (Linux Professional Institute)Linux Certified
Administrator (LCA) CertificationLinux+ CertificationNovell
Certified Linux Engineer
*
Linux Certification (continued)Red Hat’s certification
programVery highly regardedTraining program consists of three
courses
*
The Work of a System AdministratorLinux is increasingly part
of information technology infrastructure of large
organizationsKnowledge of Linux can set you on path to a
fulfilling and profitable career
*
Careers in LinuxSystem administrator Network administrator
Software engineerTrainer Technical writer Product marketing
Business consultant
ECU CS server Unix version
[email protected]:~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.3 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.3 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/term
s-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
*
BASH SHELL PROGRAMMINGInputprompting usercommand
line argumentsDecision:if-then-elsecaseRepetitiondo-while,
repeat-untilforselectFunctionsTraps
*
USER INPUTshell allows to prompt for user input
Syntax:
read varname [more vars]
or
read –p "prompt" varname [more vars]
words entered by user are assigned to
varname and “more vars”last variable gets rest of input
line
*
USER INPUT EXAMPLE
#! /bin/sh
read -p "enter your name: " first last
echo "First name: $first"
echo "Last name: $last"
*
BASH CONTROL STRUCTURESif-then-
elsecaseloopsforwhileuntilselect
*
RELATIONAL OPERATORS
*MeaningNumericStringGreater than-gtGreater than or equal-
geLess than-ltLess than or equal-leEqual-eg= or ==Not equal-
ne!=str1 is less than str2str1 < str2str1 is greater str2str1 >
str2String length is greater than zero-n strString length is zero-z
str
COMPOUND LOGICAL EXPRESSIONS
! not
&& and
|| or
*
and, or
must be enclosed within
[[ ]]
EXAMPLE: USING THE ! OPERATOR
#!/bin/bash
read -p "Enter years of work: " Years
if [ ! "$Years" -lt 20 ]; then
echo "You can retire now."
else
echo "You need 20+ years to retire"
fi
*
EXAMPLE: USING THE && OPERATOR
#!/bin/bash
Bonus=500
read -p "Enter Status: " Status
read -p "Enter Shift: " Shift
if [[ "$Status" = "H" && "$Shift" = 3 ]]
then
echo "shift $Shift gets $$Bonus bonus"
else
echo "only hourly workers in"
echo "shift 3 get a bonus"
fi
*
EXAMPLE: USING THE || OPERATOR
#!/bin/bash
read -p "Enter calls handled:" CHandle
read -p "Enter calls closed: " CClose
if [[ "$CHandle" -gt 150 || "$CClose" -gt 50 ]]
then
echo "You are entitled to a bonus"
else
echo "You get a bonus if the calls"
echo "handled exceeds 150 or"
echo "calls closed exceeds 50"
fi
*
FILE TESTING
Meaning
-d file True if ‘file’ is a directory
-f file True if ‘file’ is an ord. file
-r file True if ‘file’ is readable
-w file True if ‘file’ is writable
-x file True if ‘file’ is executable
-s file True if length of ‘file’ is nonzero
*
EXAMPLE: FILE TESTING
#!/bin/bash
echo "Enter a filename: "
read filename
if [ ! –r "$filename" ]
then
echo "File is not read-able"
exit 1
fi
*
EXAMPLE: FILE TESTING
#! /bin/bash
if [ $# -lt 1 ]; then
echo "Usage: filetest filename"
exit 1
fi
if [[ ! -f "$1" || ! -r "$1" || ! -w "$1" ]]
then
echo "File $1 is not accessible"
exit 1
fi
*
EXAMPLE: IF… STATEMENT
# The following THREE if-conditions produce the same result
* DOUBLE SQUARE BRACKETS
read -p "Do you want to continue?" reply
if [[ $reply = "y" ]]; then
echo "You entered " $reply
fi
* SINGLE SQUARE BRACKETS
read -p "Do you want to continue?" reply
if [ $reply = "y" ]; then
echo "You entered " $reply
fi
* "TEST" COMMAND
read -p "Do you want to continue?" reply
if test $reply = "y"; then
echo "You entered " $reply
fi
*
EXAMPLE: IF..ELIF... STATEMENT
#!/bin/bash
read -p "Enter Income Amount: " Income
read -p "Enter Expenses Amount: " Expense
let Net=$Income-$Expense
if [ "$Net" -eq "0" ]; then
echo "Income and Expenses are equal - breakeven."
elif [ "$Net" -gt "0" ]; then
echo "Profit of: " $Net
else
echo "Loss of: " $Net
fi
*
THE CASE STATEMENTuse the case statement for a decision
that is based on multiple choices
Syntax:
case word in
pattern1) command-list1
;;
pattern2) command-list2
;;
patternN) command-listN
;;
esac
*
CASE PATTERNchecked against word for matchmay also
contain:
*
?
[ … ]
[:class:]multiple patterns can be listed via:
|
*
[:digit:]
Digits: 0 1 2 3 4 5 6 7 8 9
[:alnum:]
Alphanumeric
[:alpha:]
[:lower:]
EXAMPLE 1: THE CASE STATEMENT
#!/bin/bash
echo "Enter Y to see all files including hidden files"
echo "Enter N to see all non-hidden files"
echo "Enter q to quit"
read -p "Enter your choice: " reply
case $reply in
Y|YES) echo "Displaying all (really…) files"
ls -a ;;
N|NO) echo "Display all non-hidden files..."
ls ;;
Q) exit 0 ;;
*) echo "Invalid choice!"; exit 1 ;;
esac
*
EXAMPLE 2: THE CASE STATEMENT
#!/bin/bash
ChildRate=3
AdultRate=10
SeniorRate=7
read -p "Enter your age: " age
case $age in
[1-9]|[1][0-2]) # child, if age 12 and younger
echo "your rate is" '$'"$ChildRate.00" ;;
# adult, if age is between 13 and 59 inclusive
[1][3-9]|[2-5][0-9])
echo "your rate is" '$'"$AdultRate.00" ;;
[6-9][0-9]) # senior, if age is 60+
echo "your rate is" '$'"$SeniorRate.00" ;;
esac
*
BASH PROGRAMMING: SO FARData
structureVariablesNumeric variablesArraysUser inputControl
structuresif-then-elsecase
*
BASH PROGRAMMING: STILL TO COME
Control structuresRepetitiondo-while, repeat-
untilforselectFunctionsTrapping signals
*
The Bash Shell
The Bash Shell
Copyright Department of Computer Science, Northern Illinois
University, 2005
09-*
Copyright Department of Computer Science, Northern Illinois
University, 2005
THE WHILE LOOPPurpose:
To execute commands in “command-list” as long as
“expression” evaluates to true
Syntax:
while [ expression ]
do
command-list
done
*
EXAMPLE: USING THE WHILE LOOP
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]
do
echo The counter is $COUNTER
let COUNTER=$COUNTER+1
done
*
EXAMPLE: USING THE WHILE LOOP
#!/bin/bash
Cont="Y"
while [ $Cont = "Y" ]; do
ps -A
read -p "want to continue? (Y/N)" reply
Cont=`echo $reply | tr [:lower:] [:upper:]`
done
echo "done"
*
Translate from lower case to upper case
THE UNTIL LOOPPurpose:
To execute commands in “command-list” as long as
“expression” evaluates to false
Syntax:
until [ expression ]
do
command-list
done
*
EXAMPLE: USING THE UNTIL LOOP
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]
do
echo $COUNTER
let COUNTER-=1
done
*
EXAMPLE: USING THE UNTIL LOOP
#!/bin/bash
Stop="N"
until [ $Stop = "Y" ]; do
ps -A
read -p "want to stop? (Y/N)" reply
Stop=`echo $reply | tr [:lower:] [:upper:]`
done
echo "done"
*
THE FOR LOOPPurpose:
To execute commands as many times as the number of
words in the “argument-list”
Syntax:
for variable in argument-list
do
commands
done
*
EXAMPLE 1: THE FOR LOOP
#!/bin/bash
for i in 7 9 2 3 4 5
do
echo $i
done
*
EXAMPLE 2: USING THE FOR LOOP
#!/bin/bash
# compute the average weekly temperature
for num in 1 2 3 4 5 6 7
do
read -p "Enter temp for day $num: " Temp
let TempTotal=$TempTotal+$Temp
done
let AvgTemp=$TempTotal/7
echo "Average temperature: " $AvgTemp
*
LOOPING OVER ARGUMENTSsimplest form will iterate over
all command line arguments:
#! /bin/bash
for parm
do
echo $parm
done
*
SELECT COMMANDConstructs simple menu from word
listAllows user to enter a number instead of a wordUser enters
sequence number corresponding to the word
Syntax:
select WORD in LIST
do
RESPECTIVE-COMMANDS
done
Loops until end of input, i.e. ^d (or ^c)
*
SELECT EXAMPLE
#! /bin/bash
select var in alpha beta gamma
do
echo $var
done
Prints:
*
1) alpha
2) beta
3) gamma
#? 2
beta
#? 4
#? 1
alpha
SELECT DETAILPS3 is select sub-prompt$REPLY is user input
(the number)
#! /bin/bash
PS3="select entry or ^D: "
select var in alpha beta
do
echo "$REPLY = $var"
done
*
Output:
select entry or ^D:
1) alpha
2) beta
? 2
2 = beta
? 1
1 = alpha
SELECT EXAMPLE
#!/bin/bash
echo "script to make files private"
echo "Select file to protect:"
select FILENAME in *
do
echo "You picked $FILENAME ($REPLY)"
chmod go-rwx "$FILENAME"
echo "it is now private"
done
*
BREAK AND CONTINUEInterrupt for, while or until loopThe
break statement transfer control to the statement AFTER the
done statementterminate execution of the loopThe continue
statementtransfer control to the statement TO the done
statementskip the test statements for the current
iterationcontinues execution of the loop
*
The Bash Shell
*
Copyright Department of Computer Science, Northern Illinois
University, 2005
THE BREAK COMMAND
while [ condition ]
do
cmd-1
break
cmd-n
done
echo "done"
*
This iteration is over and there are no more iterations
The Bash Shell
The Bash Shell
Copyright Department of Computer Science, Northern Illinois
University, 2005
09-*
Copyright Department of Computer Science, Northern Illinois
University, 2005
THE CONTINUE COMMAND
while [ condition ]
do
cmd-1
continue
cmd-n
done
echo "done"
*
This iteration is over; do the next iteration
The Bash Shell
The Bash Shell
Copyright Department of Computer Science, Northern Illinois
University, 2005
09-*
Copyright Department of Computer Science, Northern Illinois
University, 2005
EXAMPLE:
for index in 1 2 3 4 5 6 7 8 9 10
do
if [ $index –le 3 ]; then
echo "continue"
continue
fi
echo $index
if [ $index –ge 8 ]; then
echo "break"
break
fi
done
*
The Bash Shell
*
Copyright Department of Computer Science, Northern Illinois
University, 2005
DONE !
BASH SHELL PROGRAMMINGSequenceDecision:if-then-
elsecaseRepetitiondo-while, repeat-untilforselectFunctionsTraps
*
still to come
SHELL FUNCTIONSA shell function is similar to a shell
scriptstores a series of commands for execution latershell stores
functions in memoryshell executes a shell function in the same
shell that called itWhere to defineIn .profileIn your scriptOr on
the command lineRemove a functionUse unset built-in
*
The Bash Shell
*
Copyright Department of Computer Science, Northern Illinois
University, 2005
SHELL FUNCTIONSmust be defined before they can be
referencedusually placed at the beginning of the script
Syntax:
function-name () {
statements
}
*
The Bash Shell
Copyright Department of Computer Science, Northern Illinois
University, 2005
EXAMPLE: FUNCTION
#!/bin/bash
funky () {
# This is a simple function
echo "This is a funky function."
echo "Now exiting funky function."
}
# declaration must precede call:
funky
*
The Bash Shell
Copyright Department of Computer Science, Northern Illinois
University, 2005
EXAMPLE: FUNCTION
#!/bin/bash
fun () { # A somewhat more complex function.
JUST_A_SECOND=1
let i=0
REPEATS=30
echo "And now the fun really begins."
while [ $i -lt $REPEATS ]
do
echo "-------FUNCTIONS are fun-------->"
sleep $JUST_A_SECOND
let i+=1
done
}
fun
*
FUNCTION PARAMETERSNeed not be declaredArguments
provided via function call are accessible inside function as $1,
$2, $3, …
$# reflects number of parameters
$0 still contains name of script
(not name of function)
*
EXAMPLE: FUNCTION WITH PARAMETER
#! /bin/sh
testfile() {
if [ $# -gt 0 ]; then
if [[ -f $1 && -r $1 ]]; then
echo $1 is a readable file
else
echo $1 is not a readable file
fi
fi
}
testfile *
*
EXAMPLE: FUNCTION WITH PARAMETERS
#! /bin/bash
checkfile() {
for file
do
if [ -f "$file" ]; then
echo "$file is a file"
else
if [ -d "$file" ]; then
echo "$file is a directory"
fi
fi
done
}
Checkfile $1 $2 $3
*
LOCAL VARIABLES IN FUNCTIONSVariables defined within
functions are global,
i.e. their values are known throughout the entire shell
program
keyword “local” inside a function definition makes referenced
variables “local” to that function
*
EXAMPLE: FUNCTION
#! /bin/bash
global="pretty good variable"
foo () {
xyz=124
echo “xyz inside function = $xyz”
local rrr="not so good variable"
echo $global
echo $rrr
global="better variable"
}
echo “xyz outside function = $xyz”
echo $global
foo
echo $global
echo $rrr
*
RECURSION;
FUNCTIONS CAN BE RECURSIVE - HERE'S A SIMPLE
EXAMPLE OF A FACTORIAL FUNCTION:
*
HANDLING SIGNALSUnix allows you to send a signal to any
process
-1 = hangup kill -HUP 1234 -2 = interrupt with ^C
kill -2 1235no argument = terminate kill 1235-9
= kill kill -9 1236-9 cannot be blocked
list your processes with
ps -u userid
*
LIST OF THE COMMONLY USED SIGNAL NUMBERS,
DESCRIPTION AND WHETHER THEY CAN BE TRAPPED
OR NOT:
*
SIGNALS ON LINUX
% kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE
9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16)
SIGSTKFLT
17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20)
SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24)
SIGXCPU
25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28)
SIGWINCH
29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN
35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38)
SIGRTMIN+4
39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42)
SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46)
SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50)
SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54)
SIGRTMAX-10
55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58)
SIGRTMAX-6
59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62)
SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
^C is 2 - SIGINT
*
HANDLING SIGNALSDefault action for most signals is to end
processterm: signal handler
Bash allows to install custom signal handler
Syntax:
trap 'handler commands' signals
Example:
trap 'echo do not hangup' 1 2
*
EXAMPLE: TRAP HANGUP
#! /bin/bash
trap 'echo dont terminate me’ 2
while true
do
echo "try to terminate"
sleep 1
done
*
EXAMPLE: TRAP MULTIPLE SIGNALS
#! /bin/sh
trap 'echo 2’ 2
trap 'echo 3’ 3
while true; do
echo -n .
sleep 1
done
*
EXAMPLE: REMOVING TEMP FILES
#! /bin/bash
cleanup () {
/bin/rm -f /tmp/tempfile.$$.?
}
trap 'cleanup; exit' 2
for i in 1 2 3 4 5 6 7 8
do
echo "$i.iteration"
touch /tmp/tempfile.$$.$i
sleep 1
done
cleanup
*
RESTORING DEFAULT HANDLERStrap without a command
list will remove a signal handlerUse this to run a signal handler
once only
#! /bin/sh
trap 'justonce' 2
justonce() {
echo "not yet"
trap 2 # now reset it
}
while true; do
echo -n "."
sleep 1
done
*
DONE !
SUMMARY: BASH SHELL
PROGRAMMINGSequenceDecision:if-then-
elsecaseRepetitiondo-while, repeat-untilforselectFunctionsTraps
*
The vi Editor
The vi EditorWhen you write some C or Java programs or shell
(or perl) scripts, or edit some system files, you need to use an
editor.There are two versatile editors in UNIX – vi and emacs.vi
is a full-screen editor. It was created by a graduate student –
Bill Joy – later to become the cofounder of Sun Microsystems.
The vi/vim EditorA vi session begins by invoking the command
vi with (or without) a filename:
vi myfileYou are presented a full empty screen, each line
beginning with a ~ (tilde).
The vi/vim EditorThe ~ is vi’s way to indicating that they are
nonexistent line.For text editing, vi uses 24 of the 25 lines that
are normally available in a terminal. The last line is reserved
for some commands that you’ll enter to act on the text. This line
is also used by the system to display messages.
The Three ModesThere are three modes in which vi
works:Command Mode – Where keys are used as commands to
act on text.Input Mode – Where any key depressed is entered as
text.Last Line Mode - Where commands can be entered in the
last line of the screen to act on text.
Quitting vi – The Last Line ModeThe editor works with a copy
of the file which is placed in a buffer that is simply a temporary
storage area which is associated with the file on disk.It is
necessary to know how to leave the editor. Saving and quitting
are handled by the Last Line Mode.Every command in this mode
is preceded by a : (colon) and followed by the [Enter] key.
Quitting vi – The Last Line ModeRemember to leave the Input
Mode by pressing the [Esc] key.The Last Line Mode offers two
ways of saving and quitting - :x and :wq.The commands return
you to the shell after saving your work.
Quitting vi – The Last Line ModeTo abort editing, you can use
q (quit) command.The q command takes you out of the editor
only if you don’t have a changed buffer.
: q [Enter]
$The q! command always return you to the prompt irrespective
of the status of the buffer.
Quitting viThese are exit commands in vi::x – saves files and
quits editing mode:wq – saves files and quits editing mode.:q –
quits editing mode when no changes are made to file:q! – quits
editing mode but after abandoning changes.:sh – escapes to
UNIX shell (use exit to return to vi).
Inserting and Replacing TextBefore you are able to enter text,
you have to change from the default Command Mode to Input
Mode.You can set the editor in showmode (with :set showmode)
to display a suitable message in the last line.The simplest type
of input is insertion of text.
Inserting and Replacing TextWhen vi is invoked, the cursor is
always positioned at the first character of the first line.To insert
text at this position, press i. The character doesn’t show up on
the screen, but pressing this key changes the mode from
Command to Input.Since the showmode setting was made,
you’ll see the words INSERT or INSERT MODE in the last line.
Inserting and Replacing TextFurther key depressions will show
text on the screen as it is being entered.
This is the vi editor
It is quite powerful.
It operates in three modes.
Inserting and Replacing TextYou can use [Backspace] to correct
a mistake or erase the previous word using [Ctrl-w].After you
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide
Shell Programming Guide

More Related Content

Similar to Shell Programming Guide

Shell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdfShell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdfHIMANKMISHRA2
 
Bash shell
Bash shellBash shell
Bash shellxylas121
 
Linux shell script-1
Linux shell script-1Linux shell script-1
Linux shell script-1兎 伊藤
 
34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.pptKiranMantri
 
RHCSA EX200 - Summary
RHCSA EX200 - SummaryRHCSA EX200 - Summary
RHCSA EX200 - SummaryNugroho Gito
 
BASH Shell Scripting – Part II
BASH Shell Scripting – Part IIBASH Shell Scripting – Part II
BASH Shell Scripting – Part IIZeeshan Iqbal
 
Licão 09 variables and arrays v2
Licão 09 variables and arrays v2Licão 09 variables and arrays v2
Licão 09 variables and arrays v2Acácio Oliveira
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaaShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaaewout2
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide SummaryOhgyun Ahn
 
perl course-in-mumbai
 perl course-in-mumbai perl course-in-mumbai
perl course-in-mumbaivibrantuser
 

Similar to Shell Programming Guide (20)

Slides
SlidesSlides
Slides
 
Shell programming
Shell programmingShell programming
Shell programming
 
Spsl by sasidhar 3 unit
Spsl by sasidhar  3 unitSpsl by sasidhar  3 unit
Spsl by sasidhar 3 unit
 
KT on Bash Script.pptx
KT on Bash Script.pptxKT on Bash Script.pptx
KT on Bash Script.pptx
 
Shell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdfShell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdf
 
Bash shell
Bash shellBash shell
Bash shell
 
Linux shell script-1
Linux shell script-1Linux shell script-1
Linux shell script-1
 
34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.ppt
 
Perl Basics with Examples
Perl Basics with ExamplesPerl Basics with Examples
Perl Basics with Examples
 
RHCSA EX200 - Summary
RHCSA EX200 - SummaryRHCSA EX200 - Summary
RHCSA EX200 - Summary
 
BASH Shell Scripting – Part II
BASH Shell Scripting – Part IIBASH Shell Scripting – Part II
BASH Shell Scripting – Part II
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Licão 09 variables and arrays v2
Licão 09 variables and arrays v2Licão 09 variables and arrays v2
Licão 09 variables and arrays v2
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
UNIX - Class1 - Basic Shell
UNIX - Class1 - Basic ShellUNIX - Class1 - Basic Shell
UNIX - Class1 - Basic Shell
 
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaaShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
SHELL PROGRAMMING
SHELL PROGRAMMINGSHELL PROGRAMMING
SHELL PROGRAMMING
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide Summary
 
lec4.docx
lec4.docxlec4.docx
lec4.docx
 
perl course-in-mumbai
 perl course-in-mumbai perl course-in-mumbai
perl course-in-mumbai
 

More from adkinspaige22

. Review the three articles about Inflation that are found below thi.docx
. Review the three articles about Inflation that are found below thi.docx. Review the three articles about Inflation that are found below thi.docx
. Review the three articles about Inflation that are found below thi.docxadkinspaige22
 
.         Find an invertebrate that is endemic to Florida. Endem.docx
.         Find an invertebrate that is endemic to Florida. Endem.docx.         Find an invertebrate that is endemic to Florida. Endem.docx
.         Find an invertebrate that is endemic to Florida. Endem.docxadkinspaige22
 
. Read the Harvard Business Review article  Link3. View this ve.docx
. Read the Harvard Business Review article  Link3. View this ve.docx. Read the Harvard Business Review article  Link3. View this ve.docx
. Read the Harvard Business Review article  Link3. View this ve.docxadkinspaige22
 
. Go to a news site and look at the headlines of several articles. A.docx
. Go to a news site and look at the headlines of several articles. A.docx. Go to a news site and look at the headlines of several articles. A.docx
. Go to a news site and look at the headlines of several articles. A.docxadkinspaige22
 
-Describe the Plessy v. Ferguson Supreme Court Case of 1896; how was.docx
-Describe the Plessy v. Ferguson Supreme Court Case of 1896; how was.docx-Describe the Plessy v. Ferguson Supreme Court Case of 1896; how was.docx
-Describe the Plessy v. Ferguson Supreme Court Case of 1896; how was.docxadkinspaige22
 
-Do the schedule with Activity on Node and also draw the schedule.docx
-Do the schedule with Activity on Node and also draw the schedule.docx-Do the schedule with Activity on Node and also draw the schedule.docx
-Do the schedule with Activity on Node and also draw the schedule.docxadkinspaige22
 
.  Record your initial reaction to the work (suggested length of 1.docx
.  Record your initial reaction to the work (suggested length of 1.docx.  Record your initial reaction to the work (suggested length of 1.docx
.  Record your initial reaction to the work (suggested length of 1.docxadkinspaige22
 
-Describe the existing needs for cost information in healthcare firm.docx
-Describe the existing needs for cost information in healthcare firm.docx-Describe the existing needs for cost information in healthcare firm.docx
-Describe the existing needs for cost information in healthcare firm.docxadkinspaige22
 
--------250 words---------Chapter 18 – According to literatu.docx
--------250 words---------Chapter 18 – According to literatu.docx--------250 words---------Chapter 18 – According to literatu.docx
--------250 words---------Chapter 18 – According to literatu.docxadkinspaige22
 
-Please name the functions of the skeletal system.2-Where are lo.docx
-Please name the functions of the skeletal system.2-Where are lo.docx-Please name the functions of the skeletal system.2-Where are lo.docx
-Please name the functions of the skeletal system.2-Where are lo.docxadkinspaige22
 
-TOPIC= Civil Right Movement and Black Power Movement#St.docx
-TOPIC= Civil Right Movement and Black Power Movement#St.docx-TOPIC= Civil Right Movement and Black Power Movement#St.docx
-TOPIC= Civil Right Movement and Black Power Movement#St.docxadkinspaige22
 
- Wordcount 500 to 1000 words- Structure Cover, Table of Conte.docx
- Wordcount 500 to 1000 words- Structure Cover, Table of Conte.docx- Wordcount 500 to 1000 words- Structure Cover, Table of Conte.docx
- Wordcount 500 to 1000 words- Structure Cover, Table of Conte.docxadkinspaige22
 
-What benefits can a diverse workforce provide to an organization.docx
-What benefits can a diverse workforce provide to an organization.docx-What benefits can a diverse workforce provide to an organization.docx
-What benefits can a diverse workforce provide to an organization.docxadkinspaige22
 
-How would you define or describe the American Great Migration m.docx
-How would you define or describe the American Great Migration m.docx-How would you define or describe the American Great Migration m.docx
-How would you define or describe the American Great Migration m.docxadkinspaige22
 
- We learned from our readings that the use of mobile devices in our.docx
- We learned from our readings that the use of mobile devices in our.docx- We learned from our readings that the use of mobile devices in our.docx
- We learned from our readings that the use of mobile devices in our.docxadkinspaige22
 
- Goals (short and long term) and how you developed them; experience.docx
- Goals (short and long term) and how you developed them; experience.docx- Goals (short and long term) and how you developed them; experience.docx
- Goals (short and long term) and how you developed them; experience.docxadkinspaige22
 
- Pick ONE Theme for the 5 short stories (ex setting, character.docx
- Pick ONE Theme for the 5 short stories (ex setting, character.docx- Pick ONE Theme for the 5 short stories (ex setting, character.docx
- Pick ONE Theme for the 5 short stories (ex setting, character.docxadkinspaige22
 
- Briefly summarize the Modernization Theory (discuss all four stage.docx
- Briefly summarize the Modernization Theory (discuss all four stage.docx- Briefly summarize the Modernization Theory (discuss all four stage.docx
- Briefly summarize the Modernization Theory (discuss all four stage.docxadkinspaige22
 
+16159390825Whats app the test online on time .docx
+16159390825Whats app the test online on time .docx+16159390825Whats app the test online on time .docx
+16159390825Whats app the test online on time .docxadkinspaige22
 
(philosophy1. why is mills philosophy closely identified with.docx
(philosophy1. why is mills philosophy closely identified with.docx(philosophy1. why is mills philosophy closely identified with.docx
(philosophy1. why is mills philosophy closely identified with.docxadkinspaige22
 

More from adkinspaige22 (20)

. Review the three articles about Inflation that are found below thi.docx
. Review the three articles about Inflation that are found below thi.docx. Review the three articles about Inflation that are found below thi.docx
. Review the three articles about Inflation that are found below thi.docx
 
.         Find an invertebrate that is endemic to Florida. Endem.docx
.         Find an invertebrate that is endemic to Florida. Endem.docx.         Find an invertebrate that is endemic to Florida. Endem.docx
.         Find an invertebrate that is endemic to Florida. Endem.docx
 
. Read the Harvard Business Review article  Link3. View this ve.docx
. Read the Harvard Business Review article  Link3. View this ve.docx. Read the Harvard Business Review article  Link3. View this ve.docx
. Read the Harvard Business Review article  Link3. View this ve.docx
 
. Go to a news site and look at the headlines of several articles. A.docx
. Go to a news site and look at the headlines of several articles. A.docx. Go to a news site and look at the headlines of several articles. A.docx
. Go to a news site and look at the headlines of several articles. A.docx
 
-Describe the Plessy v. Ferguson Supreme Court Case of 1896; how was.docx
-Describe the Plessy v. Ferguson Supreme Court Case of 1896; how was.docx-Describe the Plessy v. Ferguson Supreme Court Case of 1896; how was.docx
-Describe the Plessy v. Ferguson Supreme Court Case of 1896; how was.docx
 
-Do the schedule with Activity on Node and also draw the schedule.docx
-Do the schedule with Activity on Node and also draw the schedule.docx-Do the schedule with Activity on Node and also draw the schedule.docx
-Do the schedule with Activity on Node and also draw the schedule.docx
 
.  Record your initial reaction to the work (suggested length of 1.docx
.  Record your initial reaction to the work (suggested length of 1.docx.  Record your initial reaction to the work (suggested length of 1.docx
.  Record your initial reaction to the work (suggested length of 1.docx
 
-Describe the existing needs for cost information in healthcare firm.docx
-Describe the existing needs for cost information in healthcare firm.docx-Describe the existing needs for cost information in healthcare firm.docx
-Describe the existing needs for cost information in healthcare firm.docx
 
--------250 words---------Chapter 18 – According to literatu.docx
--------250 words---------Chapter 18 – According to literatu.docx--------250 words---------Chapter 18 – According to literatu.docx
--------250 words---------Chapter 18 – According to literatu.docx
 
-Please name the functions of the skeletal system.2-Where are lo.docx
-Please name the functions of the skeletal system.2-Where are lo.docx-Please name the functions of the skeletal system.2-Where are lo.docx
-Please name the functions of the skeletal system.2-Where are lo.docx
 
-TOPIC= Civil Right Movement and Black Power Movement#St.docx
-TOPIC= Civil Right Movement and Black Power Movement#St.docx-TOPIC= Civil Right Movement and Black Power Movement#St.docx
-TOPIC= Civil Right Movement and Black Power Movement#St.docx
 
- Wordcount 500 to 1000 words- Structure Cover, Table of Conte.docx
- Wordcount 500 to 1000 words- Structure Cover, Table of Conte.docx- Wordcount 500 to 1000 words- Structure Cover, Table of Conte.docx
- Wordcount 500 to 1000 words- Structure Cover, Table of Conte.docx
 
-What benefits can a diverse workforce provide to an organization.docx
-What benefits can a diverse workforce provide to an organization.docx-What benefits can a diverse workforce provide to an organization.docx
-What benefits can a diverse workforce provide to an organization.docx
 
-How would you define or describe the American Great Migration m.docx
-How would you define or describe the American Great Migration m.docx-How would you define or describe the American Great Migration m.docx
-How would you define or describe the American Great Migration m.docx
 
- We learned from our readings that the use of mobile devices in our.docx
- We learned from our readings that the use of mobile devices in our.docx- We learned from our readings that the use of mobile devices in our.docx
- We learned from our readings that the use of mobile devices in our.docx
 
- Goals (short and long term) and how you developed them; experience.docx
- Goals (short and long term) and how you developed them; experience.docx- Goals (short and long term) and how you developed them; experience.docx
- Goals (short and long term) and how you developed them; experience.docx
 
- Pick ONE Theme for the 5 short stories (ex setting, character.docx
- Pick ONE Theme for the 5 short stories (ex setting, character.docx- Pick ONE Theme for the 5 short stories (ex setting, character.docx
- Pick ONE Theme for the 5 short stories (ex setting, character.docx
 
- Briefly summarize the Modernization Theory (discuss all four stage.docx
- Briefly summarize the Modernization Theory (discuss all four stage.docx- Briefly summarize the Modernization Theory (discuss all four stage.docx
- Briefly summarize the Modernization Theory (discuss all four stage.docx
 
+16159390825Whats app the test online on time .docx
+16159390825Whats app the test online on time .docx+16159390825Whats app the test online on time .docx
+16159390825Whats app the test online on time .docx
 
(philosophy1. why is mills philosophy closely identified with.docx
(philosophy1. why is mills philosophy closely identified with.docx(philosophy1. why is mills philosophy closely identified with.docx
(philosophy1. why is mills philosophy closely identified with.docx
 

Recently uploaded

Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 

Recently uploaded (20)

Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 

Shell Programming Guide

  • 1. * of 70 UNIX Unbounded 5th Edition Amir Afzal Chapter 12 Shell Programming Copyright ©2008 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved. * of 70 Chapter 12 Exploring the Shell This chapter explains capabilities of the shell as an interpretive high-level languageIt describes shell programming constructs and particularsVariablesFlow controlRunningDebugging
  • 2. * * of 70 UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTIONA command language – interpreted (not compiled)Shell program files are called shell procedures, shell scripts, or simply scriptsA file that contains a series of commands for the shell to execute * of 70 Writing a Simple Script * of 70 Executing a Script Making Files Executable: The chmod Command * of 70 Executing a Script Making Files Executable: The chmod Command * of 70 Executing a Script Making Files Executable: The chmod Command
  • 3. * of 70 Executing a Script Making Files Executable: The chmod Command chmod u=rwx,g=rx,o=r myfile chmod 754 myfile # 754 = 1 1 1 1 0 1 1 0 0 r w x r - x r - - * of 70 WRITING MORE SHELL SCRIPTSBuilt in commands * of 70 WRITING MORE SHELL SCRIPTS Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 by Pearson Education, Inc Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 by Pearson Education, Inc * of 70 WRITING MORE SHELL SCRIPTS Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 by Pearson Education, Inc Amir Afzal UNIX Unbounded, 5th Edition
  • 4. Copyright ©2008 by Pearson Education, Inc * of 70 WRITING MORE SHELL SCRIPTSUsing Special Characters When using the echo command on the prompt sign: make sure to use –e option for the escape characters to work as specified $echo –e “HnEnLnLnO” When using the echo command inside script files, and you are using the bash command or chmod +x command to run this cript make sure to use –e option for the escape characters to work as specified echo –e “HnEnLnLnO” * of 70 WRITING MORE SHELL SCRIPTSUsing Special Characters When using the echo command on the prompt sign: make sure to use –e option for the escape characters to work as specified $echo –e “HnEnLnLnO” When using the echo command inside script files, and you are using the bash command or chmod +x command to run this cript make sure to use –e option for the escape characters to work as specified echo –e “HnEnLnLnO” * of 70 WRITING MORE SHELL SCRIPTSUsing Special Characters When using the echo command on the prompt sign: make sure to use –e option for the escape characters to work as
  • 5. specified $echo –e “HnEnLnLnO” * of 70 WRITING MORE SHELL SCRIPTSLogging Off in Style * of 70 WRITING MORE SHELL SCRIPTSExecuting Commands: The dot CommandLets you execute a program in the current shell and prevents the shell from creating the child process * of 70 WRITING MORE SHELL SCRIPTSPath Modification Command SubstitutionPlace the output of a command in the argument string * of 70 WRITING MORE SHELL SCRIPTSReading Inputs: The read Command * of 70 Reading Inputs: The read Command Amir Afzal
  • 6. UNIX Unbounded, 5th Edition Copyright ©2008 by Pearson Education, Inc Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 by Pearson Education, Inc * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSCommentsThe shell recognizes # as the comment symbol; therefore, characters after the # are ignored. VariablesWrite the name of the variable, followed by the equal sign and the value you want to store in the variableNo spaces either side of the = sign * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSDisplaying Variablesthe echo command can be used to display the contents of variables Command SubstitutionYou can store the output of a command in a variable * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSCommand Line Parameters
  • 7. * of 70 Command Line Parameters Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 by Pearson Education, Inc Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 by Pearson Education, Inc * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSAssigning ValuesPossible to assign values to the positional variables is to use the set command Accents Not single quotes * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSScenario – write a script file that does the followingStores the specified file in a directory called keep in your HOME directoryInvoke the vi editor to edit the specified file * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSResult * of 70
  • 8. EXPLORING THE SHELL: PROGRAMMING BASICSTerminating Programs: The exit CommandUse to immediately terminate execution of your shell program Conditions and TestsThe if-then construct * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSThe if- then-else Construct * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSThe if- then-else Construct no # Notice the spaces before and after * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSThe if- then-elif Construct * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSThe if- then-elif Construct hour= `date +%H’ Replace these two lines (8 and 9)
  • 9. * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSThe if- then-elif Construct if [ “$answer” = Y ] * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSTesting Different CategoriesNumeric Valuesuse the test command to test (compare) two integer numbers algebraically * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSScenario – write a script that:Accepts three numbers as input (command line arguments) and … Displays the largest of the threeOutput should look like this: * of 70 EXPLORING THE SHELL: PROGRAMMING BASICS if [ “$num1” –gt “$num2” -a “$num1” –gt “$num3” ] * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSString ValuesCompare (test) strings with the test command * of 70
  • 10. EXPLORING THE SHELL: PROGRAMMING BASICSString ValuesCompare (test) strings with the test command * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSFiles – use the test command to test file characteristicsSize, type, permissions * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSFiles – use the test command to test file characteristicsSize, type, permissions if [ -r “$FILE” ] * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSFiles – use the test command to test file characteristics * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSParameter SubstitutionLets you test the value of a parameter and change its value according to a specified option If variable has value return this value Else return the string If variable has value
  • 11. return the string Else nothing If variable has value return this value Else variable = string If variable has value return this value Else print string * of 70 echo ${var:-"Variable is not set"} echo "1 - Value of var is ${var}" echo ${var:="Variable is not set"} echo "2 - Value of var is ${var}" unset var echo ${var:+"This is default value"} echo "3 - Value of var is $var“ var="Prefix" echo ${var:+"This is default value"}
  • 12. echo "4 - Value of var is $var" echo ${var:?"Print this message"} echo "5 - Value of var is ${var}" Variable is not set 1 - Value of var is Variable is not set 2 - Value of var is Variable is not set 3 - Value of var is This is default value 4 - Value of var is Prefix Prefix 5 - Value of var is Prefix * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSParameter Substitution Notice the exit from the script If name is still empty * of 70 EXPLORING THE SHELL: PROGRAMMING BASICSParameter Substitution * of 70
  • 13. ARITHMETIC OPERATIONSThe expr Command – Arithmetic OperatorsNote: spaces between the elements of an expression are necessary, integer only * of 70 ARITHMETIC OPERATIONSThe expr Command – Arithmetic Operators The * (multiplication) and % (remainder) characters have special meaning to the shell, they must be preceded by a backslash [] * of 70 ARITHMETIC OPERATIONSThe expr Command – Arithmetic Operators The grave accent marks [` and `] surrounding the command are necessary and cause the output of the command (expr) to be substituted. $ x=10 $ y=20 $ z=`expr $x + $y` * of 70 $ c=`expr $a + $b` $ echo “the value of addition=$c” $ d=`expr $a - $b` $ echo “the value of subtraction=$d” $ e=`expr $a * $b`
  • 14. $ echo “the value of multiplication=$e” $ f=`expr $a / $b` $ echo “the value of division=$f” $ g=`expr $a % $b` $ echo “the value of modulus=$g” Show the output of each code segment? * of 70 ARITHMETIC OPERATIONSRelational OperatorsRelational operators that work on both numeric and nonnumeric arguments.If both arguments are numeric, the comparison is numericIf one or both arguments are nonnumeric, the comparison is nonnumeric and uses the ASCII values.because > (greater than) and < (less than) characters have special meaning to the shell, they must be preceded by a backslash [] n1=10 n2=20 if [ “$n1” > “$n2” ] then echo “$n1 is greater than $n2” else echo ”$n1 is not greater than $n2” fi * of 70 ARITHMETIC OPERATIONSArithmetic Operations: The let CommandSimpler alternative to the expr command and includes all the basic arithmetic operations
  • 15. * of 70 THE LOOP CONSTRUCTSThe For Loop: The for-in-done ConstructUsed to execute a set of commands a specified number of times * of 70 THE LOOP CONSTRUCTSThe For Loop: The for-in-done Construct * of 70 for var in "[email protected]" do echo "$var" done for i in {1..5} do echo "Welcome $i times" done for num in `seq 1 2 10` do echo "$num" done * of 70 for (( c=1; c<=5; c++ ))
  • 16. do echo "Welcome $c times" done exit 0 Spaces are not important here * of 70 THE LOOP CONSTRUCTSThe For Loop: The for-in-done Construct * of 70 THE LOOP CONSTRUCTSThe While Loop: The while-do-done ConstructThe while loop continues as long as the loop condition is true. * of 70 THE LOOP CONSTRUCTSThe While Loop: The while-do-done Construct * of 70 THE LOOP CONSTRUCTSThe While Loop: The while-do-done ConstructCommand line version of the carryon program * of 70 THE LOOP CONSTRUCTSThe While Loop: The while-do-done Construct
  • 17. * of 70 THE LOOP CONSTRUCTSThe While Loop: The while-do-done Construct * of 70 THE LOOP CONSTRUCTSThe While Loop: The while-do-done Construct * of 70 THE LOOP CONSTRUCTSThe Until Loop: The until-do-done ConstructSimilar to the while loop, except … It continues executing the body of the loop as long as the condition of the loop is falseBody of the until loop might never get executed if the loop condition is true * of 70 THE LOOP CONSTRUCTSScenario: check whether a specified user is on the system or, if not, to be informed as soon as the user logs in * of 70 DEBUGGING SHELL PROGRAMSThe Shell CommandUse the sh, ksh, or bash command with one of its options to make the debugging of your script files easierOptions
  • 18. * of 70 DEBUGGING SHELL PROGRAMSThe Shell Command * of 70 DEBUGGING SHELL PROGRAMSThe Shell Command * of 70 DEBUGGING SHELL PROGRAMSThe Shell CommandRun the BOX program with the -v option, and specify some command line arguments. * of 70 DEBUGGING SHELL PROGRAMSThe Shell Command Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 UNIX Unbounded 5th Edition Amir Afzal
  • 19. Chapter 5 Introduction to the UNIX File System Copyright ©2008 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 5.1 DISK ORGANIZATION UNIX allows you to divide your hard disk into many units (called directories), and subunits (called subdirectories), thereby nesting directories within directories. UNIX provides commands to create, organize, and keep track of directories and files on the disk. 5.2 FILE TYPES UNDER UNIX UNIX has three categories of files:
  • 20. Regular Files Regular files contain sequences of bytes that could be programming code, data, text, and so on. Directory Files The directory file is a file that contains information (like the file name) about other files. It consists of a number of such records in a special format defined by your operating system. Special Files Special files (device files) contain specific information corresponding to peripheral devices such as printers, disks, and so on. * Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 5.3 ALL ABOUT DIRECTORIES Directories are an essential feature of the UNIX file system The directory system provides the structure for organizing files on a disk In UNIX, the directory structure is organized in levels and is
  • 21. known as a hierarchical structure The highest level directory is called the root and all other directories branch directly or indirectly from it Figure 5.1 shows the root and some other directories * Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Figure 5-1 Directory Structure Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Figure 5-2 Parent and Child Relationship The terms parent and child describe the relationship between levels of the hierarchy. Figure 5.2 shows this relationship. Only the root directory has
  • 22. no parents. It is the ancestor of all the other directories. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 5.3.1 Important Directories Following are summaries of some of the more important directories on your UNIX System: / This is the root directory. It is the highest-level directory and all other directories branch from it /usr This directory holds users’ home directories. In other UNIX systems including Linux, this can be the /home directory /usr/docs This directory holds various documents /usr/man This directory holds man (online manual) pages /usr/games This directory holds game programs /usr/bin This directory holds user-oriented UNIX programs
  • 23. /home/faculty/jomer /home/STUDENTS/nonmajors/maamci $ cd /usr/bin $ ls Blue: Directory Green: Executable or recognized data file Sky Blue: Linked file Yellow with black background: Device Pink: Graphic image file Red: Archive file Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 /usr/spool This directory has several subdirectories such as mail, which holds mail files, and spool, which holds files to be printed /usr/sbin This directory holds system administration files /bin This directory holds many of the basic UNIX program files /dev This directory holds device files. These are special files that represent the physical computer components such as printer or disk /sbin
  • 24. This directory holds system files that usually are run automatically by the UNIX system. /etc This directory and its subdirectories hold many of the UNIX configuration files Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 5.3.2 The Home Directory The system administrator creates all user accounts on the system and associates each user account with a particular directory This directory is the home directory The log on process places you into your home directory From your home directory you can expand your directory structure according to your needs You can add as many subdirectories as you like and dividing subdirectories into additional subdirectories [email protected]:~$ echo $HOME /home/faculty/jomer [email protected]:~$ pwd /home/faculty/jomer
  • 25. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 5.3.3 The Working Directory While you are working on the UNIX system, you are always associated with a directory. The directory you are associated with or working in is called the working directory Figure 5.3 shows that the directory called usr has three subdirectories called david, daniel, and gabriel. The directory david contains three files, but the other directories are empty. [email protected]:~$ echo $HOME /home/faculty/jomer [email protected]:~$ pwd /home/faculty/jomerFigure 5.3 is not the standard UNIX file structure Your login name and your home directory name are usually the same and are assigned by the system administrator The root directory is present in all UNIX file structures The name of the root directory is always the forward slash (/) 4 Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85
  • 26. Figure 5-3 Directories, Subdirectories, and Files Figure 5.3 shows that the directory called usr has three subdirectories called david, daniel, and gabriel. The directory david contains three files, but the other directories are empty. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 5.3.4 Understanding Paths and Pathnames Every file has a pathname. The pathname locates the file in the file system You determine a file’s pathname by tracing a path from the root directory to the file, going through all intermediate directories Figure 5.4 shows a hierarchy and the pathnames of its directories and files. For example, using Figure 5.4, if your current directory is root, then the path to a file (say, myfirst) under the david directory is /usr/david/myfirst.The forward slash (/) at the very beginning of a pathname stands for the root directory /usr/david/myfirst.The other slashes serve to separate the names of the other directories and files /usr/david/myfirst. The files in your working directory are immediately accessible. To access files in another directory you need to specify the particular file by its pathname 4
  • 27. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Figure 5-4 Pathnames in a Directory Structure Figure 5.4 shows a hierarchy and the pathnames of its directories and files. For example, if your current directory is root, then the path to a file (say, myfirst) under the david directory is /usr/david/myfirst. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Absolute Pathname An absolute pathname (full pathname) traces a path from the root to the file. An absolute pathname always begins with the name of the root directory, forward slash (/). For example, if your working directory is usr, the absolute pathname of the file called myfirst under the directory david is /usr/david/myfirst. 4 1. The absolute pathname specifies exactly where to find a file. Thus, it can be used to specify file location in the working directory or
  • 28. any other directory. 2. Absolute pathnames always start from the root directory and therefore have a forward slash (/) at the beginning of the pathname. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Relative Pathname A relative pathname is a shorter form of the pathname. It traces a path from the working directory to a file Like the absolute pathname, the relative pathname can describe a path through many directories. For example, if your working directory is usr, the relative pathname to the file called REPORT under the david directory is david/REPORT There is no initial forward slash (/) for a relative pathname. It always starts from your current directory. 4
  • 29. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 5.3.5 Using File and Directory Names Every ordinary and directory file has a filename. UNIX gives you much freedom in naming your files and directories. Most modern Linux and UNIX limit filename to 255 characters. However, some older version of UNIX system limits filenames to 14 characters only. You name a file using a combination of characters and/or numbers. Avoid using the following characters in filenames: < > …………… less than and greater than signs ( ) …………… open and close parentheses [ ] …………… open and close brackets {} …………… open and close braces asterisk or star ? …………… question mark " …………… double quotation mark ' …………… single quotation mark – …………… minus sign $ …………… dollar sign ^ …………… caret Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85
  • 30. Choose characters for filenames from the following list: (A–Z) …………… uppercase letters (a–z) …………… lowercase letters (0–9) …………… numbers (_) …………… underscore (.) …………… dot (period) Filename Extensions The filename extension helps to further categorize and describe the contents of a file Filename extensions are part of the filename following a period and in most cases are optional In Figure 5.5, first.c and first.cpp in the source directory have typical file extensions (.c and .cpp for the C and C++ programming languages, respectively) The following examples show some filenames with extensions: report.c report.o memo.04.10 The use of more than one period in a file extension is allowed in UNIX. 4 Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Figure 5-5 An Example of a Directory Structure
  • 31. 5.4 DIRECTORY COMMANDS Figure 5.5 is your directory structure, and your home directory is david. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 5.4 DIRECTORY COMMANDS In the following examples, assume that your login name is david, Figure 5.5 is your directory structure, and your home directory is david. 5.4.1 Displaying a Directory Pathname: The pwd Command The pwd (print working directory) command displays the absolute pathname of your working (current) directory. Log in and show the pathname of your home directory: login: david [Return] . . Enter your login name (david) password:. . . . . . . . . . .Enter your password. Welcome to UNIX! $ pwd [Return] . . . . . . Display your HOME directory path. /usr/david $_ . . . . . . . . . . . . . . . . .Prompt for next command. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
  • 32. Chapter 5: Introduction to the UNIX File System * of 85 4 1. /usr/david is your home directory pathname. 2. /usr/david is also your current or working directory pathname. 3. /usr/david is an absolute pathname because it begins with /, tracing the path of your home directory from the root. 4. david is your login name and your home directory name. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 5.4.2 Changing Your Working Directory: The cd Command To change your working directory to the source directory, use the following command sequence: $ pwd [Return] . . . . . . . . . . Check your current directory. /usr/david $ cd source [Return] . . . . . .Change to source directory. $ pwd [Return] . . . . . . . . . . Display your working directory. /usr/david/source
  • 33. $_ . . . . . . . . . . . . . . . . . . . . . Prompt for the next command. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Assuming you have permission, you can change your working directory to /dev by using the following command sequence: $ cd /dev [Return] . . . . Change to /dev directory. $ pwd [Return] . . . . . . Check your working directory. /dev . . . . . . . . . . . . . . . Your current directory is /dev. $_ . . . . . . . . . . . . . . . . Prompt for next command. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 5.4.3 Creating Directories The very first time you log on to the UNIX system, you begin work from your home directory, which is also your working directory. Advantages of Creating Directories
  • 34. The following lists some of the advantages of using directories: • Grouping related files in one directory makes it easier to remember and access them. • Displaying a shorter list of your files on the screen enables you to find a file more quickly. • You can use identical filenames for files that are stored in different directories. • Directories make it feasible to share a large-capacity disk with other users with a well-defined space for each user. • You can take advantage of the UNIX commands that manipulate directories. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Figure 5-6 Your Directory Structure at the Beginning Directory Structure Let’s start with the directory structure presented in Figure 5.6. Depending on your system configuration and administration
  • 35. requirements, you might have other files or subdirectories in your HOME directory. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 5.4.4 Directory Creation: The mkdir Command The mkdir (make directory) command creates a new subdirectory under your working directory or any other directory you specify as part of the command. Create a directory called memos under your HOME directory: $ cd [Return] . . . . . . . . . . . . . . . Make sure you are in your HOME directory. $ mkdir memos [Return] . . . . . . . Create a directory called memos. $ pwd [Return] . . . . . . . . . . . . . . Check your working directory. /usr/david $ cd memos [Return]. . . . . . . . . .Change to memos directory. $ pwd [Return] . . . . . . . . . . . . . . Check your working directory. /usr/david/memos . . . . . . . . . . . . Your current directory is memos.
  • 36. $_ . . . . . . . . . . . . . . . . . . . . . . . Prompt for next command. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Figure 5-7 Your Directory Structure After Adding the memos Subdirectories Before After Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 While you are in your HOME directory, create a new subdirectory called important in the memos directory. $ cd [Return] . . . . . . . . . . . . . . . Make sure you are in your HOME directory. $ mkdir memos/important [Return] . . . Specify the important directory pathname. $ cd memos/important [Return] . . . .Change to important directory. $ pwd [Return] . . . . . . . . . . . . . . . .Check your working
  • 37. directory. /usr/david/memos/important $_ . . . . . . . . . . . . . . . . . . . . . . . . . Now your working directory is important. Figure 5.8 shows your directory structure after adding memos and important subdirectories. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Figure 5-8 Your Directory Structure After Adding the memos and important Subdirectories Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Figure 5-9 Creating the source Directory Figure 5.9 shows how to create a directory called source under your HOME directory. Figure 5.10 shows your directory structure after adding the source subdirectory. A directory structure can be created according to your specific needs. 4
  • 38. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Figure 5-10 Your Directory Structure After Adding the source Directory Before After Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 The mkdir Command: -p Option The -p option creates levels of directories under your current directory. Create a directory structure three levels deep, starting in the HOME directory: $ cd [Return] . . . . . . . . . . . . . . . . . . Make sure you are in your HOME directory. $ mkdir -p xx/yy/zz [Return] . . . . . . Create a directory called xx; in xx create a directory called yy, and in yy create a
  • 39. directory called zz. $_ . . . . . . . . . . . . . . . . . . . . . . . . . . Ready for next command. Figure 5.11 depicts the directory structure after this command sequence has been applied. --parents Option The alternative option in Linux. Like the -p option, --parents creates levels of directories under your current or the specified directory. The command line for using the --parents is: $ mkdir --parents xx/yy/zz [Return] . . . . . Create a directory called xx; in xx, create a directory called yy; and in yy, create a directory called zz. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Figure 5-11 Your Directory Structure After Adding the Three- Levels Deep Subdirectories Before After Amir Afzal
  • 40. UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 5.4.5 Removing Directories: The rmdir Command The rmdir (remove directory) command removes (deletes) the specified directory. However, it removes only empty directories - directories that contain no subdirectories Remove the important directory from your memos directory: $ cd [Return] . . . . . . . . . . . . . . . . Make sure you are in your HOME directory. $ cd memos [Return] . . . . . . . . . . Change your working directory to memos. $ pwd [Return] . . . . . . . . . . . . . . Make sure you are in memos. /usr/david/memos $_ . . . . . . . . . . . . . . . . . . . . . . . . Yes, you are in memos. $ rmdir important [Return] . . . . . Remove the important directory. $_ . . . . . . . . . . . . . . . . . . . . . . . . Ready for next command. 1. You were able to remove the important subdirectory because it was an empty directory. 2. You must be in a parent directory to remove a subdirectory. 4
  • 41. Omer, Jalal Sheikh (OJS) Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 From the david directory, remove the source subdirectory: $ cd [Return] . . . . . . . . . . . . . . Change to david directory. $ rmdir source [Return] . . . . . . Remove the source directory. rmdir: source: Directory not empty $ rmdir xyz [Return] . . . . . . . . Remove a directory called xyz. rmdir: xyz: Directory does not exist $_ . . . . . . . . . . . . . . . . . . . . . . Ready for next command. 1. You could not remove the source subdirectory because it was not an empty directory. 2. rmdir returns an error message if you give a wrong directory name or if it cannot locate the directory name in the specified pathname. 3. You must be in the parent directory or a higher level of
  • 42. directory to remove subdirectories (children). 4 Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 5.4.6 Listing Directories: The ls Command The ls (list) command is used to display the contents of a specified directory. It lists the information in alphabetical order by filename The list includes both filenames and directory names When no directory is specified, the current directory is listed If a filename is specified, ls shows the filename with any other information requested. Figure 5.12 is used in the examples and command sequences as the directory structure, and subsequent figures show the effect of the example commands on the files and directories. Remember, a directory listing contains only the names of the files and subdirectories. If no directory name is specified, the default is your current directory. A filename does not indicate whether it refers to a file or a directory. By default, the output is sorted alphabetically.
  • 43. 4 Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Figure 5-12 The Directory Structure Used for Command Examples Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Assuming your current directory is david, show the contents of your HOME directory by typing ls [Return]. $ ls 123 Draft_1 REPORT memos myfirst phones source xx $_ Amir Afzal
  • 44. UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 In some systems, the output of the ls command is not vertical in one column and the default format is set to display filenames across the screen. $ ls 123 Draft_1 REPORT memos myfirst phones source xx $_ Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 You may want to list the contents of directories other than your current directory. While in your HOME directory david, list files in the source directory: $ cd [Return] . . . . . . . . . . . Make sure you are in the david directory. $ ls source [Return] . . . . . .While in david, display list of files in the source directory. first.c first.cpp
  • 45. $_ . . . . . . . . . . . . . . . . . . .Ready for next command. While in your HOME directory, check whether first.c exists in the source directory: $ ls source/first.c [Return]. . . . . . Display the first.c filename in the source directory to see whether it exists. It does exist, so the file-name is displayed. source first.c $ ls xyz [Return] . . . . . . . . . . . . . Display a file called xyz if it exists. If it does not exist, you get the error message. xyz: No such file or directory $_ . . . . . . . . . . . . . . . . . . . . . . . . . .You get the prompt sign again. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85
  • 46. Table 5.1 The ls Command Options ls Options When you need more information about your files or you want the listing in a different Format, use the ls command with options. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 4 1. Every option letter is preceded by a minus sign. 2. There must be a space between the command name and the option. 3. You can use pathnames to list files in a directory other than your working directory. 4. You can use more than one option in a single command line. Let’s use some of these options and observe their outputs on the screen. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85
  • 47. Figure 5-13 The ls Command and the -l Option Option: -l The most informative option is the -l (long format) option. The listing produced by the ls command and -l option shows one line for each file or subdirectory and displays several columns of information for each file. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Figure 5-14 The ls Command-Long Format Figure 5.14 gives you a general idea about what is in each column. Look at each column and see what type of information it conveys. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Figure 5-15 The ls Command and the -r Option Option: -r To display the names of the files in your HOME directory in reverse order, type ls -r and press [Return]
  • 48. Notice the option is the lowercase r. O Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Figure 5-16 The ls Command and the -C Option Option: -C To display the contents of your current directory in column format, type: ls -C [Return] 4 The columns are alphabetically sorted down two columns. This is the default output format. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Figure 5-17 The ls Command and the -m Option Option: -m To display the contents of your current directory separated by commas, type ls -m and press [Return]. Amir Afzal UNIX Unbounded, 5th Edition
  • 49. Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Using Multiple Options You can use more than one option in a single command line. For example: To list all files, including invisible (-a option) files, in long format (-l option), and with the filenames in reverse alphabetic order (-r option), you type: ls -alr or ls -a -l –r [Return] 1. You can use one hyphen to start options, but there should be no space between the option letters. 2. The sequence of the option letters in the command line is not important. 3. You can use one hyphen for each option, but there must be a space between option letters. 4 Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Options: -m -p List your HOME directory across the screen and indicate each directory name with a slash (/).
  • 50. 4 Two options are used: -m to produce filenames across the screen -p to place a slash (/) at the end of the directory filenames Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Options: -amF Show all filenames, separated by commas, and to indicate the directory files with a slash and executable files with an asterisk, do the following: -F in Unix In Linux: ls --classify 4 1. Tree options are used: -a to show hidden files -m to produce filenames across the screen separated by columns -F to indicate directories and executable files by placing a slash (/) or an asterisk at the end of the filenames respectively 2. The two invisible files (. and ..)are directory files, indicated by the slash at the end of the filenames
  • 51. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Options: -arC List all the files in your HOME directory, in column format, in reverse order: Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Options: -s -m List the files in david, separated by commas, and show the size of each file: 1. The first field (total 11) shows total size of files, usually in blocks of 512 bytes. 2. The option -s produces the file size; each file is at least 1 block (512 bytes), regardless of how small the file may be. 4 Amir Afzal
  • 52. UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Options: -a -x -s List all files (including hidden files) in david in column format and also show the file sizes: 4 1. The total size is 13 blocks, since the size of the two hidden files is added. 2. The -x option formats the columns in a slightly different manner than -C. Each column is alphabetically sorted across rather than down the page. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 5: Introduction to the UNIX File System * of 85 Options: -R -C Show the directory structure under david in …
  • 53. ARRAYSArray is a variable which contains multiple values may be of same type or different type since by default in shell script everything is treated as a string. An array is zero-based ie indexing start with 0. * HOW TO DECLARE ARRAY IN SHELL SCRIPTING? 1. Indirect Declaration Assign a value in a particular index of Array Variable. No need to first declare ARRAYNAME[INDEXNR]=value 2. Explicit Declaration declare -a ARRAYNAME 3. Compound Assignment ARRAYNAME=(value1 value2 .... valueN) * ARRAYS IN BASHbash has two types of arrays: one- dimensional indexed arrays and associative arrays • Any variable can be used as a 1D array – Identified as var[index] $x=24 $ echo ${x[0]}Indexed arrays • Index array need not be declared though they can be using the command declare -a • You can also declare arrays of any size by declare -a color[3] with the elements indexed from 0 to 2 – In reality, the index above is ignored
  • 54. – You can simply add more elements to it by assigning an element with a new index * ACCESSED BY AN INDEX – Index starts at 0 – Index can be any positive integer, up to 599147937791 – Arithmetic expressions are supported in index • Values are assigned by var[index]=value • Examples color[1]="red" color[2]="green" color[0]="blue" – Values need not be assigned in any specific order Another way to assign values, known as compound statement, is color=([2]=green [1]=red [0]=blue) * If you specify the elements in order, you can specify them as color=(red blue green) • The above values can be accessed by for i in 0 1 2 do echo ${color[$i]} done – You must use the curly braces to access array elements; otherwise, you’ll just get the first array element and the subscript
  • 55. * * IFS: internal field separator * * * * *
  • 56. TO COUNT LENGTH OF ARRAY * COLOR=(RED BLUE GREEN) * COLOR=(RED BLUE GREEN) * COLOR=(RED BLUE GREEN) * The contents of the colors array is not change, only the output * BASH ASSOCIATIVE ARRAY EXAMPLES * * (foo=bar baz=quux corge=grault)
  • 57. * QUOTING KEYS * $ k=plugh $ MYMAP['$K']=xyzzy $ echo ${MYMAP[plugh]} $ echo ${MYMAP['$K']} xyzzy * ASSOCIATIVEARRAYS * Use: printf instead of print ([apple red] [banana yellow] [grape purple]) LOOPING * Use double quotation “
  • 58. * * ECHO * * ${MYMAP[${KEYS[$I]}]} KEYS=(“foo a” “baz b) SCOPE * ARRAYSArray is a variable which contains multiple values may be of same type or different type since by default in shell script everything is treated as a string. An array is zero-based ie
  • 59. indexing start with 0. * HOW TO DECLARE ARRAY IN SHELL SCRIPTING? 1. Indirect Declaration Assign a value in a particular index of Array Variable. No need to first declare ARRAYNAME[INDEXNR]=value 2. Explicit Declaration declare -a ARRAYNAME 3. Compound Assignment ARRAYNAME=(value1 value2 .... valueN) * ARRAYS IN BASHbash has two types of arrays: one- dimensional indexed arrays and associative arrays • Any variable can be used as a 1D array – Identified as var[index] $x=24 $ echo ${x[0]}Indexed arrays • Index array need not be declared though they can be using the command declare -a • You can also declare arrays of any size by declare -a color[3] with the elements indexed from 0 to 2 – In reality, the index above is ignored – You can simply add more elements to it by assigning an element with a new index *
  • 60. ACCESSED BY AN INDEX – Index starts at 0 – Index can be any positive integer, up to 599147937791 – Arithmetic expressions are supported in index • Values are assigned by var[index]=value • Examples color[1]="red" color[2]="green" color[0]="blue" – Values need not be assigned in any specific order Another way to assign values, known as compound statement, is color=([2]=green [1]=red [0]=blue) * If you specify the elements in order, you can specify them as color=(red blue green) • The above values can be accessed by for i in 0 1 2 do echo ${color[$i]} done – You must use the curly braces to access array elements; otherwise, you’ll just get the first array element and the subscript *
  • 61. * IFS: internal field separator * * * * * TO COUNT LENGTH OF ARRAY
  • 62. * COLOR=(RED BLUE GREEN) * COLOR=(RED BLUE GREEN) * COLOR=(RED BLUE GREEN) * The contents of the colors array is not change, only the output * BASH ASSOCIATIVE ARRAY EXAMPLES * * (foo=bar baz=quux corge=grault)
  • 63. * QUOTING KEYS * $ k=plugh $ MYMAP['$K']=xyzzy $ echo ${MYMAP[plugh]} $ echo ${MYMAP['$K']} xyzzy * ASSOCIATIVEARRAYS * Use: printf instead of print ([apple red] [banana yellow] [grape purple]) LOOPING * Use double quotation “
  • 64. * * ECHO * * ${MYMAP[${KEYS[$I]}]} KEYS=(“foo a” “baz b) SCOPE * * of 44 *created by: Aho, Weinberger, and Kernighanscripting language used for manipulating data and generating reportsversions of awkawk, nawk, mawk, pgawk, … GNU awk: gawk
  • 65. * of 44 What can you do with awk?awk operation:scans a file line by line splits each input line into fieldscompares input line/fields to patternperforms action(s) on matched linesUseful for:transform data filesproduce formatted reportsProgramming constructs:format output linesarithmetic and string operationsconditionals and loops * * of 44 The Command: awk * * of 44 Simple awk commandawk ‘Pattern { Command }’ inputFile $ cat textfile Line number 1 Line number 2 Line number 3 Line number 4 Line number 5 $ awk ‘/4/ {print }’ textfile Line number 4 condition action
  • 66. * of 44 Basic awk Syntaxawk [options] ‘script’ file(s) $ awk ‘/4/ {print }’ textfile awk [options] –f scriptfile file(s) Options: -F to change input field separator -F: or -F, -f to name script file Since awk itself can be a complex language, you can store all the commands in a file and run it with the –f flag * The AWK/NAWK Utility The AWK/NAWK Utility Copyright Department of Computer Science, Northern Illinois University, 2004 * Copyright Department of Computer Science, Northern Illinois University, 2004 * of 44 Basic awk Programconsists of patterns & actions: pattern {action} if pattern is missing, action is applied to all linesif action is missing, the matched line is printedmust have either pattern or action
  • 67. Example: $ awk '/for/' testfile prints all lines containing string “for” in testfile $ awk ‘{print }’ testfile - print all lines in testfile * The AWK/NAWK Utility The AWK/NAWK Utility Copyright Department of Computer Science, Northern Illinois University, 2004 * Copyright Department of Computer Science, Northern Illinois University, 2004 * of 44 Basic Terminology: input fileA field is a unit of data in a lineEach field is separated from the other fields by the field separatordefault field separator is whitespaceA record is the collection of fields in a lineA data file is made up of records * Example Input File The AWK/NAWK Utility The AWK/NAWK Utility Copyright Department of Computer Science, Northern Illinois University, 2004 *
  • 68. Copyright Department of Computer Science, Northern Illinois University, 2004 * of 44 Buffersawk supports two types of buffers: record and field field buffer:one for each fields in the current record.names: $1, $2, … record buffer :$0 holds the entire current record * * of 44 Some System Variables FS Field separator (default=whitespace) RS Record separator (default=n) NF Number of fields in current record NR Number of the current record OFS Output field separator (default=space) ORS Output record separator (default=n) FILENAME Current filename * * of 44
  • 69. Example: Records and Fields $ cat emps Tom Jones 4424 5/12/66 543354 Mary Adams 5346 11/4/63 28765 Sally Chang 1654 7/22/54 650000 Billy Black 1683 9/23/44 336500 $ awk '{print NR, $0}' emps 1 Tom Jones 4424 5/12/66 543354 2 Mary Adams 5346 11/4/63 28765 3 Sally Chang 1654 7/22/54 650000 4 Billy Black 1683 9/23/44 336500 * No pattern, just action on each record (line) The AWK/NAWK Utility The AWK/NAWK Utility Copyright Department of Computer Science, Northern Illinois University, 2004 * Copyright Department of Computer Science, Northern Illinois University, 2004 * of 44 Example: Space as Field Separator $ cat emps Tom Jones 4424 5/12/66 543354 Mary Adams 5346 11/4/63 28765 Sally Chang 1654 7/22/54 650000 Billy Black 1683 9/23/44 336500
  • 70. $ awk '{print NR, $1, $2, $5}' emps 1 Tom Jones 543354 2 Mary Adams 28765 3 Sally Chang 650000 4 Billy Black 336500 * No pattern, just action on each record (line) The AWK/NAWK Utility The AWK/NAWK Utility Copyright Department of Computer Science, Northern Illinois University, 2004 * Copyright Department of Computer Science, Northern Illinois University, 2004 * of 44 Example: Colon as Field Separator $ cat em2 Tom Jones:4424:5/12/66:543354 Mary Adams:5346:11/4/63:28765 Sally Chang:1654:7/22/54:650000 Billy Black:1683:9/23/44:336500 $ awk -F: '/Jones/{print $1, $2}' em2 Tom Jones 4424 * Pattern and action on each record (line)
  • 71. The AWK/NAWK Utility The AWK/NAWK Utility Copyright Department of Computer Science, Northern Illinois University, 2004 * Copyright Department of Computer Science, Northern Illinois University, 2004 * of 44 BEGIN And END BlocksTwo special patterns that can be matchedBEGINCommands are executed before any records are looked atENDCommands are executed after all records are processed * of 44 Example $cat textfile Line number 1 Line number 2 Line number 3 Line number 4 Line number 5 $ awk '/4/ {print $0} BEGIN {print "hello"} END {print "goodbye"}' textfile Hello Line number 4
  • 72. goodbye $ Step 1 Step 2 Step 3 * of 44 $ ls | awk ' BEGIN {print "List of html files:" } /.html$/ {print} END { print "There you go !" }' List of html files: as1.html as2.html index.html There you go ! * of 44 Awk Patterns/regular expression/ Relational expression>, <, >=, <=, ==Pattern && patternPattern || patternPattern1 ? Pattern2 : pattern3If Pattern1 is True, then Pattern2, else pattern 3(pattern)! Pattern * of 44 Example Patterns $ cat textfile2 Just a text file Nothing to see here Some lines have More fields than others
  • 73. And some Are blank $ awk 'NF > 3 {print $0}' textfile2 Just a text file Nothing to see here More fields than others $ awk 'NF > 3 || /^$/ {print $0}' textfile2 Just a text file Nothing to see here More fields than others $ awk 'NF > 3 ? /file/ : /^And/ {print $0}' textfile2 Just a text file Nothing to see here And some * of 44 Awk ActionsEnclosed in { }() Grouping$ Field reference++ -- Increment, decrement^ Exponentiation+ - ! Plus, minus, not* / % Multiplication, division, and modulus * of 44 * * of 44
  • 74. * $ awk '{print $1, $2 * $3}' empsrh John 325 Smith 420 Tom 117 George 756 Sam 132 $cat empsrh John 13 25 Smith 14 30 Tom 9 13 George 21 36 Sam 11 12 * of 44 * $ awk '{print "total pay for", $1, " is ", $2 * $3}' empsrh total pay for John is 325 total pay for Smith is 420 total pay for Tom is 117 total pay for George is 756 total pay for Sam is 132 $ cat empsrh John 13 25 Smith 14 30 Tom 9 13 George 21 36 Sam 11 12
  • 75. * of 44 * * of 44 * * of 44 Example: Computing with awk $cat empsrh John 13 25 Smith 14 30 Tom 9 13 George 21 36 Sam 11 12 $ awk '$3 > 15 {emp = emp +1} END {print emp, “employees worked more than 15 hours"}' empsrh 3 employees worked more than 15 housr * of 44
  • 76. Example: Computing with awk $cat empsrh John 13 25 Smith 14 30 Tom 9 13 George 21 36 Sam 11 12 $awk '{pay = pay + $2 * $3} END {print NR, "employees"; print "total pay is", pay; print "average pay is", pay/NR}' empsrh 5 employees total pay is 1750 average pay is 350 * of 44 * $ awk '$2 > maxrate {maxrate = $2; maxemp =$1} END {print "Highest rate is:", maxrate, "for", maxemp}’ emprate * of 44 awk $ cat em2 Tom Jones:4424: 5/12/66:543354 Mary Adam:5346:11/4/63:28765 Sally Chang:1654:7/22/54:650000
  • 77. Billy Black:1683:9/23/44:336500 $ awk -F: '{names=names $1 " "} END {print names}' em2 Tom Jones Mary Adam Sally Chang Billy Black * * of 44 Example: I want to go through and calculate the average score on the Midterm $ cat grades Jason William:Midterm:100 Jane Smith:Quiz 1:45 Tom Ram:Final:78 Sarah George:Midterm:23 Franklin Rob:Midterm:46 $ awk -F: '/Midterm/ {count++; sum= sum + $3} BEGIN {count=0 ; sum=0} END {print sum/count}' grades 56.3333 $ * of 44 Another Example Adding 12 points to everyone’s midterm score $ cat grades Jason William:Midterm:100 Jane Smith:Quiz 1:45 Tom Ram:Final:78 Sarah George:Midterm:23 Franklin Rob:Midterm:46 $awk -F: '/Midterm/ {$3 =$3 + 12; print $0} /Quiz/ || /Final/ {print $0}' grades
  • 78. Jason William Midterm 112 Jane Smith:Quiz 1:45 Tom Ram:Final:78 Sarah George Midterm 35 Franklin Rob Midterm 58 * of 44 Example $ colors=(red blue orange green purple) $ echo ${colors[@]} | awk '{for (i=NF; i > 0; --i) print $i}’ purple green orange blue red * of 44 awk Versus bash $ argumentsAlways enclose everything to awk in single quotes $1 to awk means something completely different than $1 to bash$1 in awk means first field$1 in bash means first command line argument * of 44
  • 79. User Defined Variables Variable names could be anything, but it can’t begin with a number. You can assign a variable as in shell scripting like this: $ cat script0 BEGIN { test="This is a test" print test } $ awk -f script0 This is a test * of 44 Example: script in a file $ cat testfile {print $1 "home at " $6} $ awk -F: -f testfile /etc/passwd kuskarhome at /home/STUDENTS/majors/kuskar juswstahome at /home/STUDENTS/majors/juswsta dusgmoohome at /home/STUDENTS/nonmajors/dusgmoo jerpcamhome at /home/STUDENTS/majors/jerpcam pralam6home at /home/STUDENTS/majors/pralam6 jonnrob1home at /home/STUDENTS/majors/jonnrob1 …. $ cat testfile2 { text = $1 "home at " $6 print text }
  • 80. $ head /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin mail:x:8:8:mail:/var/mail:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin * of 44 $ cat testfile3 BEGIN { print "users and their corresponding home" print "UserName t HomePath" print "_________ t __________" FS=":" } { print $1 " t " $6 } END { print "The end" } $ awk -f testfile3 /etc/passwd users and their corresponding home UserName HomePath _________ ________ root /root daemon /usr/sbin bin /bin
  • 81. sys /dev sync /bin games /usr/games $ head /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin mail:x:8:8:mail:/var/mail:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin * of 44 Sometimes, the fields are distributed without a fixed separator. In these cases, FIELDWIDTHS variable solves the problem. $ cat testfile4 1235.96521 927-8.3652 36257.8157 $awk 'BEGIN {FIELDWIDTHS="3 4 3"}{print $1, $2, $3}' testfile4 123 5.96 521 927 -8.3 652 362 57.8 157 * of 44
  • 82. Suppose that your data are distributed on different lines $ cat testfile5 Jalal Omer 123 High Street (222) 466-1234 James Smith 456 High Street (333) 456-7890 $ awk 'BEGIN {FS="n"; RS=""} {print $1," ", $2," ", $3}' testfile5 Jalal Omer 123 High Street (222) 466-1234 James Smith 456 High Street (333) 456-7890 * of 44 $ cat testfile6 { if ($1 > 30) { x = $1 * 3 print x } else { x = $1 / 2 print x } } $ awk -f testfile6 numbers 5
  • 83. 7.5 3 99 135 10 4 11 $ cat numbers 10 15 6 33 45 20 8 22 * of 44 While Loop You can use the while loop to iterate over data with a condition. $ cat testfile7 { sum = 0 i = 1 while (i < 4) { sum += $i i++ } average = sum / 3 print "Average: ", average
  • 84. } $ awk -f testfile7 nums Average: 127 Average: 129.667 Average: 192.667 Average: 165.333 $ cat nums 124 127 130 112 142 135 175 158 245 118 231 147 For each input line do From here To Here * of 44 You can exit the loop using break command like this: $ cat testfile8 { sum = 0 i = 1 while (i < 4) { sum += $i i++ if (i == 3) break } average = sum / 3 print "Average: ", average } [email protected]:~$ awk -f testfile8 nums
  • 85. Average: 83.6667 Average: 84.6667 Average: 111 Average: 116.333 $ cat nums 124 127 130 112 142 135 175 158 245 118 231 147 Wrong averages. Why? * of 44 The for Loop $ cat testfile9 { sum = 0 for (i =1; i < 4; i++) { sum += $i } average = sum / 3 print "Average: ", average } [email protected]:~$ awk -f testfile9 nums Average: 127 Average: 129.667 Average: 192.667 Average: 165.333 $ cat nums 124 127 130 112 142 135
  • 86. 175 158 245 118 231 147 * of 44 Mathematical Functions sin(x) cos(x) sqrt(x) exp(x) log(x) rand() $ awk 'BEGIN{x=rand(); y= sqrt(16); print x, y}' 0.237788 4 String Functions $ awk 'BEGIN{x="likegeeks"; print toupper(x)}' LIKEGEEKS * of 44 User Defined Functions $ cat testfile10 function myfunc() { printf "The user %s has home path at %s n", $1, $6 } BEGIN {FS=":"} { myfunc() } $ awk -f testfile10 /etc/passwd The user root has home path at /root The user daemon has home path at /usr/sbin The user bin has home path at /bin
  • 87. The user sys has home path at /dev The user sync has home path at /bin The user games has home path at /usr/games For each input line do Call this function $ head /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin mail:x:8:8:mail:/var/mail:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin * of 44 Awk Text Split into an array $ echo "12 23 11" | awk '{split($0, a); print a[3], a[2], a[1]}' 11 23 12 $ echo "12,23,11" | awk '{split($0, a,","); print a[3], a[2], a[1]}' 11 23 12 $ awk ‘{split($0, arr, “:”), print arr[4], arr[1]}’ inputfile * of 44 $ cat file1
  • 88. Item1,200 Item2,500 Item3,900 Item2,800 Item1,600 $ awk -F, '{print > $1}' file1 $ cat Item1 Item1,200 Item1,600 $ cat Item3 Item3,900 $ cat Item2 Item2,500 Item2,800 $ awk -F, '{print > $1".txt"}' file1 $ ls *.txt Item2.txt Item1.txt Item3.txt Splitting a file into several files Print the entire line Into a file named $1 * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-5: The Old Password Prompt 3.1.2 Changing Your Password: The passwd Command The passwd command changes your current password. If you do not have a password, it creates one. Type passwd and press
  • 89. [Return] (UNIX displays the Old password: prompt) Enter your current password and press [Return] (UNIX displays the New password: prompt) Figure 3-6 New Password Prompt Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-7 Re-Enter New Password Prompt Enter your new password (UNIX shows the Re-enter new password: prompt) Retype the new password (UNIX is verifies that you did not make a mistake) Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 *
  • 90. * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-8 Sample of Error Messages Password Format Your password must comply with the following criteria: • The new password must differ from the old one by at least three characters • The password must be at least six characters long and must contain at least two characters and one number • The password must differ from your User ID If UNIX detects anything wrong with your password, it displays an error message and shows the New password: prompt again. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal
  • 91. UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-9 Changing a Password in the Linux Environment The wordings of the prompts are slightly different for the Linux system, but the command and sequence of prompts are the same. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-10 Sample of Error Messages Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal
  • 92. UNIX Unbounded, 5th Edition Copyright ©2008 3.1.3 General Rules for Choosing Passwords • Do not use a dictionary word (in any language) • Do not use the name of a person, pet, location, or a character from a book • Do not use any variation of your name and ID/account name • Do not use known information about you, such as your phone number, birthday, and so on • Do not use a simple pattern or easy sequence of keyboard keys Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal
  • 93. UNIX Unbounded, 5th Edition Copyright ©2008 3.1.4 Logging Off The process of signing off when you have finished with the system is called logging off or logging out To log off, press [Ctrl-d], which means to simultaneously hold down the key labeled Ctrl (for Control) and press the letter d UNIX responds first by displaying logging off messages Then UNIX shows the system’s standard welcome message and login: prompt exit command Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-11: The Logging-In and Logging-Out Process Amir Afzal UNIX Unbounded, 5th Edition
  • 94. Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-12 The Command Line Format 3.2.2 Basic Command Line Structure Each command line consists of three fields: • Command name • Options • Arguments Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal
  • 95. UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-13 The date Command 3.2.3 Date and Time Display: The date Command The date command displays the current date and time on the screen The date and time are set by the system administrator and users cannot change them Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 3.2.4 Information on Users: The who Command The who command lists the login names, terminal lines, and login times of the users who are currently logged on the system The who command is used to check the level of activity in the system or to find out whether a particular person is on the system Amir Afzal
  • 96. UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-14 The who Command 1. The first column shows the login name of the user 2. The second column identifies the terminal being used 3. The tty number gives you some indication about the location of the terminal 4. The third and fourth columns show the date and time that each user logged in Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started
  • 97. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-15: The who Command with am i Argument Type who am I or who am I (UNIX displays who the system thinks you are) Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 who Options Table 3.1 lists some of the who command options Linux also provides some alternative and new options Under Linux, some of these options are not available, or some of the options outputs are slightly different 1. Linux new and alternative options are listed under the Linux heading in Table 3.1 2. Linux new and alternative options are preceded by two minus signs (--)
  • 98. Table 3.1 The who Command OptionsOptionLinuxOperation-q- -countThe quick who; just displays the name and number of users-H--headingDisplays heading above each column- bDisplays the time and date of the last reboot--helpDisplays a usage message Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-16A The who Command with -H Option Figure 3-16B The who Command Using Linux --heading Option Amir Afzal UNIX Unbounded, 5th Edition
  • 99. Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-17A The who Command with -q Option Figure 3-17B The who Command Using Linux --count Option Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-18 The who Command with -b Option Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
  • 100. * * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-19 The who Command Using Linux --help Option Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 3.2.5: Display a Calendar: The cal Command The cal command displays the calendar for the specified year. If the year and the month are both specified, then the calendar for just that
  • 101. month is displayed The default argument for the cal command is the current month Displaying the calendar for the current month (see Figure 3.20). (Assuming current month, year is November 2005) Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-20 The cal Command with No Option Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal
  • 102. UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-21 The cal Command Output Notes:Type the specified year in full year value. For example, type cal 2010 (not cal 10) Use the month number (1 to 12) and not the month name The cal command without arguments displays calendar of the current month The cal command with year argument but without month argument displays a calendar for the specified year Displaying the calendar for November 2010. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-22 Learn Utility Main Menu 3.3 GETTING HELP 3.3.1 Using the learn Command The learn command brings up a computer-aided instruction program that is arranged in a series of courses and lessons. Amir Afzal UNIX Unbounded, 5th Edition
  • 103. Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-23 Help Utility Main Menu 3.3.2 Using the help Command The help command is more popular than the learn command and is available on many UNIX systems. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 3.3.3 Getting More Information: The UNIX Manual
  • 104. You can find a detailed description of the UNIX system in a large document called the User’s Manual. Some installation may have a printed copy of this manual The electronic version of the manual is stored on disk and is called the online manual The UNIX user’s manual is tersely written and difficult to read It is more like a reference guide than a true user’s manual Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 3.3.4 Using the Electronic Manual: The man Command The man (manual) command shows pages from the online system documentation. Type man followed by the name of the command You can use man to get the details about the commands For example type: man cal [Return] (UNIX responds by showing a page similar to Figure
  • 105. 3.24) Note: Nearly all UNIX installations provide the man pages, and the man command is the most popular way to obtain detailed information about command usage and options. Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-24 man Utility Display of cal Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal
  • 106. UNIX Unbounded, 5th Edition Copyright ©2008 3.4 CORRECTING TYPING MISTAKES The shell program interprets the command line after you press the [Return] key. Erasing Characters Use the [Backspace] key for erasing characters or use [Ctrl-h] once for each character you intend to erase Erasing an Entire Line You can erase an entire line any time before pressing [Return] [Ctrl-u] removes the entire command line and the cursor moves to a blank line The character that erases the entire line is called the kill character Terminating Program Execution The character that terminates your running program is called the interrupt character On most systems, [Del] or [Ctrl-c] is assigned as the interrupt character The interrupt character stops the running program and causes the shell prompt $ to be displayed Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * * of 45 Chapter 3: Getting Started Amir Afzal
  • 107. UNIX Unbounded, 5th Edition Copyright ©2008 Figure 3-25 UNIX Error Message Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 * Introduction to Unix and Linux * What is an Operating System?The operating system (OS) is the program which starts up when you turn on your computer and runs underneath all other programs - without it nothing would happen at all.In simple terms, an operating system is a manager. It manages all the available resources on a computer, from the CPU, to memory, to hard disk accesses.Tasks the operating system must perform:Control Hardware - The operating system controls all the parts of the computer and attempts to get everything working together. Run Applications - Another job the OS does is run application software. This would include word processors, web browsers, games, etc... Manage Data and
  • 108. Files - The OS makes it easy for you to organize your computer. Through the OS you are able to do a number of things to data, including copy, move, delete, and rename it. This makes it much easier to find and organize what you have. * * * Operating System FunctionsInitialize computer hardwareAllocate system resources to programs Keep track of multiple programs running at same timeProvide organized method for all programs to use system devices UNIX Structure Introduction to Linux * * Structure
  • 109. * Parts of the UNIX OSThe Kernel - handles memory management, input and output requests, and program scheduling. Technically speaking, the kernel is the OS. It provides the basic software connection to the hardware. The Shell and Graphical User Interfaces (GUIs) - basic UNIX shells provides a “command line” interface which allows the user to type in commands. These commands are translated by the shell into something the kernel can comprehend, and then executed by the kernel. The Built-in System Utilities - are programs that allow a user to perform tasks which involve complex actions. Utilities provide user interface functions that are basic to an operating system, but which are too complex to be built into the shell. Examples of utilities are programs that let us see the contents of a directory, move & copy files, remove files, etc... Application Software & Utilities – these are not part of the operating system, per se. They are additional programs that are bundled with the OS distribution, or available separately. These can range from additional or different versions of basic utilities, to full scale commercial applications. * * UNIX
  • 110. Unix is a multi-user, multi-tasking operating system. You can have many users logged into a system simultaneously, each running many programs. It's the kernel's job to keep each process and user separate and to regulate access to system hardware, including cpu, memory, disk and other I/O devices. * * General Characteristics of UNIX as an Operating System (OS)Multi-user & Multi-tasking - most versions of UNIX are capable of allowing multiple users to log onto the system, and have each run multiple tasks. This is standard for most modern OSs. Over 40 Years Old - UNIX is over 40 years old and it's popularity and use is still high. Over these years, many variations have spawned off and many have died off, but most modern UNIX systems can be traced back to the original versions. It has endured the test of time. For reference, Windows at best is half as old (Windows 1.0 was released in the mid 80s, but it was not stable or very complete until the 3.x family, which was released in the early 90s). Large Number of Applications – there are an enormous amount of applications available for UNIX operating systems. They range from commercial applications such as CAD, Maya, WordPerfect, to many free applications. Free Applications and Even a Free Operating System - of all of the applications available under UNIX, many of them are free. The compilers and interpreters that we use in most of the programming can be downloaded free of charge. Less Resource Intensive - in general, most UNIX
  • 111. installations tend to be much less demanding on system resources. In many cases, the old family computer that can barely run Windows is more than sufficient to run the latest version of Linux. Internet Development - Much of the backbone of the Internet is run by UNIX servers. Many of the more general web servers run UNIX with the Apache web server - another free application. * * History of UNIX First Version was created in Bell Labs in 1969. Some of the Bell Labs programmers who had worked on this project, Ken Thompson, Dennis Ritchie, Rudd Canaday, and Doug McIlroy designed and implemented the first version of the Unix File System on a PDP-7 along with a few utilities. It was given the name UNIX by Brian Kernighan. 00:00:00 Hours, Jan 1, 1970 is time zero for UNIX. It is also called as epoch. * *
  • 112. History of UNIX 1973 Unix is re-written mostly in C, a new language developed by Dennis Ritchie. Being written in this high-level language greatly decreased the effort needed to port it to new machines. * * History of UNIX Introduction to Linux 1977 There were about 500 Unix sites world-wide. 1980 BSD 4.1 (Berkeley Software Development) 1983 SunOS, BSD 4.2, System V 1988 AT&T and Sun Microsystems jointly develop System V Release 4 (SVR4). This later developed into UnixWare and Solaris 2. 1991 Linux was originated. An interesting and rather up-to-date timeline of these variations of UNIX can be found at http://www.levenez.com/unix/history.html.
  • 113. * * Flavors of UNIXThese can be grouped into two categories: Open Source and TrademarkedTrademarked : (redistribution and modification prohibited or restricted; not free)Solaris . IRIXMac OS X . and many others... Open Source: (source code is readily available and free to modify)FreeBSDLinux Distributions:RedHat and the Fedora Project (maintained by RedHat)MandrakeDebianSuSESlackwareUbuntuand many others...As a side note, Linux is a open source UNIX-based OS that was originally developed in 1991 by Linus Torvalds, a Finnish undergraduate student. * * What is Linux?A clone of UnixDeveloped in 1991 by Linus Torvalds, a Finnish graduate studentInspired by and replacement of Minix Minix was a mini-OS originally developed by Andrew Tanenbaum to teach of the fundamentals of operating system design Linus' Minix became LinuxConsist ofLinux KernelGNU (GNU is Not Unix) Software differs from Unix by being free software and containing no Unix code.Software Package management *
  • 114. The GNU Linux project was created for the development of a Unix-like operating system that comes with source code that can be copied, modified, and redistributed What is LINUX Introduction to Linux Linux is a free Unix-type operating system originally created by Linus Torvalds with the assistance of developers around the world. The Kernel version 1.0 was released in 1994 and today the most recent stable version is 2.6.9 Developed under the GNU General Public License , the source code for Linux is freely available to everyone. * * What is Linux?Originally developed for 32-bit x86-based PCPorted to other architectures, eg.Alpha, VAX, PowerPC, IBM S/390, MIPS, IA-64PS2, TiVo, cellphones, watches, Nokia N810, NDS, routers, NAS, GPS, … *
  • 115. Which Linux Distribution is better?> 300 Linux DistributionsSlackware (one of the oldest, simple and stable distro.)RedhatRHEL (commercially support)Fedora (free)CentOS (free RHEL, based in England)SuSe ( based in German)Gentoo (Source code based)Debian (one of the few called GNU/Linux)Ubuntu (based in South Africa)Knoppix (first LiveCD distro.)… * * The Free Software Foundation and the GNU ProjectFree software foundation (FSF)Software itself should not be restricted in distribution by standard commercial license agreementGNU projectCompletely free version of UNIXWritten from scratch * The Free Software Foundation and the GNU Project (continued)Software licenseLegal definition of who can use software and how it can be usedGNU general public license (GPL) Very different from standard commercial software licenseAuthor agrees to give away source codeAnyone is licensed to redistribute it in any form * Linux ArrivesLinus TorvaldsDecided to create UNIX-like operating system kernel for IBM-compatible PCSolicited help via InternetReleased Linux kernel under GPLLinux development methodPerson identifies need and begins writing
  • 116. programDeveloper announces project on Internet * Linux Arrives (continued)Linux development method (continued)Others respond and work on different parts of projectPerson leading project releases softwarePeople download source code and try program; send back information about problems Developers fix bugsForkingCreating new project based on existing source code * Motivating Free Software DevelopersWhy would so many people devote so much effort to something without expecting any reward?Fills developer’s specific technical needRespect of like-minded professionalsSense of contribution and communityValuable boost to developer’s resume * The Strengths Of LinuxStabilitySecuritySpeedCostMultiprocessing and other high-end featuresApplications * Hardware RequirementsCan run on very minimal hardwareRecommend that computer have minimum of:1 GB of free disk space 64 MB of RAMFor Red Hat Enterprise Linux installations:256 MB of RAM300 MHZ CPU800 MB of free disk space
  • 117. * Version NumberingVersion numbers assigned to:Each release of Linux kernel Each component of Linux distribution Linux distributionsMost users select latest available version * Linux CertificationIndustry certification programsRed Hat Certified TechnicianRed Hat Certified EngineerLPI Certification (Linux Professional Institute)Linux Certified Administrator (LCA) CertificationLinux+ CertificationNovell Certified Linux Engineer * Linux Certification (continued)Red Hat’s certification programVery highly regardedTraining program consists of three courses * The Work of a System AdministratorLinux is increasingly part of information technology infrastructure of large organizationsKnowledge of Linux can set you on path to a fulfilling and profitable career * Careers in LinuxSystem administrator Network administrator
  • 118. Software engineerTrainer Technical writer Product marketing Business consultant ECU CS server Unix version [email protected]:~$ cat /etc/os-release NAME="Ubuntu" VERSION="18.04.3 LTS (Bionic Beaver)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 18.04.3 LTS" VERSION_ID="18.04" HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/term s-and-policies/privacy-policy" VERSION_CODENAME=bionic UBUNTU_CODENAME=bionic * BASH SHELL PROGRAMMINGInputprompting usercommand line argumentsDecision:if-then-elsecaseRepetitiondo-while, repeat-untilforselectFunctionsTraps * USER INPUTshell allows to prompt for user input Syntax:
  • 119. read varname [more vars] or read –p "prompt" varname [more vars] words entered by user are assigned to varname and “more vars”last variable gets rest of input line * USER INPUT EXAMPLE #! /bin/sh read -p "enter your name: " first last echo "First name: $first" echo "Last name: $last" * BASH CONTROL STRUCTURESif-then- elsecaseloopsforwhileuntilselect * RELATIONAL OPERATORS *MeaningNumericStringGreater than-gtGreater than or equal- geLess than-ltLess than or equal-leEqual-eg= or ==Not equal- ne!=str1 is less than str2str1 < str2str1 is greater str2str1 > str2String length is greater than zero-n strString length is zero-z str
  • 120. COMPOUND LOGICAL EXPRESSIONS ! not && and || or * and, or must be enclosed within [[ ]] EXAMPLE: USING THE ! OPERATOR #!/bin/bash read -p "Enter years of work: " Years if [ ! "$Years" -lt 20 ]; then
  • 121. echo "You can retire now." else echo "You need 20+ years to retire" fi * EXAMPLE: USING THE && OPERATOR #!/bin/bash Bonus=500 read -p "Enter Status: " Status read -p "Enter Shift: " Shift if [[ "$Status" = "H" && "$Shift" = 3 ]] then echo "shift $Shift gets $$Bonus bonus" else echo "only hourly workers in" echo "shift 3 get a bonus" fi * EXAMPLE: USING THE || OPERATOR #!/bin/bash read -p "Enter calls handled:" CHandle read -p "Enter calls closed: " CClose if [[ "$CHandle" -gt 150 || "$CClose" -gt 50 ]] then echo "You are entitled to a bonus" else echo "You get a bonus if the calls" echo "handled exceeds 150 or"
  • 122. echo "calls closed exceeds 50" fi * FILE TESTING Meaning -d file True if ‘file’ is a directory -f file True if ‘file’ is an ord. file -r file True if ‘file’ is readable -w file True if ‘file’ is writable -x file True if ‘file’ is executable -s file True if length of ‘file’ is nonzero * EXAMPLE: FILE TESTING #!/bin/bash echo "Enter a filename: " read filename if [ ! –r "$filename" ] then echo "File is not read-able" exit 1 fi * EXAMPLE: FILE TESTING #! /bin/bash if [ $# -lt 1 ]; then echo "Usage: filetest filename"
  • 123. exit 1 fi if [[ ! -f "$1" || ! -r "$1" || ! -w "$1" ]] then echo "File $1 is not accessible" exit 1 fi * EXAMPLE: IF… STATEMENT # The following THREE if-conditions produce the same result * DOUBLE SQUARE BRACKETS read -p "Do you want to continue?" reply if [[ $reply = "y" ]]; then echo "You entered " $reply fi * SINGLE SQUARE BRACKETS read -p "Do you want to continue?" reply if [ $reply = "y" ]; then echo "You entered " $reply fi * "TEST" COMMAND read -p "Do you want to continue?" reply if test $reply = "y"; then echo "You entered " $reply fi * EXAMPLE: IF..ELIF... STATEMENT
  • 124. #!/bin/bash read -p "Enter Income Amount: " Income read -p "Enter Expenses Amount: " Expense let Net=$Income-$Expense if [ "$Net" -eq "0" ]; then echo "Income and Expenses are equal - breakeven." elif [ "$Net" -gt "0" ]; then echo "Profit of: " $Net else echo "Loss of: " $Net fi * THE CASE STATEMENTuse the case statement for a decision that is based on multiple choices Syntax: case word in pattern1) command-list1 ;; pattern2) command-list2 ;; patternN) command-listN ;; esac * CASE PATTERNchecked against word for matchmay also contain: *
  • 125. ? [ … ] [:class:]multiple patterns can be listed via: | * [:digit:] Digits: 0 1 2 3 4 5 6 7 8 9 [:alnum:] Alphanumeric [:alpha:] [:lower:] EXAMPLE 1: THE CASE STATEMENT #!/bin/bash echo "Enter Y to see all files including hidden files" echo "Enter N to see all non-hidden files" echo "Enter q to quit" read -p "Enter your choice: " reply case $reply in Y|YES) echo "Displaying all (really…) files" ls -a ;; N|NO) echo "Display all non-hidden files..." ls ;; Q) exit 0 ;; *) echo "Invalid choice!"; exit 1 ;; esac *
  • 126. EXAMPLE 2: THE CASE STATEMENT #!/bin/bash ChildRate=3 AdultRate=10 SeniorRate=7 read -p "Enter your age: " age case $age in [1-9]|[1][0-2]) # child, if age 12 and younger echo "your rate is" '$'"$ChildRate.00" ;; # adult, if age is between 13 and 59 inclusive [1][3-9]|[2-5][0-9]) echo "your rate is" '$'"$AdultRate.00" ;; [6-9][0-9]) # senior, if age is 60+ echo "your rate is" '$'"$SeniorRate.00" ;; esac * BASH PROGRAMMING: SO FARData structureVariablesNumeric variablesArraysUser inputControl structuresif-then-elsecase * BASH PROGRAMMING: STILL TO COME Control structuresRepetitiondo-while, repeat- untilforselectFunctionsTrapping signals * The Bash Shell The Bash Shell Copyright Department of Computer Science, Northern Illinois
  • 127. University, 2005 09-* Copyright Department of Computer Science, Northern Illinois University, 2005 THE WHILE LOOPPurpose: To execute commands in “command-list” as long as “expression” evaluates to true Syntax: while [ expression ] do command-list done * EXAMPLE: USING THE WHILE LOOP #!/bin/bash COUNTER=0 while [ $COUNTER -lt 10 ] do echo The counter is $COUNTER let COUNTER=$COUNTER+1 done * EXAMPLE: USING THE WHILE LOOP #!/bin/bash
  • 128. Cont="Y" while [ $Cont = "Y" ]; do ps -A read -p "want to continue? (Y/N)" reply Cont=`echo $reply | tr [:lower:] [:upper:]` done echo "done" * Translate from lower case to upper case THE UNTIL LOOPPurpose: To execute commands in “command-list” as long as “expression” evaluates to false Syntax: until [ expression ] do command-list done * EXAMPLE: USING THE UNTIL LOOP #!/bin/bash COUNTER=20 until [ $COUNTER -lt 10 ] do echo $COUNTER let COUNTER-=1 done *
  • 129. EXAMPLE: USING THE UNTIL LOOP #!/bin/bash Stop="N" until [ $Stop = "Y" ]; do ps -A read -p "want to stop? (Y/N)" reply Stop=`echo $reply | tr [:lower:] [:upper:]` done echo "done" * THE FOR LOOPPurpose: To execute commands as many times as the number of words in the “argument-list” Syntax: for variable in argument-list do commands done * EXAMPLE 1: THE FOR LOOP #!/bin/bash for i in 7 9 2 3 4 5 do echo $i
  • 130. done * EXAMPLE 2: USING THE FOR LOOP #!/bin/bash # compute the average weekly temperature for num in 1 2 3 4 5 6 7 do read -p "Enter temp for day $num: " Temp let TempTotal=$TempTotal+$Temp done let AvgTemp=$TempTotal/7 echo "Average temperature: " $AvgTemp * LOOPING OVER ARGUMENTSsimplest form will iterate over all command line arguments: #! /bin/bash for parm do echo $parm done * SELECT COMMANDConstructs simple menu from word listAllows user to enter a number instead of a wordUser enters sequence number corresponding to the word
  • 131. Syntax: select WORD in LIST do RESPECTIVE-COMMANDS done Loops until end of input, i.e. ^d (or ^c) * SELECT EXAMPLE #! /bin/bash select var in alpha beta gamma do echo $var done Prints: * 1) alpha 2) beta 3) gamma #? 2 beta #? 4 #? 1 alpha SELECT DETAILPS3 is select sub-prompt$REPLY is user input (the number) #! /bin/bash PS3="select entry or ^D: " select var in alpha beta
  • 132. do echo "$REPLY = $var" done * Output: select entry or ^D: 1) alpha 2) beta ? 2 2 = beta ? 1 1 = alpha SELECT EXAMPLE #!/bin/bash echo "script to make files private" echo "Select file to protect:" select FILENAME in * do echo "You picked $FILENAME ($REPLY)" chmod go-rwx "$FILENAME" echo "it is now private" done * BREAK AND CONTINUEInterrupt for, while or until loopThe break statement transfer control to the statement AFTER the done statementterminate execution of the loopThe continue statementtransfer control to the statement TO the done statementskip the test statements for the current iterationcontinues execution of the loop
  • 133. * The Bash Shell * Copyright Department of Computer Science, Northern Illinois University, 2005 THE BREAK COMMAND while [ condition ] do cmd-1 break cmd-n done echo "done" * This iteration is over and there are no more iterations The Bash Shell The Bash Shell Copyright Department of Computer Science, Northern Illinois University, 2005 09-* Copyright Department of Computer Science, Northern Illinois University, 2005
  • 134. THE CONTINUE COMMAND while [ condition ] do cmd-1 continue cmd-n done echo "done" * This iteration is over; do the next iteration The Bash Shell The Bash Shell Copyright Department of Computer Science, Northern Illinois University, 2005 09-* Copyright Department of Computer Science, Northern Illinois University, 2005 EXAMPLE: for index in 1 2 3 4 5 6 7 8 9 10 do if [ $index –le 3 ]; then echo "continue" continue fi echo $index if [ $index –ge 8 ]; then
  • 135. echo "break" break fi done * The Bash Shell * Copyright Department of Computer Science, Northern Illinois University, 2005 DONE ! BASH SHELL PROGRAMMINGSequenceDecision:if-then- elsecaseRepetitiondo-while, repeat-untilforselectFunctionsTraps * still to come SHELL FUNCTIONSA shell function is similar to a shell scriptstores a series of commands for execution latershell stores functions in memoryshell executes a shell function in the same shell that called itWhere to defineIn .profileIn your scriptOr on the command lineRemove a functionUse unset built-in * The Bash Shell
  • 136. * Copyright Department of Computer Science, Northern Illinois University, 2005 SHELL FUNCTIONSmust be defined before they can be referencedusually placed at the beginning of the script Syntax: function-name () { statements } * The Bash Shell Copyright Department of Computer Science, Northern Illinois University, 2005 EXAMPLE: FUNCTION #!/bin/bash funky () { # This is a simple function echo "This is a funky function." echo "Now exiting funky function." } # declaration must precede call:
  • 137. funky * The Bash Shell Copyright Department of Computer Science, Northern Illinois University, 2005 EXAMPLE: FUNCTION #!/bin/bash fun () { # A somewhat more complex function. JUST_A_SECOND=1 let i=0 REPEATS=30 echo "And now the fun really begins." while [ $i -lt $REPEATS ] do echo "-------FUNCTIONS are fun-------->" sleep $JUST_A_SECOND let i+=1 done } fun * FUNCTION PARAMETERSNeed not be declaredArguments provided via function call are accessible inside function as $1, $2, $3, … $# reflects number of parameters
  • 138. $0 still contains name of script (not name of function) * EXAMPLE: FUNCTION WITH PARAMETER #! /bin/sh testfile() { if [ $# -gt 0 ]; then if [[ -f $1 && -r $1 ]]; then echo $1 is a readable file else echo $1 is not a readable file fi fi } testfile * * EXAMPLE: FUNCTION WITH PARAMETERS #! /bin/bash checkfile() { for file do if [ -f "$file" ]; then echo "$file is a file" else if [ -d "$file" ]; then echo "$file is a directory" fi fi done
  • 139. } Checkfile $1 $2 $3 * LOCAL VARIABLES IN FUNCTIONSVariables defined within functions are global, i.e. their values are known throughout the entire shell program keyword “local” inside a function definition makes referenced variables “local” to that function * EXAMPLE: FUNCTION #! /bin/bash global="pretty good variable" foo () { xyz=124 echo “xyz inside function = $xyz” local rrr="not so good variable" echo $global echo $rrr global="better variable" } echo “xyz outside function = $xyz” echo $global foo echo $global echo $rrr *
  • 140. RECURSION; FUNCTIONS CAN BE RECURSIVE - HERE'S A SIMPLE EXAMPLE OF A FACTORIAL FUNCTION: * HANDLING SIGNALSUnix allows you to send a signal to any process -1 = hangup kill -HUP 1234 -2 = interrupt with ^C kill -2 1235no argument = terminate kill 1235-9 = kill kill -9 1236-9 cannot be blocked list your processes with ps -u userid * LIST OF THE COMMONLY USED SIGNAL NUMBERS, DESCRIPTION AND WHETHER THEY CAN BE TRAPPED OR NOT: * SIGNALS ON LINUX % kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16)
  • 141. SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX ^C is 2 - SIGINT * HANDLING SIGNALSDefault action for most signals is to end processterm: signal handler Bash allows to install custom signal handler Syntax: trap 'handler commands' signals Example: trap 'echo do not hangup' 1 2
  • 142. * EXAMPLE: TRAP HANGUP #! /bin/bash trap 'echo dont terminate me’ 2 while true do echo "try to terminate" sleep 1 done * EXAMPLE: TRAP MULTIPLE SIGNALS #! /bin/sh trap 'echo 2’ 2 trap 'echo 3’ 3 while true; do echo -n . sleep 1 done * EXAMPLE: REMOVING TEMP FILES #! /bin/bash cleanup () { /bin/rm -f /tmp/tempfile.$$.?
  • 143. } trap 'cleanup; exit' 2 for i in 1 2 3 4 5 6 7 8 do echo "$i.iteration" touch /tmp/tempfile.$$.$i sleep 1 done cleanup * RESTORING DEFAULT HANDLERStrap without a command list will remove a signal handlerUse this to run a signal handler once only #! /bin/sh trap 'justonce' 2 justonce() { echo "not yet" trap 2 # now reset it } while true; do echo -n "." sleep 1 done *
  • 144. DONE ! SUMMARY: BASH SHELL PROGRAMMINGSequenceDecision:if-then- elsecaseRepetitiondo-while, repeat-untilforselectFunctionsTraps * The vi Editor The vi EditorWhen you write some C or Java programs or shell (or perl) scripts, or edit some system files, you need to use an editor.There are two versatile editors in UNIX – vi and emacs.vi is a full-screen editor. It was created by a graduate student – Bill Joy – later to become the cofounder of Sun Microsystems. The vi/vim EditorA vi session begins by invoking the command vi with (or without) a filename: vi myfileYou are presented a full empty screen, each line beginning with a ~ (tilde). The vi/vim EditorThe ~ is vi’s way to indicating that they are nonexistent line.For text editing, vi uses 24 of the 25 lines that are normally available in a terminal. The last line is reserved for some commands that you’ll enter to act on the text. This line is also used by the system to display messages.
  • 145. The Three ModesThere are three modes in which vi works:Command Mode – Where keys are used as commands to act on text.Input Mode – Where any key depressed is entered as text.Last Line Mode - Where commands can be entered in the last line of the screen to act on text. Quitting vi – The Last Line ModeThe editor works with a copy of the file which is placed in a buffer that is simply a temporary storage area which is associated with the file on disk.It is necessary to know how to leave the editor. Saving and quitting are handled by the Last Line Mode.Every command in this mode is preceded by a : (colon) and followed by the [Enter] key. Quitting vi – The Last Line ModeRemember to leave the Input Mode by pressing the [Esc] key.The Last Line Mode offers two ways of saving and quitting - :x and :wq.The commands return you to the shell after saving your work. Quitting vi – The Last Line ModeTo abort editing, you can use q (quit) command.The q command takes you out of the editor only if you don’t have a changed buffer. : q [Enter] $The q! command always return you to the prompt irrespective of the status of the buffer.
  • 146. Quitting viThese are exit commands in vi::x – saves files and quits editing mode:wq – saves files and quits editing mode.:q – quits editing mode when no changes are made to file:q! – quits editing mode but after abandoning changes.:sh – escapes to UNIX shell (use exit to return to vi). Inserting and Replacing TextBefore you are able to enter text, you have to change from the default Command Mode to Input Mode.You can set the editor in showmode (with :set showmode) to display a suitable message in the last line.The simplest type of input is insertion of text. Inserting and Replacing TextWhen vi is invoked, the cursor is always positioned at the first character of the first line.To insert text at this position, press i. The character doesn’t show up on the screen, but pressing this key changes the mode from Command to Input.Since the showmode setting was made, you’ll see the words INSERT or INSERT MODE in the last line. Inserting and Replacing TextFurther key depressions will show text on the screen as it is being entered. This is the vi editor It is quite powerful. It operates in three modes. Inserting and Replacing TextYou can use [Backspace] to correct a mistake or erase the previous word using [Ctrl-w].After you