You are on page 1of 56

CS2750 Advanced Programming with UNIX

Lecture 1

Introduction

CS2750 Advanced Programming with UNIX

Basics


Computer
Collection of components that can perform arithmetic and logical calculations at incredible speed

Hardware
Actual circuits and associated machinery that is visible as a computer CPU, disk, tape, keyboard, monitor, etc.

Software
Sequence of instructions/commands to make the computer perform a desired task

CS2750 Advanced Programming with UNIX

Computer Systems


Organization

CS2750 Advanced Programming with UNIX

Hardware


CPU (Central Processing Unit)


Brain of a computer Reads machine code from memory and executes the code ALU (Arithmetic and Logic Unit)  Fundamental part of CPU  Does the actual calculations

Memory
RAM (Random-Access Memory)  Holds the machine code and data ( forgets later ) ROM (Read-Only Memory)  Contents are remembered and may not be changed
4

CS2750 Advanced Programming with UNIX

Hardware


Disk
Secondary memory (also Tapes, Floppy disks, ...) Holds large amounts of data and code Remembered after computer is turned off

I/O Devices
Input devices  Monitor, Keyboard, Mouse, Scanner, etc. Output devices  Monitor, Printer, Speaker, etc.

CS2750 Advanced Programming with UNIX

Software


File
A collection of instruction/data stored on disk

Program
A collection of bytes representing code and data that are stored in a file Most of the programs we use (or write) are application programs

Process
When a program starts, it is loaded from disk to RAM Once it is loaded into memory and is executed, it is called process
6

CS2750 Advanced Programming with UNIX

Operating System


Special software
An interface between hardware and application program Coordinates most of the computer system components Manages processes and resources Without it, the computer cannot function properly

Examples
Windows XP, UNIX, VMS, Mac OS X, ...

CS2750 Advanced Programming with UNIX

UNIX
 

Standard operating system of computing world Multi-user, Multitasking OS


CPUs, Memory, Disk space are all shared among Processes

Written in C language
Portable

CS2750 Advanced Programming with UNIX

UNIX


Developed at AT&T Bell labs (1970)


by Ken Thompson Initially written in assembly language Later rewritten by Thompson and Dennis Ritchie in C language

UNIX BSD (Berkeley Software Distribution)


by grad students (including Bill Joy who developed vi, csh) memory management system + networking capability

CS2750 Advanced Programming with UNIX

UNIX


Different groups later developed their own versions


UNIX System III (1982, AT&T) UNIX System V (1983, AT&T) UNIX System V Release 4 (SVR4) (1989, AT&T) Linux (Linus Torvalds, 1991) GNU (Free Software Foundation, 1991) Solaris (BSD + System V) (Sun Microsystems) IRIX (Silicon Graphics) Mac OS X

10

CS2750 Advanced Programming with UNIX

Logging in/out


Logging in
Type in your username and password The system sets up a working environment according to some hidden files (e.g., .cshrc, .login, .profile) Unix is case sensitive, so be careful with the case you type Connecting from other machines  Use telnet (or rlogin, ssh)  e.g., telnet admiral.umsl.edu  e.g., rlogin admiral.umsl.edu l apujols

11

CS2750 Advanced Programming with UNIX

Logging in/out


Logging out
Use logout or exit command ^D (Ctrl-D) also works  It terminates current shell  Is also means end of input or end of file $ cat > alice.txt And I fall into them, Like Alice fell into Wonderland. ^D $
12

CS2750 Advanced Programming with UNIX

Shell


What is shell?
An interface program between user and UNIX Provides environment to work in  Interprets commands and executes them Shows a prompt (%, $, ...) to receive commands $ (type your command here) % (type your command here)

13

CS2750 Advanced Programming with UNIX

Shell


Popular shells

Shell name Bourne shell C shell Korn shell tcsh bash

Author Stephen Bourne Bill Joy David Korn Ken Greer Brian Fox

