Unix Unit1
Unix Unit1
Unix Unit1
Introduction, History, Architecture, General Purpose Utilities: cat, cal, date, calendar, who, printf, tty,
sty, uname, passwd, echo, tput, bc. Introduction to Shell Scripting, Shell Scripts, read, Command Line
Arguments, Exit Status of a Command, The Logical Operators, exit, if, and case conditions, expr,
sleep and wait, while, until, for, redirection, set and shift, trap.
1. Unix Components/Architecture
2. Features of Unix
3. The UNIX Environment and UNIX Structure, Posix and Single Unix specification
4. General features of Unix commands/ command structure. Command arguments and options
5. Basic Unix commands such as echo, printf, ls, who, date,passwd, cal, Combining commands
Unix Components/Architecture
Page 1
The UNIX architecture has three important agencies-
Division of labor: Kernel and shell
The file and process
Apart from providing support to user's program, kernel also does important housekeeping.
It manages the system's memory, schedules processes, and decides their priorities and so on.
The kernel has to do a lot of this work even if no user program is running.
The kernel is also called as the operating system - a programs gateway to the computer's
resources.
The Shell
Computers don't have any capability of translating commands into action.
That requires a command interpreter, also called as the shell.
Shell is acts interface between the user and the kernel.
Most of the time, there's only one kernel running on the system, there could be
several shells running – one for each user logged in.
The shell accepts commands from user, if require rebuilds a user command,
andfinally communicates with the kernel to see that the command is executed.
Example:
$echoVTU Belagavi #Shell rebuilds echo command by removing multiple spaces
VTU Belagavi
Page 2
The following figure shows the kernel-shell Relationship:
The Process
The process is the name given to the file when it is executed as a program (Process is
program under execution).
We can say process is the “time image” of an executable file.
UNIX provides tools to control processes move them between foreground and background and
killthem.
Page 3
Features of Unix
1. A Multiuser System
UNIX is a multiprogramming system, it permits multiple programs to run and compete for the
attention of the CPU.
This can happen in two ways:
- Multiple users can run separate jobs
- A single user can also run multiplejobs
A single user system where the CPU, memory and hard disks are all dedicated to a single user.
In UNIX, the resources are shared between all users; UNIX is also a multiuser system.
2. A Multitasking System
A single user can also run multiple tasks concurrently.
UNIX is a multitasking system.
It is usual for a user to edit a file, print another one on the printer, send email to a friend and browse
www- all without leaving any of applications.
The kernel is designed to handle a user's multiple needs.
In a multitasking environment, a user sees one job running in the foreground; the rest run in the
background.
User can switch jobs between background and foreground, suspend, or even terminate them.
5. Pattern Matching
UNIX features very sophisticated pattern matching features.
Example: The * (zero or more occurrences of characters) is a special character used by system
Page 4
toindicate that it can match a number of filenames.
6. Programming Facility
The UNIX shell is also a programming language; it was designed for programmer, not for end user.
It has all the necessary ingredients, like control structures, loops and variables, that establish
powerful programming language.
These features are used to design shell scripts – programs that can also invoke UNIX commands.
Many of the system's functions can be controlled and automated by using these shellscripts.
7. Documentation
The principal on-line help facility available is the man command, which remains the most
important references for commands and their configuration files.
Apart from the man documentation, there's a vast ocean of UNIX resources available on the Internet.
A variable that specifies how an operating system or another program runs, or the devices
that the operating system recognizes.
Set of dynamic values that can affect the way running processes will behave on a computer.
Page 5
The following table lists keyboard commands to try when things go wrong.
Basic Unix commands such as echo, printf, ls, who, date,passwd, cal, Combining
commands
echo command is used is shell scripts to display a message on the terminal, or to issue a
prompt for taking user input.
Page 6
$ echo "Enter your
name:\c" Enter your
name:$_
$echo $SHELL
/usr/bin/bash
Echo can be used with different escape sequences
Constant Meaning
„a‟ Audible Alert (Bell)
„b‟ Back Space
„f‟ Form Feed
„n‟ New Line
„r‟ Carriage Return
„t‟ Horizontal Tab
„v‟ Vertical Tab
„\‟ Backslash
„\0n‟ ASCII character represented by the octal value n
The printf command is available on most modern UNIX systems, and is the one we can use
instead of echo. The command in the simplest form can be used in the same way as echo:
printf also accepts all escape sequences used by echo, but unlike echo, it doesn‟t
automatically insert newline unless the \n is used explicitly. printf also uses formatted
strings in the same way the C language function of the same name uses them:
The %s format string acts as a placeholder for the value of $SHELL, and printf replaces %s
with the value of $SHELL. %s is the standard format used for printing strings. printf uses
many of the formats used by C‟s printf function. Here are some of the commonly used ones:
%s – String
Page 7
%30s – As above but printed in a space 30 characters wide
%d – Decimal integer
%6d - As above but printed in a space 30 characters wide
%o – Octal integer
%x – Hexadecimal integer
%f – Floating point number
Example:
$ printf "The value of 255 is %o in octal and %x in hexadecimal\n" 255 255
$ who
root tty7 2017-09-04 16:38 (:0)
root tty17 2017-09-04 16:38 (:0)
$_
One can display the current date with the date command, which shows the date and time to
the nearest second:
$ date
Mon Sep 4 16:40:02 IST 2017
The command can also be used with suitable format specifiers as arguments. Each symbol is
preceded by the + symbol, followed by the % operator, and a single character describing the
format. For instance, you can print only the month using the format +%m:
Page 9
$date +%m
09
Or
the month name name:
$ date +%h
Aug
Or
You can combine them in one command:
$ date + “%h %m”
Aug 08
There are many other format specifiers, and the useful ones are listed below:
- d – The day of month (1 - 31)
- y – The last two digits of the year.
- H, M and S – The hour, minute and second, respectively.
- D – The date in the format mm/dd/yy
- T – The time in the format hh:mm:ss
6. Passwd: Changing your password
cal command can be used to see the calendar of any specific month or a complete year.
Syntax:
cal [ [ month] year ]
Everything within the rectangular box in optional. So cal can be used without any arguments, in
which case it displays the calendar of the current month
$ cal
September 2017
Su Mo Tu We Th Fr Sa
12
345 6 789
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
The syntax show that cal can be used with arguments, the month is optional but year is not.
To see the calendar of month August 2017, we need to use two arguments as shownbelow,
$ cal 8 2017
August 2017
Su Mo Tu We Th Fr Sa
1 2 345
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
You can‟t hold the calendar of a year in a single screen page; it scrolls off rapidly before you
can use [ctrl-s] to make it pause. To make cal pause using pager using the | symbol to connect
them.
$cal 2017 | more
8. Combining commands
UNIX allows you to specify more than one command in the single command line.
Example:
$ wc note; ls -l note #Two commands combined here using ; (semicolon)
2 3 16 note
-rw-rw-r-- 1 mahesh mahesh 16 Jan 30 09:35 note
Page 11
In the above example, wc command returns number of lines, words and characters in that file, and
ls –l command returns list all the attributes of the file
A command line can overflow or be split into multiple lines
UNIX terminal width is restricted to maximum 80 characters.
Shell allows command line to overflow or be split into multiple lines.
Example:
$ echo “This is # $ first prompt
> a three-line # > Second prompt
> text message” #Command line ends here
This is
a three-line text
message
When the shell execute command(file) from its own set of built-in commands that are not
stored as separate files in /bin directory, it is called internal command.
They can be executed any time and are independent.
If the command (file) has an independence existence in the /bin directory, it is called
external command.
External commands are loaded when the user requests for them. It will have an individual
process.
Examples:
$ type echo # echo is an internal command
echo is shell built-in
$ type ls # ls is an external command
ls is /bin/ls
If the command exists both as an internal and external one, shell execute internalcommand
only.
Internal commands will have top priority compare to external command of samename.
The type command is used to describe how its argument would be translated if used as commands. It
is also used to find out whether it is built-in or external binary file.
Page 12
The type command is shell builtin.
Syntax:
type [Options] command names
If you want to know the location of executable program (or command), use type
command-
Example:
$ type date
date is /bin/date
$ type echo
echo is a shell builtin
When you execute date command, the shell locates this file in the /bin directory
and makesarrangements to execute it.
SHELL PROGRAMMING
VARIABLE NAMES:
A variable is a character string to which we assign a value. The value assigned could be a
number, text, filename, device, or any other type of data.
A variable is nothing more than a pointer to the actual data. The shell enables you to create,
assign, and delete variables.
The name of a variable can contain only letters(a to z or A to Z),numbers(0 to 9) or the
underscore character( _ ).
By convention, Unix shell variables would have their names in UPPERCASE
The following examples are valid variable names:-
VAR_1
VAR_2
TOKEN_A
DEFINING VARIABLE:
Variable are defiened as follows:
Variable_name= variable_value
For example
NAME=”Sumithabha Das”
ACCESSING VARIABLES:
To access the value stored in a variable, prefix its name with the dollar sign($).
For example following script would access the value of defined variable NAME and would
print it on STDOUT
#!/bin/sh
NAME=”Sumitabha Das”
Echo $NAME
ENVIRONMENT VARIABLES:
An environment variables that is available to any child process of the shell. Some programs need
environment variables in order to function correctly. Usually a shell script defines only those
environment variables that are needed by the programs that it runs
SHELL: points to the shell defined as default.
DISPLAY: contains the identifier for the display that X11 programs should use by default.
HOME: Indicates the home directory of the current user default argument for the cd built in
command
IFS: Indicates the Internal Field Separator that is used by the parser for word splitting after
expansion.
PATH: Indicates search path for commands .It is a colon separated list of directories in
which shell looks the command.
PWD: Indicates the current working directory as set by the cd command.
RANDOM: Generates a random interger between 0 and 32767 each time it s referenced.
SHLVL: Increments by one each time an instance of bash is created.
UID: Expands to the numeric user ID of the current user initialized at shell prompt.
Following is the sample example showing few environment variables
$ echo $HOME
/root
]$ echo $DISPLAY
$ echo $TERM
xterm
$ ech $PATH
/usr/local/bin:/bin:/usr/bin:/home/amrood/bin:/usr/local/bin
$