P. 1
Unix Brief

Unix Brief

|Views: 419|Likes:
Published by api-26872269

More info:

Published by: api-26872269 on Oct 19, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PPT, PDF, TXT or read online from Scribd
See more
See less





Introduction to UNIX

What is UNIX?
   

An Operating System (OS) Mostly coded in C Machine independence It provides a number of facilities:
  

management of hardware resources directory and file system loading / execution / suspension of programs

History (Brief)

  

  

First UNIX at Bell Labs The MULTICS Kernighan, Ritchie, Thompson Bell Labs makes UNIX freeware Berkeley UNIX (BSD) Bill Joy vi editor, C Shell


 

System V release 4 TCP/IP Sun Microsystems Solaris Microsoft Xenix, SCO MIT X-Windows GNU, LINUX Stallman, Torvalds

 

 

Why Use UNIX?
     

multi-tasking / multi-user lots of software networking capability graphical (with command line) easy to program portable (PCs, mainframes, super-computers)

  

 

free! (LINUX, FreeBSD, GNU) popular profitable 1996 Sales: US$34.5 Billion, up 12% not tied to one company active community

Your Account

Each user has their own space called their account. Type your login ID and password to enter your account. Only if the login ID and password match will you be let in.

Login to your Account
login: ad Password:

You type your ID and RETURN. You type your password and RETURN. It does not appear. The UNIX prompt (or similar). You can now enter



Logout from your Account


Press CONTROL and D together


On-line Help

man gnuchess man man

Manual pages Spacebar to go on; ^C to stop

apropos topic
apropos game apropos help

Lists commands related to topic

UNIX Books

The Unix Programming Environment, Brian W. Kernighan and Rob Pike. Prentice Hall, Inc., 1984. Sumitabha Das, "Unix : Concepts and Applications" A Student’s Guide to UNIX, Harley Hahn, McGraw-Hill, 1993 A Practical Guide to the UNIX System, Mark G. Sobell, Benjamin-Cummings, 3rd Edition, 1995

Kernel-Shell Relationship
ic pl a
se r U

p r A re e th wa O ft So

Kernel Compilers
S ell h

Sh el
U r se


Int To ernet ols
e Us r


el l Sh

/ on ti

tem s Sy
ds x an ni m U m Co

U ser

S hel l

e as tab ges Da cka Pa

The Shell
 

The UNIX user interface is called the shell. The shell does 4 jobs repeatedly:
display prompt read command

the shell
process command

execute command

Typing Commands

Try these:
date cal 3 2005 who ls -a man cal clear

Changing your Password

The command is:

It will ask you for the new password twice.

Date Commands

date cal cal 1997 cal 3 cal 7 1962 cal 9 1752

Gives time and date Calendar

You and the System
 

uptime hostname whoami who

Machine’s ‘up’ time Name of the machine Your name

 


expr e
expr 3 + 5 + 7

Simple arithmetic Programmable Calculator


Some General Purpose Commands
date cal who ls man clear uptime hostname quota whoami apropos whatis which locate more passwd echo banner tty uname tput spell ispell cat sort pwd

Redirection, pipes , processes

Output can be redirected to a file with‘>‘:
ls > dir.txt cal 2004 > year2004

Output can be appended to a file with ‘>>‘
cal 2004 > years cal 2005 >> years

Pipes : sending the output of one program to the input of the other
ls | sort who | sort

Processes : Running two commands sequentially
locate mj > xxx; date locate usr > xxx &

The UNIX File System

The File

Ordinary Files Directory Files Device Files

The Parent Child Relationship

A simplified UNIX directory/file system:

etc ...

bin date . . . cal

usr1 faculty mj

dev ...

tmp ...

Some System Directories

/ /bin /etc

root directory commands system data files (e.g. /etc/passwd) files representing I/O devices



A pathname is a sequence of directory names (separated by /’s) which identifies the location of a directory. There are two sorts of pathnames
 

absolute pathnames relative pathname

Absolute Pathnames

The sequence of directory names between the top of the tree (the root) and the directory of interest. For example:
/bin /etc/terminfo /export/user/home/ad /export/user/home/s3910120/proj1

Relative Pathnames

The sequence of directory names below the directory where you are now to the directory of interest. If you are interested in the directory proj1:
proj1 s3910120 s3910120/proj1 home/s3910120/proj1

if you are in if you are in home if you are in user

Commands and Pathnames

Commands often use pathnames. For example:
/usr/games/fortune cat /etc/passwd

List the password file

Moving between Directories

s3910120’s home directory:

s3910120 hobby.c proj1 ... proj2 ...

If you are in directory s3910120 how do you move to directory proj1?
cd proj1

You are now in proj1. This is called the current working directory.


Print name of current working directory

Move back to directory s3910120 (the parent directory):
cd ..

When in proj1, move to proj2 with one command:
cd ../proj2

../proj2 is a relative pathname

Special Directory Names
 

/ . directory ..

The root directory The current working The parent directory (of your current directory)

   

cd / cd ~ cd cd ../..

Change to root directory Change to home directory (Special case; means cd ~) Go up two levels.

Investigate the System

Use cd
cat file
cd /etc cat passwd

List file Directory listing
List current dir. List /etc

ls ls /etc

Making / Deleting / Renaming Directories

Usually, you can only create directories (or delete or rename them) in your home directory or directories below it.
mkdir rmdir mv

Make a directory Delete a directory Rename a directory

   

ls –l /etc/passwd
-rw-r--r-1 root root 2365 Jul 28 16:19 /etc/passwd

read, write, execute (r w x) rw- r-- r-owner group everyone


