You are on page 1of 90

History of Unix

1970-1974, “Simple is beautiful”
– early stage of AT&T Bell Lab, on PDP-11 machine – Unix is not an acronym, but a weak pun on MULTICS

       

1976, first licensed release, Version 6 1978, first portable version, Version 7 1979, Berkeley 3BSD 1983, System V as industry standard 1984, Microsoft releases Xenix 1986, BSD 4.3, AT&T Version 9 1987, SVR4, Mach, … 1993, Linux

Unix Philosophy

Easy to program by combining small building blocks
– Not (naive) user friendly

A clean minimal design (nothing extra, nothing unnecessary) Open access:
– Provide tools and mechanism to combining the tools – Minimal restrictions to the ways of doing things – A user can be very creative (and frustrated).

Major Unix OS features:
– Kernel – Shell – File System

Unix Operating System Structure

OS mediates between the user and the computer

Application Programs




Kernel      Manages memory and allocates it to each process Schedules work done by the CPU Organizes transfer of data from one part of machine to another Accepts instructions from shell and carries them out Enforces access permission on the file system 4 .

Shell        Command interpreter Create customized environments Write shell scripts Define command aliases Manipulate command history File and command completion Edit the command line 5 .

etc) pipe (temporary file for command linkage) 6 . terminals. Easy to manage. File: basic storage unit. Types: – – – – ordinary file (stores info) directory (holds other files and directories) special file (represents physical devices like printers.File System     Logical method for organizing and storing large amounts of information.

In Unix.UNIX: Multi-user Multi-tasking       More than one user can run at the same time and more than one task can run at the same time Unix is multiuser multitasking. Windows is not. but there may be many processes running the same program. To each interactive user (window): – only one process in foreground – may have several processes in background 7 . each program is started as a process. Window NT is. A process is a program in execution. Usually only one copy of a program.

Start process 1. fork exec /etc/getty condition terminal for login exec /bin/login check password exec shell command interpreter /etc/init inetd lpd httpd fork exec /etc/getty exec /bin/login exec shell 8 .Processes kernal mode user mode kernel Process 0: Kernel bootstrap. Process 1: create processes to allow login.

%) 9 . Init process Unix Process – last step in booting procedure – create other processes to allow the users to login  Getty process – conditions for terminal connection – wait for user-id  display login on the screen  Login process – check password with the uid – execute .profile or .login (depends on default shell) – display shell prompt   Shell process (command line interpreter) Shell prompt ($.

process-group-id Opened files Working directory File creation mask User ID. 10 .UNIX Process  Process environment – – – – – – – – Process id. parent-process-id. Group ID Resource limits Environment variables Code  A child process inherits parent‟s environment.

11 . – use fg to move a background task to foreground. $ ps PID TTY TIME CMD 221 pts/4 4:15 netscape 201 pts/4 0:05 bash 215 pts/4 1:15 emacs-19 – use “&” to execute a task in background  Example: $ sort infile > outfile & – use ctrl-Z to suspend the foreground task. and then use bg.Processes use ps to see the processes that you are running.

Shell: Command Interpreter  Bourne Shell: the default shell (sh) – – – – original unix shell does not have interactive features of newer shells widely used for writing shell scripts standard on Unix System V  C Shell (csh): available on BSD Unix and most other systems – with syntax similar to the C language – with many enhancement over the Bourne shell. bash 12 .  Korn Shell (ksh): AT&T‟s answer to C Shell – combine features of Bourne and C shells – very efficient  Other shells: tcsh.

Command cat cp mv rm lpr lp (Sys V) ls chmod pwd cd mkdir rmdir ps man df du grep Function Display a file Copies a file Renames a file or moves it Delete files Sends a file to a printer Day-to-Day Use Meaning conCATenate CoPy MoVe ReMove Line Printer LiSt Change MODe Print WorkingDir Change Dir MaKe DIR ReMove DIR Process Status Manual Disk File Disk Utilization 13 Lists the contents of a directory Changes the “mode” of permissions Shows the current directory Change current directory Create a directory Delete a directory Shows the processes on the system Shows info. about commands Shows file system status Shows the space allocation of files Search for patterns in files .

 command [options] [arguments] Standard Command Format wc [-c | -m | -C] [-lw] [file …]  stuff in brackets is optional  boldface words are literals (must be typed as is)  italicized (or <> enclosed) words are args (replace appropriately)   Options modify how the command works command [options] [--] [<file> …] options ::= -option white-space options* option ::= noargoption* argoption | noargoption+ noargoption ::= letter argoption ::= letter whitespace string $cc -po zap zap.c 14 .

#links. list subdirectories. permissions. group. etc) R for recursive.Some Examples  ls [-alRF…] file-list – – – – a for listing all files including the dot files l for long format (file type. F for listing directories with a trailing /  ps [<options>] – List the information about running processes – Example: %ps -el # print the info about all processes (e) in the long format (l) 15 . owner.

defaults..g. system programs. %man wait %man man %man -k <keywords> %man 1 wait %man 1 man On-line Documentation  Man(ual) page format Name Synopsis Description (options.  For shell command. %man [n] <command> e. detail desc. and library functions. examples) Files See Also Bugs 16 .

who | wc Or getit < in | check_it | process_it | format_id > out “>>“ appends output to a file E. sort < infile > outfile “|” pipe output of program 1 to input of program 2 E. 17 .I/O Redirection  Redirection and Pipe “>“ redirects standard output (screen) to a file E.g.g.g. ls > dirlist “<“ redirects standard input (keyboard) from a file E.g. ls -l >> oldfile Exercise: find out the definition of “tee”.

who  Concurrent: %pgm1 & prgm2 > file1 & pgm3 %make > log & %sort +1 pdir. Concurrent Process  Sequential: %date %ps -ef %who OR %date.Sequential vs. ps -ef. ((pr dir | lpr) & sort +1 local)) 18 .