Command sh csh ksh/dtksh tcsh bash

Prompt $ % $ % $

14

CS2750 Advanced Programming with UNIX

Shell


Checking your shell


Checking your current shell % echo $0 tcsh Checking your login shell % echo $SHELL /bin/csh

15

CS2750 Advanced Programming with UNIX

Utilities


Form
% utility [option(s)] <parameter(s)>

Mostly in lowercase letters Options are prefixed by a dash (-r, -f, ...)

16

CS2750 Advanced Programming with UNIX

Utilities


Examples
date utility (displays the current date and time) % date Thu Jan %

4 22:01:44 CST 2007

clear utility (clears your screen) % clear

17

CS2750 Advanced Programming with UNIX

man (manual page)




Provides on-line help on a command


% man pwd User Commands NAME pwd - return working directory name SYNOPSIS /usr/bin/pwd DESCRIPTION

pwd(1)

...
18

CS2750 Advanced Programming with UNIX

File system


Hierarchical directory structure


Each directory contains one or more files File attributes  file type, permission, hard link count, owner, group ID, file size, date modified, file name

% ls -al drwxr-xr-x drwx------rwxr-xr-x -rw-r--r-2 4 1 1 apujols apujols apujols apujols 100 100 100 100 512 512 8224 454 Jan Jan Jan Jan 4 2 4 4 22:29 17:59 21:29 21:30 ./ ../ a.out* cube.c

19

CS2750 Advanced Programming with UNIX

Pathname


Absolute pathname

/usr/local/bin/gcc /accounts/classes/apujols/myFile.c /usr/dt/bin/dtksh /accounts/students/ccarpenter

20

CS2750 Advanced Programming with UNIX

Pathname


Relative pathname
./myFile.c ../myFile.c ../../bin/myFile.c HW1/myFile.c     . indicates current directory .. indicates parent directory ~ indicates your home directory ~apujols indicates home directory of user apujols

21

CS2750 Advanced Programming with UNIX

I/O channels


Standard channels
stdin  Standard input stdout  Standard output stderr  Standard error (where program writes error messages)

22

CS2750 Advanced Programming with UNIX

I/O channels


Standard channels
By default, all of these channels are the terminal  can be redirected by using > or < symbols % myprogram < input.data % myprogram > output.data

23

CS2750 Advanced Programming with UNIX

Basic utilities


Exploring file system


pwd  Displays the current working directory ls  Lists the directory contents % % % % ls ls ls ls l a -R F ..shows ..shows ..shows ..shows contents in long format even hidden files recursively with symbols like *,/
24

CS2750 Advanced Programming with UNIX

Basic utilities


Exploring file system


cd directory  Changes the current working directory % % % % cd cd cd cd . ..has no effect .. ..changes to parent directory ~ ..changes to home directory ../../tmp

25

CS2750 Advanced Programming with UNIX

Basic utilities


Directory management
mkdir directory  Creates a new directory rmdir directory1 directory2 ...  Removes directories

26

CS2750 Advanced Programming with UNIX

Basic utilities


Copying files
% cp file1 file2  Copies file1 to file2 % cp -i file1 file2  Copies file1 to file2; Prompts for confirmation % cp file1 dir1  Copies file1 to dir1 % cp -r dir1 dir2  Copies dir1 to dir2, recursively
27

CS2750 Advanced Programming with UNIX

Basic utilities


Moving/Renaming files
% mv file1 file2  Rename file1 to file2 % mv -i file1 file2  Prompts for confirmation % mv file1 dir1  Moves file1 to dir1 % mv dir1 dir2  Moves dir1 to dir2
28

CS2750 Advanced Programming with UNIX

Basic utilities


Removing files
% rm file1  Remove file1 % rm -i file1  Prompts for confirmation % rm -r dir1  Remove all contents of dir1, recursively % rm -f file1  Skip error messages and prompts
29

