You are on page 1of 10

Operating Systems and Networking Lab

What is UNIX?
UNIX is an operating system which was first developed in the 1960s, and has been under
constant development ever since It is a stable, multi!user, multi!tas"ing system for servers,
des"tops and laptops
The UNIX operating system
#he UNIX operating system is made up of three parts$ the "ernel, the shell and the programs
The shell
#he shell acts as an interface between the user and the "ernel %hen a user logs in, the
login program chec"s the username and password, and then starts another program called the
shell #he shell is a command line interpreter &'(I) It interprets the commands the user types in
and arranges for them to be carried out #he commands are themselves programs* when they
terminate, the shell gives the user another prompt &+ on our systems)
#he shell is UNIX,s command interpreter It waits patiently for you to type a command,
then figures out what you want to do It either performs the action itself, or more li"ely will find
and launch whatever you as"ed for #he shell ta"es care of finding your commands in the proper
directory, maintaining your custom environment, interpreting command!line wildcards, and
maintaining a history of your commands for editing and recall
-ifferent types of shells . /orn 0hell &"sh), ' shell &csh), 1ourne 0hell &bash) etc
2ow do I "now what shell I am using 3 #ype echo 40 at the prompt
The kernel
#he "ernel of UNIX is the hub of the operating system* it allocates time and memory to
programs and handles the filestore and communications in response to system calls
5s an illustration of the way that the shell and the "ernel wor" together, suppose a user
types rm myfile &which has the effect of removing the file myfile) #he shell searches the
filestore for the file containing the program rm, and then re6uests the "ernel, through system
calls, to e7ecute the program rm on myfile %hen the process rm myfile has finished running,
the shell then returns the UNIX prompt + to the user, indicating that it is waiting for further
commands
Files and proesses 8verything in UNIX is either a file or a process
The !iretory Str"t"re
5ll the files are grouped together in the directory structure #he file!system is arranged in
a hierarchical structure, li"e an inverted tree #he top of the hierarchy is traditionally called root
&written as a slash 9 )
# $root% !! #he 9 notes the :root: of the filesystem, where the entire system is contained
Unli"e ;icrosoft %indows, where each drive has its own root directory named by a
letter, such as '*< or =*<, Uni7 holds the entire system in this single top!level directory,
including each device and document #hus, it is commonly said that in Uni7,
:everything is a file: Note that Uni7 uses the forward slash & 9 ) rather than the
bac"slash & < ) commonly used in %indows
#bin !! 0tands for :binaries:$ 'ontains some fundamental utilities needed by a system
administrator
#et !! 'ontains configuration files and some system databases
#de& !! short for devices 'ontains file representations of every peripheral device
attached to the system
#home !! contains the home directories for the users
#mnt !! #his is the default location to mount e7ternal devices li"e hard dis" drives, pen
drives etc
#lib !! #his is the depository of all integral UNIX system libraries
#root !! the home directory for the superuser root
UNIX 'ommands
1. ls !! to list files in a directory
2. mkdir ! ma"e directory
3. d . change directory 'd will ta"e to the directory one level up in the hierarchy
4. (wd . present wor"ing directory . to find out where you are in the tree structure
5. 'p . copy $ cp file1 file>
6. )& &move) . rename files $ mv file1 file>
7. *m . remove files
8. *mdir . remove directories
9. 'lear ! 1efore you start the ne7t section, you may li"e to clear the terminal window of
the previous commands so the output of the following commands can be clearly
understood
10.'at ! #he command cat can be used to display the contents of a file on the screen cat
sciencet7t
11.)ore !! -isplays te7t one screen at a time
'at sciencet7t ? more
12.+rep !! It searches files for specified words or patterns$
grep science sciencet7t
13.W . word count . to find word count of a file
wc !w sciencet7t & word count)
wc !l sciencet7t &line count)
14.'hmod !! #o set9modify a file,s permissions
If we type at the prompt, ls .l &l stands for long9detailed file listing), we see the following
output*
,ess rights on files-
r &or !), indicates read permission &or otherwise), that is, the presence or absence of
permission to read and copy the file
w &or !), indicates write permission &or otherwise), that is, the permission &or otherwise)
to change a file
7 &or !), indicates e7ecution permission &or otherwise), that is, the permission to e7ecute a
file, where appropriate
,ess rights on diretories-
r allows users to list files in the directory$
w means that users may delete files from the directory or move files into it$
7 means the right to access files in the directory #his implies that you may read files in
the directory provided you have read permission on the individual files
8g of chmod usage *
chmod go!rw7 biglist !!!!!!!! to remove read write and e7ecute permissions on the file
biglist for the group and others
chmod a@rw biglist !!!!!!!!!!! #o give read and write permissions on the file biglist to all
15.ps ! #o see information about your processes, with their associated AI- and status #o
bac"ground a process, type an . at the end of the command line
16./ill ! to "ill a process $ 8g* "ill >00BB where >00BB is the process id If a process
refuses to be "illed, uses the 01 option, ie type, "ill !9 >00BB C' is used to "ill the Dob
running in the foreground
17.Who . to see list of users logged in
18.)an . to get help
Shell (rogramming
0hell program is series of (inu7 commands 0hell script is Dust li"e batch file is ;0!-E0 but
have more power than the ;0!-E0 batch file 0hell script can ta"e input from user, file and
output them on screen Useful to create our own commands that can save our lots of time and to
automate some tas" of day today life
Writing yo"r first sript
#o successfully write a shell script, you have to do three things*
1 %rite a script
> Five the shell permission to e7ecute it
G Aut it somewhere the shell can find it
Writing a script
5 shell script is a file that contains 50'II te7t #o create a shell script, you use a text editor,
like vi, emacs,gedit etc.
#ype vi firstsh and put the following code into it
HI9bin9bash
echo Alease, enter your name
read N5;8
echo :2i 4N5;8I:
Essential Vi Commands
Epen a file*
vi filename
#o go into edit mode*
press 80' and type I
#o go into command mode*
press 80'
#o save a file
press 80' and type *w fileName
#o save a file and 6uit*
press 80' and type *w6
EJ
press 80' and type *7
#o Dump to a line*
press 80' and type the line number
#o 0earch for a string*
Aress 80' and type 9word#o0earch
#o 6uit vi*
Aress 80' and type *6
Setting permissions
'hmod u@rw7 firstsh
Putting it in your path
%hen you type in the name of a command, the system does not search the entire computer to
find where the program is located the shell maintains a list of directories where e7ecutable files
&programs) are "ept, and Dust searches the directories in that list If it does not find the program
after searching each directory in the list, it will issue the famous command not found error
message
his list of directories is called your path Kou can view the list of directories with the following
command*
echo 4A5#2
Kou can add directories to your path with the following command, here directory is the name of
the directory you want to add*
e7port A5#2L4A5#2*directory
5 better way would be to edit your bashMprofile file to include the above command #hat way, it
would be done automatically every time you log in
23e"ting the sript4
9firstsh or sh firstsh
Shell 5ariables
#here are two types of variable*
1. 0ystem Nariables
2. User -efined Nariables
System Variables
'reated and maintained by (inu7 bash shell itself #his type of variable s defined in
'5AI#5( (8##8J0 Kou can configure aspects of the shell by modifying system
variables such as A01, A5#2, (5NF,2I0#0IO8,and -I0A(5K etc
'ommonly Used Shell 5ariables
#he following variables are set by the shell*
System 5ariable )eaning
To 5iew 5ariable
5al"e Type
1502MN8J0IEN 2olds the version of this instance of bash echo 41502MN8J0IEN
2E0#N5;8 #he name of the your computer echo 42E0#N5;8
'-A5#2 #he search path for the cd command echo 4'-A5#2
2I0#=I(8
#he name of the file in which command history is
saved
echo 42I0#=I(8
2I0#=I(8
#he name of the file in which command history is
saved
echo 42I0#=I(8
2I0#=I(80IO8
#he ma7imum number of lines contained in the
history file
echo 42I0#=I(80IO8
2I0#0IO8
#he number of commands to remember in the
command history #he default value is P00
echo 42I0#0IO8
2E;8 #he home directory of the current user echo 42E;8
I=0
#he Internal =ield 0eparator that is used for word
splitting after e7pansion and to split lines into words
with the read builtin command #he default value is
QspaceRQtabRQnewlineR
echo 4I=0
(5NF
Used to determine the locale category for any
category not specifically selected with a variable
starting with ('M
echo 4(5NF
A5#2
#he search path for commands It is a colon!
separated list of directories in which the shell loo"s
for commands
echo 4A5#2
A01 Kour prompt settings echo 4A01
#;EU#
#he default timeout for the read builtin command
5lsom in an interactive shell, the value is interpreted
as the number of seconds to wait for input after
issuing the command If not input provided it will
logout user
echo 4#;EU#
#8J; Kour login terminal type
echo 4#8J;
e7port #8J;Lvt100
028(( 0et path to login shell echo 4028((
-I0A(5K 0et X display name
echo 4-I0A(5K
e7port -I0A(5KL*01
8-I#EJ 0et name of default te7t editor
e7port
8-I#EJL9usr9bin9vim
6ow !o I !isplay The 5al"e Of a 5ariable?
Use echo command to display variable value #o display the program search path, type*
eho :4A5#2:
#o display your prompt setting, type*
eho :4A01:
User Defined Variables
'reated and maintained by user #his type of variable defined may use any valid variable name,
but it is good practice to avoid all uppercase names as many are used by the shell
'reating and setting variables within a script is fairly simple Use the following synta7*
varNameLsomeNalue
Kou can display the value of a variable with eho 7&arName or eho 78&arName9*
eho :4varName:
23ample shell sripts
2g: Write Sript to see "rrent date; time; "sername; and "rrent diretory
H8g1
echo :2ello, 4(EFN5;8:
echo :'urrent date is SdateS:
echo :User is Swho i amS:
echo :'urrent direcotry SpwdS:
2g<4 Write sript alled say6ello; p"t this sript into yo"r start"p file alled -bash=profile;
the sript sho"ld r"n as soon as yo" logon to system; and it print any one of the following
message4
Food ;orning
Food 5fternoon
Food 8vening , according to system time
H 8g>
temphLSdate ? cut !c1>!1GS
datLSdate @:+5 +d in +1 of +K &+r):S
if T 4temph !lt 1> U
then
echo :Food ;orning 4(EFN5;8, 2ave nice dayI:
fi
if T 4temph !gt 1> !a 4temph !le 16 U
then
echo :Food 5fternoon 4(EFN5;8:
fi
if T 4temph !gt 16 !a 4temph !le 1V U
then
echo :Food 8vening 4(EFN5;8:
fi

You might also like