File Name Expansion    Each shell program recognizes a set of special characters called meta characters. Bourne and Korn shell meta/wildcard characters – – – – – * ? [a-dA-D] [!AB123] \ matches any string (including null) matches any one character matches any one character in the range matches a char not in this range escape  ~<username> : (not bourne shell) the home dir of the user. The metacharacters are used to create patterns to match filenames and command names. 19 .

two. three. a.* *. 235.1. Doc. one.c.2. Doc.* cd ~foo 20 . 2#5. c*2.File Name Expansion  Assume we have the following files under the current directory: 120.c ls [a-z]*[!0-9] ls ??? ls * ls c*2 a. Doc.c ls *.out.c. c22.3.

Filters   Most UNIX utilities are filters A filter is a program which – – – – reads input (text) from standard input only writes output to standard output only writes error to standard error only may use temporary files for intermediate results   Filters can be combined to work together using pipes Pipe: takes stdout of one command and uses it as stdin of another command ls | wc 21 .

Bourne shell (displays value)  Displaying the value of an alias alias ll 22 .Command Alias   Assign your own name for a command Syntax is shell dependent alias ll „ls -l‟ alias ll=„ls -l‟ C shell Korn.

Unix File Systems  File: a collection of data – Just a sequence of bytes – no record or block structure required  Directory – A file that contains information for files – distinction between a directory and a file  system can alter the contents of a directory directories can contain both files and other directories info. – rooted tree file structure (inverted tree)  23 .