CS2750 Advanced Programming with UNIX

Basic utilities


Displaying file contents


cat filename  Shows the contents of the file more filename  Shows the contents of the file, one page at a time head/tail -n filename  Shows the first/last n lines

30

CS2750 Advanced Programming with UNIX

Basic utilities


Printing files
lpr filename  Sends the file to a printer lpq  Shows the printer queue lprm  Removes a printing job

31

CS2750 Advanced Programming with UNIX

Basic utilities


Counting words in a file


wc % % % % wc wc wc wc w test.c ..obtain a word count l test.c ..obtain a line count c test.c ..obtain a character count test.c ..counts all three

32

CS2750 Advanced Programming with UNIX

Basic utilities


Checking users/processes
who (shows the list of users currently logged in) whoami (shows your login name) finger name ... (shows information of a user) ps (shows the list of processes) kill [-9] process1 process2 ...  Kills processes  -9 option makes it mandatory

33

CS2750 Advanced Programming with UNIX

File type


Example
-rw-r--r-- 1 ccarpenter cs 867 Nov 18 20:13 test.c

File types (indicated by the starting character)  - (regular file)  d (directory file)  l (symbolic link) % file test.c (lets you know the type of file) test.c: c program text

34

CS2750 Advanced Programming with UNIX

File permissions


Example
-rwxr-xr-- 1 ccarpenter cs 867 Nov 18 20:13 test.c

User (owner) rwx

Group r-x

Others r--

What the characters mean r Read permission w Write permission x Execute permission
35

CS2750 Advanced Programming with UNIX

Changing file permission




chmod
chmod change filename u user/owner + add permission g group remove permission o others = assign permission absolutely x execute
36

a all

r read

w write

CS2750 Advanced Programming with UNIX

Changing file permission




chmod
% % % % % % chmod u+x myfile (allow my execution) chmod go-r myfile (disallow others reading) chmod g+w myfile (allow groups writing) chmod u-rw myfile (disallow my read/write) chmod a+x myfile (allow everyones execution) chmod g=r myfile (give group read-only permission) % chmod u+w,g-r myfile (allow my writing and disallow groups reading)

37

CS2750 Advanced Programming with UNIX

Changing file permission




chmod
Octal numbers can be used for permission specification % chmod 700 myfile (disallow others access) % chmod 750 myfile (rwxr-x---)

user setting binary octal rwx 111 7

group r-x 101 5

others --000 0

38

CS2750 Advanced Programming with UNIX

File name substitution (wildcards)




Makes it easier to specify files


