1
Basic Unix And Basic Shell Scripting
2
Course Objectives
The participant will learn
Unix Introduction
Unix file related commands
Unix text manipulation commands
Environment variables
The vi-editor
Shell programming
Awk programming
3
Introduction to Unix
Features of Unix System:
1. Simple design, organization and functioning
2. Portability
3. Background processing
4. Hierarchical File System
5. Multi-user
6. Multi-tasking
7. Security
8. Interactive Operating System
9. Communication facilities
10. Utilities
4
Introduction to Unix
Utility Software:
1. Text Manipulation tools: cut, grep, tr, etc.
2. Advanced filters: sed, awk, etc.
3. Document Formatting tools: troff, nroff, etc.
4. Various Programming Languages: C, C++, Java,
etc.
5. Interactive Calculators : bc, dc, etc.
6. Advanced tools : lex, yacc.
5
Unix File System
The Unix file system is the structure in which all the
information on your computer is stored.
It has hierarchical file structure, where each directory
can contain files, as well as other directories.
It looks like an upside down tree.
Unix looks at everything as a file.
At the top is the root directory, represented by a
forward slash (/).
At the children node of it is a set of common
directories in the Linux system, such as /bin, /dev,
/home, etc.
6
Unix File System
The diagram shows the Linux file structure
/
bin/ home/ etc/ dev/ …
itp1/ itp2/ …
test1.sh test2.sh …
7
Unix File System
In Windows file systems, drive letters represent
different storage devices like A: is a floppy drive, C:
is a hard disk, etc. In Linux, all storage devices are
in the same file system hierarchy.
Windows file systems uses backslash (\) to separate
directory names, whereas Linux uses forward slash
(/) for the same.
Every file and directory in a Linux system has
permissions and ownership associated with it.
File names have suffixes in Windows, whereas in
Linux you can use them as conventions.
8
Unix File System
The chunk of the disk is divided into blocks (1024-
bytes block, or 4096-bytes block, etc.) in multiple of
512 bytes in size.
These blocks are organized into four groupings- boot
block, superblock, inode blocks and data blocks.
FS1
FS2
FS3
Boot
block
Super
block
inode list Data blocks …
9
UNIX Users
Users
1. Super user
2. Owner
3. Group
4. Others
10
Introduction to Unix
Logging on and Logging off.
Step 1: telnet <System Address>
Step 2: login : ravindra
Step 3: password : ******
Login incorrect
Step 4: login : ravindra
Step 5: password : ******
Step 6: $ Command prompt to execute commands
Step 7: $ logout
11
Unix Shells
The Bourne shell /bin/sh (written by S. R. Bourne).
Along came the people from UCB and the C-shell
/bin/csh was born. Into this shell they put several
concepts which were new, (the majority of these
being job control and aliasing) and managed to
produce a shell that was much better for interactive
use.
Eventually David Korn from AT&T had the bright idea
to sort out this mess and the Korn shell /bin/ksh
made its appearance. The Korn shell became part of
System V but had one major problem; unlike the rest
of the UNIX shells it wasn't free, you had to pay
AT&T for it.
12
Unix Shells
Also at about this time the GNU project was
underway and they decided that they needed a free
shell, they also decided that they wanted to make
this new shell POSIX compatible, thus bash (the
Bourne again shell) was born.
Like the Korn shell bash was based upon the Bourne
shells language and like the Korn shell, it also
pinched features from the C shell and other
operating systems.
13
Unix Shells
The Bourne Shell
The Bourne shell is the original UNIX shell program.
It is very widely used. You can start the Bourne shell
—if it hasn't been set as your default startup shell—
by typing "sh" or "/bin/sh" at the command prompt.
This will not spawn a new shell window, but rather
will just change your current shell to the Bourne
shell.
The Bourne shell supports conditional branching in
the form of if/then/else statements. In addition, the
Bourne shell supports case statements and loops
(for, while, and until).
The Bourne shell uses the $ as a prompt.
14
Unix Shells
The Korn shell
The Korn shell is a much newer variation of the
Bourne shell. It supports everything the Bourne shell
does, and adds features not available in the Bourne
shell. The Korn shell is not a standard offering in
UNIX installations. If you have the Korn shell, you
can run it by typing ksh or /bin/ksh at the shell
prompt. A public-domain version of the Korn shell,
called pdksh.
The Korn shell was originally written by David Korn
and is copyrighted by AT&T.
The programming structure of the Korn shell is very
similar to that of the Bourne shell. The Korn shell,
however, is more interactive.
15
Unix Shells
The C shell
The C shell is a very commonly used shell. Its
programming structure closely resembles that of the
programming language "C."
The C shell uses the "%" as a prompt.
The C shell supports all of the features that the
Bourne shell supports, and has a more natural syntax
for programming.
The C shell is more interactive than the Bourne shell,
with additional features that aren't available in older
shells.
The configuration of the C shell is controlled by the
.rc and the .login files.
16
Unix Shells
The tc shell
The tc shell is a more modern variation of the C
shell.
It reads the same configuration files that the C shell
uses.
Tcsh contains command line editing keystrokes that
the C shell is missing, and has more "modern"
conveniences that the C shell lacks.
17
Unix Shells
The Bourne-Again shell
The Bourne-Again shell is a variation of the Bourne
shell.
It is commonly used in Linux, but is widely available
in other standard UNIX distributions.
The Bourne Again shell is another modification of the
Bourne shell, and uses the $ as a prompt.
To start the Bourne Again shell, type "bash" at the
shell prompt.
The behavior and environment of the Bourne Again
shell is controlled by the .bashrc file, which you'll find
as a hidden file in your home directory.
18
Unix Shells
Unix Shell application comparison table
Application sh csh ksh bash tcsh
Job control N Y Y Y Y
Aliases N Y Y Y Y
Input/Output redirection Y N Y Y N
Command history N Y Y Y Y
Command line editing N N Y Y Y
Vi Command line editing N N Y Y Y
Underlying Syntax sh csh ksh sh csh
19
Basic commands in Unix
$ who
The command shows who is logged on
$ who am I
The command shows who are you
$ who \
> am \
> i \
We can type a command over two or more lines. A
backslash character before the end of the line
followed by a new line is taken to be continuation of
the line.
20
Commands In Unix
$ man
The command formats and displays on-line
manual pages.
The manual pages are divided into logical
grouping of the commands called the sections.
Sections are numbered from 1 through 9. For
example, commands are 1, system calls are 2,
library function are 3, file formats are 5, &
management commands are 8.
If section is specified, man only looks in that
section of the manual.
The command man 2 open will display the
manual for the system call open.
21
Commands In Unix
Option Description
–k searches a set of database files containing
short descriptions of system commands
for keywords and displays the result on
the standard output.
-f gives one line introduction to the
command (only complete word matches
are displayed).
22
Basic commands in Unix
$ date
The command prints or sets the system date and
time
$ date –r TestFile
It displays the last modification time of the file
“TestFile”
$ date string
The super user can invoke the date command
with a numeric argument to set the system date.
This argument is usually an eight-character string
of the form MMDDhhmm (month, day, hour in 24-
hour format, minutes), and optionally followed by
a two-digit year string.
23
Basic commands in Unix
The command can be used with suitable format
specifiers as arguments. Each format is preceded by
a + symbol, followed by the % operator, and a
single character describing the format.
Sequence Interpretation
+%a abbreviated weekday name (Mon Sun)
+%b abbreviated month name (Jan Dec)
+%d day of month (01 31)
+%r time (12- hour)
+%T time (24- hour)
+%y last two digits of year (00 99)
+%D date (mm/dd/yy)
24
Commands In Unix
$ ls
The command lists contents of directories
Option Description
-l list in long format
-C multicolumn output
-F indicates type of file by /, *
-R recursive listing of all subdirectories
encountered
-a list all files including hidden files
25
Commands In Unix
$ cat
The command concatenates files and prints on the
standard output
$ cat
file1 file2
…
It displays contents of all files specified on the
command line one below the other
$ cat >
file
It creates a new file by accepting text from the
standard input
Press CTRL-d to save and exit the file