printer messages mail(in) wtmp mail(out) … uucp  /home sue john Special files: /dev/* represents I/O devices. 24 ...Unix File System Road Map / /dev /etc /var /bin /tmp /usr /mnt passwd hosts spool adm include etc bin lib 5bin ….

which uniquely represents a file in the file system. – Each file system has a superblock. I-node table file1 file2 free file3 free superblock  An I-node contains uid. gid. permissions. It contains a description of the file system. modification. location of the file on the disk. an i-node table and files    The superblock is at a fixed location relative to the beginning of the file system. and access. Each entry of the I-node table is an I-node.File Systems and the I-nodes  Each disk drive contains one or more file systems – Each file system occupies a number of cylinder groups. time of creation. 25 . One can find the location of the I-node table thru superblock.

. . \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 0000020 277 ( . thus to the file contents) 26 . od -b . Inodes 0000000 4 . \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 0000040 390 = b l a h \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 0000060  A filename is a link (links name in directory hierarchy to the inode.

octal dump cat foo hi there welcome to unix od -c myfile (output in decimal by byte pairs) 0000000 h i t h e r e \n w e l c o m e 0000020 t o u n i x \n 0000031 1st 7-digits are position in file. ordinal number of next character (in octal) 27 . Directory is an ordinary file Directories – can be read as ordinary files (by any program that reads text) – can‟t be created or written as ordinary files (only system can)     od .

removes directory entry (link) Only when all links are gone is the file (inode) removed ln -command for creating symbolic links ln oldfile newfile (creates another link to the inode)   28 .Symbolic Links    Can have many links to the same file (inode) rm .does not remove inode.

g. group.Permissions  Every file has a set of permissions associated with it – three types of permissions: read ( r). the program “passwd” 29 .  In Unix system. and execute (x) – three sets of permission: user. users are identified by numbers:uid. world. gid ls -l -rwxr-xr-x 1 root 3743 Jan 4 1970 test user group others #links owner size (time of last mod) (file name)  Problem: how do you write a program which allows different users to access a set of files? E. write (w).

30 .)   Solution: use the set-uid bit When a user execute a program with the set-uid bit set. particularly if the owner is root. – For example ls -l /bin/passwd -rwsr-xr-x 1 root set-uid 8454 Jan 4 1994 /bin/passwd   Set-uid bit may break the security wall.Permissions (cont. the user assume the identity of the owner of the program. (users can run the /bin/passwd and act like root) Only special programs can be set-uid program.

one can do “ls” on the directory.w.. 31 .Chmod   Change the access permissions of a file chmod <permissions> <filename> – permissions can be specified as 3 octal digits.  If a directory has “x” in its permision. or u+x. <user. … – “chmod +s test” sets the set-uid bit for file test. ie.x  Example: chmod 755 test – permissions can be specified as +x. or g+r.others>. the three bits of an octal means r. the dir is

Pathnames  Every file and directory in the file system can be identified by a “full path name” (route from root to file) /home/sue/email/f1  Relative path name . Current directory Parent directory / home sue – location relative to current directory fred – if cwd is /home/sue: ls email ls ../email cd ../fred docs f1 email f2 32 . . ls ..

Mounting File Systems
/ /dev /etc /var /bin /tmp /usr /mnt /home

a file system

 

A file system must be mounted before it can be used Root file system is mounted at boot time. A file system can be mounted to a dir. of another mounted fs. Mounting is done in memory

What’s Next?

Shell scripts!

But first, some details


Some Details

cp [-ir…] file1 file2 cp [-ir…] file-list directory
– i for interactive. prompt use whenever a file will be overwritten – r for recursive. copy a directory tree

ls [-alRF…] file-list
– – – – a for listing all files including the dot files l for long format R for recrusive. list the all subdirectories. F for listing directories with a trailing /

date [+format]
– %date „+%h %d, 19%y‟ Oct 1, 1996

   

wc file-list

Some Details (cont.)

– display the number of lines, words and charaters

more file-list
– Browse through text files on page at a time.

head [-n …] file-list
– Display the first n lines of the files (default=10)

tail [+n|-n| -f| …]
– Display the last few lines in the files (default = 10) – Example: %tail +5 foo # display the last parf of foo starting from line 5 %tail -5 foo # display the last five lines of foo %tail +30 foo | head -15 | more #display line 30-45 of foo %tail -f foo # wait and display the new lines appended to foo


 

cut -c list file cut -f list [-dChar] file

Some Details (cont.)

– Cut out selected charaters or fields from each line of a file – Examples: %cut -c 1-5,15-20 foo # extract chars 1-5 and 5-20 from each line of foo. %cut -f 1,3 -d” “ moo # extract field 1 and 3 from each line of moo.

paste file1 file2
– Concatenate corresponding lines of the given input files – Example (reverse two fields of the file abc) %cut -f1 abc > abc1 %cut -f2 abc > abc2 %paste abc2 abc1 > xyz


.] pattern [file-list] – Search the input text files for lines matcing the pattern %grep Unix doc.* # Display the lines with line numbers %grep -v Unix doc. fgrep grep [-nv.)  grep.1 # Display the lines in doc.1 that contains “Unix” %grep -n Unix doc.Some Details (cont. egrep.1 # Display the lines which do not contain “Unix”  sort [-tC…] [-o outfile] [field-list] [file-list] – sort the files %sort +1 list1 # sort list 1 starting from field 2 to the end of the line %sort +2-3 list2 # sort list2 on the third field %sort -n -o list4 list3 sort list3 numerically and place the output in list4 38 ..

– Example file1 file2 file3 apples apples oranges oranges oranges bananas bananas kumquats kiwis peaches 39 .diff  diff file1 file 2 – Display different lines that are found when comparing two files – It prints a message that users ed-lide notation (a . c change. d -delete) to describe how a group of lines has changed.append. – It also describes what changes need to be made to the first file to make it the same as the second file.

4 >kiwis >peaches 40 .) %diff file1 file2 3c3 <bananas ---------------->kumquats %diff file1 file3 1d0 <apples 3a3.diff (cont.

column 2: lines only in file2.– Takes two sorted text fiels and print common lines and lines which appear exclusively in one file on separate colmns. col 3: comm – Example file1 file2 %comm file1 file2 apple apple apple banana cantaloup banana grape grade cantaloup orange kiwi grape lemon kiwi %comm -12 file1 file2 apple grape 41 comm file1 file2 . – column1: lines only in file1.

tr [-csd…] pattern1 pattern2 – Translate input character to output character based on the input and output patterns – Example %tr „[A-Z]‟ „[a-z]‟ <in >out # xlate all letters to lower case. %tr -s „\012\011\040‟ „\012\012\012‟ < in > out # xlate blank. %tr -d „\040‟ < in > out # delete all blanks. 42 . tab and newline chars to newline chars and squeeze (-s) consecutive newline char into one %tr -cs „[a-z][A-Z]‟ „[\012*]‟ < in > out # change all non-aplphbetic (-c) chars to newline chars and squeeze consecutive newlne char into one.

removing all successive repeated lines – Example: file1: %uniq file1 apple apple banana banana banana apple apple banana %sort fruit | uniq -c apple 2 banana 3 %tr -cs „[a-z][A-Z]‟ „[\012*]‟ < fileA | sort | uniq # show a list of distinct words in fileA.uniq [-cdu…] file-list – Display a fiel. 43 .

%find / \(-name a. # remove the files under the curent directory whose names begin with a lower case letter.doc %find /etc/source -atime 2 -print # print the names of the files under /etc/source whose lst access time was 2 days ago.o” \) -atime +7 -exec {} \. – Examples: %find . -name \*.out -o -name “*. -name “[a-z]*” -exec rm {} \.find <dir-name> <exp> – Recursively search the directory tree rooted at <pathname> and find all files whose names satisfy <exp> – There are many details in the expression. . %find . # remove the object and binary executable files under the root 44 direory which have not be accessed more than 7 days.doc -print # list all files names ending with .

cpi # get file name from stdin and “-o” createsarchive which is redirected to proj.cpio %cpio -icdv “*.cpio -i[cdv] -o[cBv]  System V file archive and backup progam – Example %find proj -print | cpio -ocBv > /dev/rmt8 # cpio get file names from stdin. %find proj -print | cpio -ocBv > proj. 45 . -d creates directories as needed.c” </dev/rmt8 # -i read from archive file from the tape device. “-o” create archive which is redirected to the tape device.

tar /usr/local/proj # avoid using full path names.tar proj # create archive file proj.tar % tar tf proj.tar # list of the filenames in proj. %tar cbf 20 proj. “p” to keep all the protection mode.tar from file or dir proj %tar xvf proj.– key := c (create) | t (table of content) |r (append the file) | u (update the file) – options := v (verbose) | b (block) | f (file name follows) | m (use extraction time as the mod file) … – Create/extracting archive files for backup and transporting files %tar cvf proj. tar will insist to put fiels to /usr/local/proj.proj | (cd /newproj/. tar xvpf -) # copy proj to the directory /newproj/.tar # extract files in proj. – cp -r copies a dir tree but all the time info is gone.tar without extracting data. 46 When you extract the file. %tar cf . tar [options] [file-list] . Tar preserve the time info.

tar to proj.tar # compress proj.bin – Sending a dir tree via email %tar cvf proj.uu and generate a new file newfile.uu # encode file.bin and put the result in file.tar.tar proj %compress proj.bin > file.Z | %tar xvf - 47 .bin.uu %uudecode file.tar.bin.Z %uuencode proj.bin.uu # decode the file file.Z proj.Z | mail qli … at the receiving end.bin newfile.tar.bin.uuencode & uudecode  Generate an ASCII encoded version of the give files – Example: %uuencode file. extract the mail and save it in xx %uudecode xx %zcat proj.tar.

File1 is unchanged.7d‟ file1 # delete lines 5 to 7 from file1.sed [-n] | [-e] sed_cmd file_lists  A stream editor. -n suppress stdout so only the lines that matches are copied. %sed „5. – Examples: %sed -n „/hello/p‟ < input > output # copy the lines contains “hello”. %sed „s/Unix/UNIX/g‟ doc2 # replace all Unix by UNIX 48 . %sed „s/Unix/UNIX/‟ doc2 #replace the first occurrence of Unix in each line by UNIX. It copies the lines from the file_list or stdin to stdout. editing the lines in the process.

} linetype == 1 { print $0} END {printf(“%d lines processed\n”.} # finishing it up 49 . $0). print the last field /^$/ { print “This is an empty line” } /^Unix/ { printf(“Line starts with Unix\n %s\n”. linetype=1. NR).} /NonUnix$/ { printf(“End with NonUnix\n”). next. linetype=0. next. – Example: – Program awkexample: BEGIN { linetype=0} # initialization NR == 1 { print $1 “ “ $NF} # if it is the first line.awk [-f progfile] [-Fc] [prog] [files]  Pattern matching and stream editor.

awk (cont.) – Test data file (awktest): Line1-field1 this is the las-field This line should not show Unix is simple and difficult Hello world is very simple Next blank line should show I line some other NonUnix This line should not show Bye 50 .

awk (cont. %awk „{print $2 $1}‟ # exchange field1 and field2 51 .) – %awk -F” “ -f awkexample awktest Line1-field1 las-field This is an empty line Line starts with Unix Unix is simple and difficult Hello world is very simple Next blank line should show This is an empty line End with NonUnix This is an empty line 11 lines processed – Command line program: most of the awk commands can be used in he command line.

Why?) – Example: %chown -R john testdir # john becomes the owner of all files under testdir.. chown [-R] <new-owner> <file-list> Other Commands – Only owner and root can chown. – Examples: %kill -1 1 # poke the init process to reinitialize (reading /etc/ttytab) %kill -9 1234 # kill process 1234 – Signals can be spefified by name. e.  kill [-signal] pid – Sending the specified singal to the process. (Root only in some systems. 52 – %kill -l # lists signal names . KILL is 9.g. HUP is 1. The process can be programmed to catch most of the signals.

-s lists the grand total of each dir given – Examples: %du -s # print the total disk space used by the files in and under the current dir.)  du [-as….Other Commands (cont.] [dir list] [file list] – Reports the allocated dispace for each file and/or directory specified – -a lists all files. 53 . %du -s * # print the disk space used by each file and dir in the current dir.

– -i ignore error code returned by a command – -s silent mode – -r suppress built-in rules – -n no execute mode – -t touch target file – -q question before change – -p printout macro definitions and target descriptions – -d debug mode 54 – -f alernative make file name .make   A tool for maintaining programming projects make [-isrntqpd] [-f file] [macro-definition] [targets] – It allows the users to specify dependencies among different source and binary files in his/her applications.

c def.make (cont.) – Makefile: prog: x.o z.c x.c prog z.c z.o: x.h y.c def.h cc -c x.o def.c cc -c z.o y.o -lm x.o – make does depth-first search on the dependency graph 55 .h cc -c y.o y.c x.o cc -o prog x.o y.o:y.o:z.c y.c z.o z.

Makefile format  a makefile containing explicit dependency lines in the following format: target1 [target2 …]: [dependency …] [.c – Each command is executed by a different subshell.c and f. e. and no other lines should start with a tab. – commands are bourne shell commands – a set of built-in rules are used by make.: . compile f.o depends on . commands] [#commnets] [<tab> commands] [#comments] – each command line start with a tab character.c. 56 .o: $(CC) $(CFLAGS) -c $< # if .c is newer.g.

E.the basename (suffix removed) of the current target test.o files newers than libops into libops $$ .g. 57 .o : test.c $@ .h cc -c $*.the $ sign.c # cc -c test.the full target name $? . LIB=/users3/foo/lib – Predefined Macros for C CC=cc AS=as CFLAGS= -O -g LOADLIBS= – Built-in Macro (evaluated each time make reads a dependency line) $* .o ar r $@ $? # put any .the list of dependencies that are newer than the target libops: ineract.make Macro – Syntax: Name=String.o shed.o gen.

o files. Example: prog: x. x.o z.c.o: $(AS) $(ASFLAGS) -o $@ $< – $< evaluates to whatever the dependecies triggered the rule. If x. # make finds files which can generate the .o y.s. generalized descriptions: . – Suffix rules are predefined.s # define the suffix to be consdiered significant .c .o .make -. $* is similar to $< except that the suffix is removed. Both are used only in 58 suffix rules .o cc -c ….o.SUFFIXES: .c is newer than x.c.c is compiled.o: $(CC) $(FLAGS) -c $< .Suffix Rules – make uses some conventions to simplify the makefille. Eg. x.

c.c HEADERS=interface.c interface.c OBJECT=$(SOURCE:.c dbms.h SOURCE =driver.o: $(CC) $(CFLAGS) -c $*.c=.An Example of make MYPROG=/usr/local/myprog INCLUDE=$(MYPROG)/include BIN=$(MYPROG)/bin LIB=$(MYPROG)/lib CFLAGS= -g -I$(INCLUDE) .h dbms.o) app: ($OBJECT) $(CC) -o app $(OBJECT) -l$(LIB) (continued) 59 .

) print: @echo print source files # @ suppress the comman line printing @for file in $(SOURCE) \ do \ pr -n $$file. \ # $$ to make a $-sign for the shell command done clean: @rm -f *.o – Usage: make app make clean 60 .make-example (cont.

Resource limits. env Working directory. File creation mask code User ID. Environment variables Code 61 . process-group-id Opened files. Group ID. parent-process-id.  Process environment – – – – – – – – Process id.Processes  Process: instance of a program – has unique pid.

waits for child to die – parent reactived upon death of child  Child created as a background process: – parent forks new child – parent immediately resumes activity 62 . – Current process (parent) forks a new process (child)  Child created as a foreground (wrt parent) process: – parent forks new child – parent deactivated.UNIX Process  New process created each time you execute a command.

move background job to foreground.Processes – use “&” to execute a task in background  Example: $ sort infile > outfile & – – – – – – ps . kill pid . jobs . may leave children alive) 63 .list background processes.kill the process   -1 (kill process.list processes. fg pid .move (suspended) job into background. ctrl-C (cancel foreground job) ctrl-Z (suspend foreground job) bg . and children) -9 (kill process.

Shell Process      Upon login: shell process created Any command you type at prompt: new child of your shell process What is your current shell? %echo $SHELL How to switch to another shell? %bash just type shell name How to switch login shell? %chsh user newshell (but won‟t work here) 64 .

& prog > file prog>>file prog<file p1|p2 direct stdout to file append stdout to file take stdin from file connect stdout of p1 to stdin to p2 match string of 0 or more characters match any single character match any single character from ccc ranges like 0-9 or a-e are legal command terminator background process 65 .Shell Metacharacters > >> < | * ? [ccc] .

if successful. `…`.`…` (…) $1. after $. run p2 66 . \ interpreted comment assign variable var run p1. $2 $var \ „…‟ “…” # var=val p1 && p2 p1 || p2 Shell Metacharacters \c run commands the output of … produces run commands in … in a sub-shell arguments to shell file value of shell variable var take c literally (don‟t evaluate) take … literally take … literally. if unsuccessful. run p2 run p1.

pipes. and files are used? %cat doc1 > out1. programs.What’s going on?  What processes. wc -l <out1 %grep “root” /etc/passwd >OUT2 . cat <IN >OUT & %cat doc1 | wc -l 67 .

What’s going on? % date . who ) |wc 5 30 211 68 . % (date . who | wc Wed Sep 24 16:00:00 PDT 1997 4 24 182 | higher precedence than .

What’s Next?  Shell scripts! 69 .

stderr. Use standard library and tools to save coding effort. If file arg is absent use std) Keep messages and prompts to a minimum. Guidelines for writing Unix Commands/Tools/Scripts 70 .stdout. Provide verbose options Input/output data should be text whenever possible. Use dot files and environment variables for frequently used info.        Standard command format Recognize meta-characters (handle multiple files) Standard I/O (stdin.

Shell Script   Bourne Shell/Korn Shell Topics: – – – – pass arguments global and local variables macro functions  Invoking a shell script $shell_script_file or $sh -options shell_script_file – the script file must have execute-permission. 71 .

profile PATH=.:$HOME/bin:/bin:/usr/bin:/usr/local TERM=vt100 export TERM PATH – – – – HOME -.Environment Variables – .shortcut directories for cd command PS1 PS2 -.home directory PATH -.prompt 72 .terminal type for programs such as the editor CDPATH -.where to look for commands TERM -.

who am i exit 0 $chmod +x loginfo $loginfo The current date and time: Thursday October 10 … The number of users: 7 Personal info: lseiter ttya …. 73 .A shell script example The script file loginfo (must be executable): echo “The current date and time: \c” date echo “The number of users: \c” who | wc -l # count number of lines echo “personal info: \c”).

Another Example $cat shellvariables #! /bin/sh # show predefined shell variables echo “The number of arguments is $#” date & echo “The process id of date command is $!” wait echo “The process id of this shell is $$” grep root /etc/passwd echo “The return code from grep is $?” echo “The current set of options is $-” $shellvariables one 2 xyz $sh -a shellvariables one 2 xyz 74 .

# default value of n is 1 75 .Positional parameters – set to shell script arguments.xy $4 -.my_script $1 -.g.a $2 -. e. $my_script a b xy “1 2 3” – positional parameters have the values $0 -.1 2 3 – shift command: $shift [n] # parambers $n+1 $n+2 … become $1 $2 ….b $3 -.

y=Hello echo $x dir1=/usr/bob/doc ls -a $dir1 echo “x = $x” 76 .Using Shell Variables – Symbolic Variables parameter = value Examples: I=5. Y=$I # There should be no space around the “=“ sign # More than one command can be in a line separated by . x = “hello there\n”.

Quoting    „string‟ take string literally $echo „* $HOME‟ * $HOME “string” take string literally.\.`.”.‟ \c take c literally 77 . except $.

case.\n `banner „the banner‟`”  Compound commands: – a pipeline. a list. if. 78 . a command that begins with a certain reserved words: for. a group (…). a list and the time command.0‟ – “ quotes all char except \ $ “ „ ` – „ quotes all char except „ – command substitution (grave accent): $echo “Users currently on the system:\n `who`” $echo “The banner command. time.Quoting & Compound Command – “The Quotes from Hell” “This is a string\n” „The amount is $100. … – I/O redirection applies to the complete command except a pipeline.

a file. executable. writeable. n1 -eq | -ne | -gt | -ge | -lt | -le n2 # n1 = | <> | > | >= | < | <= n2 79 . or a directory.test Command or [ ] if test $# -eq 0 then echo “no positional param!” fi if [ $# -eq 0 ] then echo “no positional param!” fi – options: -r | -w | -x | -f | -d file # the file is readable.

Test Command  Options File testing -r file -w file -x file -f file -d file Logical Connectives ! -a -o 80 Numerical comparison n1 -eq n2 n1 -ne n2 n1 -gt n2 n1 -ge n2 n1 -lt n2 n1 -le n String Comparison str1 = str2 str1 != str2 str .

param defined to word ${param:?word} if defined. otherwise print word and exit shell ${param:+word} word if param defined. param. otherwise word. if param undefined. param remains undefined ${param:=word} value of param if defined. otherwise word.Parameter substitution – Parameter substitution ${param} ${param:-word} value of param if defined. otherwise nothing 81 .

while. case.Commands and Functions  Reserved word commands – [[ test_expression]] – if. select. until  command grouping: (command_list) {command_list}  Function definition function id { compound_list } 82 . for.

For example.CX command Suppose you want a command cx that will take a filename and set its execute permission. $2 (second). $* (all args) chmod +x $1 83 . $cx foo Need to be able to get the filename from the command line Shell variables: $1 (first argument).

fi $if test -d /dir >then > echo “it‟s a dir” >fi >$ 84 . then echo “it‟s a dir”. else command_listn fi check exit code of the command 0 -> normal termination -> True non-0 -> abnormal termination -> False if test -d /usr.Flow Control  General Format: if command_list1 then command_list2 elif command_list3 then command_list4 ….

Looping  What does this command do? $wc -l * 6 file1 10 file2 3 file3 19 total What if you would like things formatted nicely? There are 6 lines in file1 There are 10 lines in file2 There are 3 lines in file3 85   .

if 0. execute body and loop again while test ! -s file1 do echo file1 does not exist or is still empty sleep 500 done 86 .Loops  General Format: while command(s) do body done check return code of command.

Loops  General Format: until command do body done until who | grep $1 do echo $1 has not logged in yet sleep 500 done 87 .

Shell scripts  for var in listofwords do commands done  for f in $* do x=`wc -l $f` echo There are `echo $x |cut -f1 -d‟ „` lines in $f done 88 .

Examples  if test ! -f $1 then echo First arg is not a file if  if [“$NAME” = “John Doe” -a “$BAL” -gt 5000] then echo “ok” else echo “not ok” fi 89 .

Examples  if test -f $1 then echo First arg is a file elif test -d $1 then echo First arg is a directory fi 90 .