e.g.,  ls l *.c (shows all C program files)  rm HW1/* (remove all the files in HW1) Symbols  *   ? matches any string

 matches any single character string  [..]  matches any one of the characters in the bracket
39

CS2750 Advanced Programming with UNIX

File name substitution (wildcards)




Example
$ ls a.c bb.c cc.d d.e $ ls *.c a.c bb.c $ ls ?.c a.c $ ls [ac]* a.c cc.d $ ls [A-Za-z]* a.c bb.c cc.d d.e $ ls ?.? a.c d.e
40

CS2750 Advanced Programming with UNIX

Pipes


One process standard output is used as another process standard input


$ command1 | command2 e.g., $ ls a.c b.c cc.c dir1 dir2 $ ls | wc w 5 (counts the number of words) $ ls al | more (shows one page at a time)
41

CS2750 Advanced Programming with UNIX

Special characters


Terminal metacharacters
Interpreted by your UNIX terminal

% stty -a (displays all metacharacters) % stty erase ^b % stty erase ^h (set erase key to control-B) (set erase key to control-H)

42

CS2750 Advanced Programming with UNIX

Special characters


^C (Ctrl-C)
Terminates (interrupts) the current process % man pwd User Commands NAME pwd - return working directory name... ^C %

pwd(1)

43

CS2750 Advanced Programming with UNIX

Special characters


^D (Ctrl-D)
Terminates current shell, or indicates EOF (end-of-file)

% cat > myFile Hello world. My name is Albert. ^D %

44

CS2750 Advanced Programming with UNIX

VI editor
 

Standard text editor in UNIX Starting vi


without a file parameter (opens a blank text file) % vi with a file parameter (opens the specified file) % vi myFile.c % vi poem.txt

45

CS2750 Advanced Programming with UNIX

VI editor


2 Modes
Text Entry mode (Insert mode) Command mode

Text Entry mode (Insert mode)


User can enter text here Press i (or one of the following keys) to enter this mode  a: After the cursor  I: Start of current line  A: End of current line  o: After current line  O: Before current line  R: Replacing text from the cursor
46

CS2750 Advanced Programming with UNIX

VI editor


Command mode
User can edit text here Press ESC (escape key) to enter this mode Supported operations  Moving the cursor  Deleting text  Copying-and-Pasting text  Replacing text  Searching through text  Searching and replacing text  Saving or loading files  Miscellaneous
47

CS2750 Advanced Programming with UNIX

VI editor


Cursor movement
Movement Up one line Down one line Right one character Left one character To start of line To end of line Down half screen Up half screen To line nn To end of file Key sequence <cursor up> or k <cursor down> or j <cursor right> or l <cursor left> or h ^ $ Ctrl-D Ctrl-U :nn<Enter> (or nnG) G

48

CS2750 Advanced Programming with UNIX

VI editor


Deleting text
Items to delete Character Word Line n Lines (from the current line) Current position to end of current line Block of lines All of the lines in the file All of the lines from line 1 to current line All of the lines from current line to end of file Two lines beginning from current line x dw dd ndd D :<range>d<Enter> :1,$d<Enter> :1,.d<Enter> :.,$d<Enter> :.,.+2d<Enter> Key sequence

49

CS2750 Advanced Programming with UNIX

VI editor


Copying-and-Pasting text
Action Copy current line Paste the copied text after current line Paste the copied text before current line n Lines (from the current line) Copy block of lines p P nyy (or nY) :<range>y<Enter> Key sequence yy (or Y)

50

CS2750 Advanced Programming with UNIX

VI editor


Replacing text
Items to replace Character Word Line r cw cc Key sequence

51

CS2750 Advanced Programming with UNIX

VI editor


Searching (for a string)


Action Search forward from current position for string sss Search backward from current position for string sss Repeat last search Repeat last search in the reverse direction Key sequence /sss<Enter> ?sss<Enter> n N

52

CS2750 Advanced Programming with UNIX

VI editor


Searching and Replacing


Action Replace the first occurrence of sss on current line with ttt Replace every occurrence of sss on current line with ttt (global replace) Replace all occurrences in a range of lines Key sequence :s/sss/ttt<Enter> :s/sss/ttt/g<Enter> :<range>s/sss/ttt/g<En ter>

53

CS2750 Advanced Programming with UNIX

VI editor


Saving and Loading files


Action Save file as <name> Save file with current name Save file with current name and exit Read in contents of another file at current position Edit file <name> instead of current file Key sequence :w <name><Enter> :w<Enter> :wq<Enter> (or ZZ) :r <name><Enter> :e <name><Enter>

54

CS2750 Advanced Programming with UNIX

VI editor


Miscellaneous
Action Redraw screen Undo the last operation Undo multiple changes made on the current line Join the next line with the current line Repeat the last operation Quit vi if work is saved Quit vi without saving Show line numbers Remove line numbers Ctrl-L u U J . :q :q! :set nu :set nonu Key sequence

55

CS2750 Advanced Programming with UNIX

VI editor


~/.exrc
You can store basic vi environment in .exrc file First go to your home directory and open the file: % cd ~ (or just cd) % vi .exrc Then insert your own vi settings, e.g., :set ts=3 :set autoindent :set number
56

You might also like