-w, +w ….

Commands to work with files
          

cat > filename less head tail cp mv rm wc grep spell ispell

Communicating with People

Information on Others

users who ps
ps -au

Who else is logged on? Information on current users What are people doing?

w -sh

What are people doing?
A shorter report

Examine password info:
more /etc/passwd grep s38 /etc/passwd

Fingering People

finger -l

Info. on current users
Longer information

finger user
finger ad

Information on user (need not be logged in)

finger @machine-name

User info. for that

finger @catsix finger @ratree.psu.ac.th

ping machine-name
ping catsix

Is machine alive (on)?
(^C to stop)

Your Finger Information


Change your finger entry

finger also prints the contents of the .plan and .project files in your home

directory. List ‘.’ files with:
ls -a


talk user

Talk to user (on any machine)

talk ad talk bill-gates@ratree.psu.ac.th

Get out by typing ^C

write user user
write ad

Send a message to on this machine

mesg n mesg y

Switch off talk / write acceptance. Switch on

Sending E-mail

Send mail :
mail Add

Subject: Shoe Problem What colour are my shoes? I cannot see them at the moment because of my desk. - Jim ^D

The vi Editor

Two modes
 

Insert i Command <ESC>

   

Append a Replace character r, Replace word R ….. Deleting character x, Deleting line dd Exit
Goto command mode press :wq ……

The Shell

Exploring the Shell

Making a new command
Create a directory called ‘bin’ inside your directory mkdir bin  Create a file containing a set of commands cat > nuls echo ‘Welcome to your directory’ ls –l echo ‘Thank you’ ^D  Make nuls executable Chmod +x nuls

The Convention filename.sh

Exploring the Shell

Command arguments and parameters
   

Argument 1 to 9 $1, ….,$9 All the arguments $* $0 – The name of the shell script $# - The Number of arguments Set a variable
Variable name = value $ x = Good Day $ echo $x $ set

Shell Variables

Exploring the Shell

Making the script interactive : read
echo ‘Enter file name’ read fname echo $fname

Exit status of a command

$? - exit status
0 command succeeds  Any other non zero value command fails

Exploring the Shell

Conditional Execution.
Delimits two commands  && : The second command is executed only when the first command is executed successfully  || : The second command is executed only when the first command fails.

Script Termination


Exploring the Shell

Conditional statements
if (conditional is true)
then execute commands else execute commands

fi Example if grep “professor” employee.list
then echo “record found” else echo “record not found”


Exploring the Shell

Numeric comparison
test $x –eq $y if test $# -eq 0 then echo "no input argument" else echo $1 fi

Operator meaning -eq = -ne != -gt > -ge >= -lt < -le <=

Test -n stg -z stg s1 = s2 s1 != s2 stg

True if stg is not a null string stg is a null string s1 = s2 s1 != s2 stg is assigned and not null

String Comparison
if [ -z "$1" ]; then echo "no input argument" else echo $1 fi

Exploring the Shell

The case structure
case expression in pattern1) Commands ;; pattern2) Commands ;; pattern9) Commands ;; esac

Exploring the Shell

 

expr 3 + 5 expr $x + $y P1 ; P2 - does P1 then P2 P1 & P2 - does P1 then P2 but does not wait for P1 to finish.

Command Termination
 

Exploring the Shell

while (condition is true) do commands done

while true ;
do date sleep 100; done &

Looping with a list for var in list
do commands done

for file in *.c
do cc –o $file{x} $file done


The UNIX programs that read some input, perform a simple transformation on it and write some output. grep, egrep, fgrep tr, dd, sort Sed, awk – programmable filters

  


grep options pattern format filename(s)

Some option
   

-c Counting number of occurrences -n Line numbers along with lines grep Mamata –e mamata database grep [Mm]amata database

grep : Regular Expressions

Character sets

[mM] , [aeiou] , [a-zA-Z0-9] G*, [gG]* 2… A four character pattern starting with 2 .* A number of characters or none

Immediately preceeding character

Matching a single character
 

grep : RE

c \c

Specifying pattern boundaries

^ $ . […] [^…] r* r1r2

Any non-special character c matches Turn off any special meaning of character c Beginning of line End of line Any single character Any one of character in …; ranges like a-z are legal Any single character not in …; ranges are legal Zero or more occurrences of r RE r1 followed by RE r2

^r pattern beginning with expression r ^[^r] pattern not beginning with expression r r$ pattern ending with expression r

egrep : Regular Expressions
   

r+ : one or more occurrences of r r? : zero or more occurrences of r r1|r2 : r1 or r2 (r) : nested r

  

Searches for multiple patterns Does not accept regular expression Multiple patterns are separated by new line character. The disadvantage of grep family is that none of them has a separate facility to identify fields.


    

-f : eliminates distinction between uppercase and lowercase letters. -n : numeric comparison - r : largest to smallest +m : comparison skips first m fields +0 : beginning of the line -u : discard duplicates

 

File comparison command Gives three columns of the output
  

Lines that occur only in file 1 Lines that occur only in file 2 Lines that occur in both Comm –12 f1 f2

One or more columns can be suppressed


Transliteration of character in the input

tr a-z A-N

Mostly used for character conversion

1. Try all the UNIX commands. Store the output in a file appropriately using redirection operators. 2. Read a word from the terminal and check if the spelling is correct. Suggest few alternatives. 3. Create a file using Vi. Store few names in the file. Search all the names containing the letter M or m. 4. Create another file using cat command 5. Compare both the files to find the diffirences 6. Use calculator commands to compute 5 arithmetic expressions.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->