Lecture Slides
Total Page:16
File Type:pdf, Size:1020Kb
Credit Where Credit is Due • These slides for CSC209H have been developed by Sean Culhane, a previous instructor: I have modified them for this presentation of the Introduction to course, but must acknowledge their origins! UNIX S -1 S -2 Logging in Shells • Login name, password • Bourne shell, C shell, Korn shell, tcsh • System password file: usually “/etc/passwd” – command line interpreter that reads user input and executes commands • /etc/passwd has 7 colon-separated fields: > ls -l /var/shell total 6 maclean:x:132:114:James MacLean: lrwxrwxrwx 1 root 12 May 15 1996 csh -> /usr/bin/csh ^^^1^^^ 2 ^3^ ^4^ ^^^^^^5^^^^^^ lrwxrwxrwx 1 root 12 May 15 1996 ksh -> /usr/bin/ksh /u/maclean:/var/shell/tcsh lrwxrwxrwx 1 root 17 May 15 1996 newsh -> /local/sbin/newsh ^^^^^6^^^^ ^^^^^^^7^^^^^^^ lrwxrwxrwx 1 root 11 May 15 1996 sh -> /usr/bin/sh lrwxrwxrwx 1 root 15 May 15 1996 tcsh -> /local/bin/tcsh 1: user name 5: “in real life” 2: password (hidden) 6: $HOME 3: uid 7: shell 4: gid S -3 S -4 newsh “man page” Files and Directories newsh • UNIX filesystem is a hierarchical arrangement of directories & files newsh - shell for new users • Everything starts in a directory called root whose name is the single SYNOPSIS character / newsh DESCRIPTION • Directory: file that contains directory entries newsh shows the CDF rules, runs passwd to force the user to • File name and file attributes change his or her password, and runs chsh to change the – type user's shell to the default system shell (/local/bin/tcsh). – size FILES /etc/passwd – owner SEE ALSO – permissions passwd(1), chsh(1) – time of last modification HISTORY Written by John DiMarco at the University of Toronto, CDF S -5 S -6 1 Files: an example Directories and Pathnames > stat /u/maclean • Command to create a directory: mkdir • Two file names automatically created: File: "/u/maclean" -> "/homes/u1/maclean" – current directory (“.”) Size: 17 Allocated Blocks: 0 Filetype: Symbolic Link – parent directory (“..”) Mode: (0777/lrwxrwxrwx) Uid: ( 0/ root) Gid: ( 1/ other) Device: 0/1 Inode: 221 Links: 1 Device type: 0/0 Access: Sun Sep 13 18:32:37 1998 • A pathname is a sequence of 0 or more file names, separated by /, Modify: Fri Aug 28 15:42:09 1998 optionally starting with a / Change: Fri Aug 28 15:42:09 1998 – absolute pathnames: begins with a / – relative pathnames: otherwise S -7 S -8 Working directory Permissions • Current working directory (cwd) • When a file is created, the UID and GID of the creator are remembered – directory from which all relative pathnames are interpreted • Every named file has associated with it a set of permissions in the form of a string of bits: • Change working directory with the command: cd or chdir rwxs rwxs rwx • Print the current directory with the command: pwd owner group others • Home directory: working directory when we log in mode regular directory – obtained from field 6 in /etc/passwd r read list contents • Can refer to home directory as ~maclean or $HOME w write create and remove x execute search s setuid/gid n/a • setuid/gid executes program with user/group ID of file’s owner • Use chmod to change permissions S -9 S -10 Input and Output Basic UNIX Tools • File descriptor man ("man -k", "man man") (1.13) – a small non-negative integer used by kernel to identify a file ls -la ("hidden files") cd • A shell opens 3 descriptors whenever a new program is run: pwd – standard input (normally connected to terminal) du, df – standard output chmod – standard error cp, mv, rm (in cshrc: "alias rm rm -i" ... ) mkdir, rmdir (rm -rf) • Re-direction: diff ls >file.list grep sort S -11 S -12 2 More Basic UNIX Tools C Shell Commands more, less, cat which head, tail, wc echo compress, uncompress, bg, fg, jobs, kill, nice gzip, gunzip, zcat alias, unalias lpr, lpq, lprm dirs, popd, pushd quota -v a209xxxx exit pquota -v a209xxxx source logout, exit rehash mail, mh, rn, trn, nn set/unset who, finger date, password S -13 S -14 Additional Commands arch cal Introduction to the ps hostname C Shell clear tar uptime xdvi gs, ghostview setenv, printenv S -15 S -16 What is the Shell? csh Shell Facilities • A command-line interpreter program that is the interface between the • Automatic command searching (6.2) user and the Operating System. • Input-output redirection (6.3) • The shell: • Pipelining commands (6.3) – analyzes each command • Command aliasing (6.5) – determines what actions to be performed • Job control (6.4) – performs the actions • Command history (6.5) • Example: • Shell script files (Ch.7) wc -l file1 > file2 S -17 S -18 3 I/O Redirection Pipes • stdin (fd=0), stdout (fd=1), stderr (fd=2) • Examples: who | wc -l • Redirection examples: ( <, >, >>, >&, >!, >&! ) ls /u/csc209h |& sort -r fmt • For a pipeline, the standard output of the first process is connected to fmt < personal_letter the standard input of the second process fmt > new_file fmt < personal_letter > new_file fmt >> personal letter fmt < personal_letter >& new_file fmt >! new_file fmt >&! new_file S -19 S -20 Filename Expansion Command Aliases • Examples: • Examples: ls *.c alias md mkdir rm file[1-6].? alias lc ls -F cd ~/bin alias rm rm -i ls ~culhane \rm *.o unalias rm * Matches any string (including null) alias ? Matches any single character alias md [...] Matches any one of the enclosed characters alias cd 'cd \!*; pwd' [.-.] Matches any character lexically between the pair [!...] Matches any character not enclosed S -21 S -22 Job Control Some Examples a | b | c • A job is a program whose execution has been initiated by the user – connects standard output of one program to standard input of another • At any moment, a job can be running or stopped (suspended) – shell runs the entire set of processes in the foreground • Foreground job: – prompt appears after c completes – a program which has control of the terminal a & b & c • Background job: – executes a and b in the background and c in the foreground – runs concurrently with the parent shell and does not take control of – prompt appears after c completes the keyboard a & b & c & • Initiate a background job by appending the “&” metacharacter – executes all three in the background • Commands: jobs, fg, bg, kill, stop – prompt appears immediately a | b | c & – same as first example, except it runs in the background and prompt appears immediately S -23 S -24 4 The History Mechanism Shell Variables (setting) • Example session: • Examples: alias grep grep -i set V grep a209 /etc/passwd >! ~/list set V = abc history set V = (123 def ghi) cat ~/list set V[2] = xxxx !! set !2 unset V !-4 !c !c > newlist grpe a270 /etc/passed | wc -l ^pe^ep S -25 S -26 Shell Variables Shell Control Variables (referencing and testing) • Examples: filec a given with tcsh echo $term prompt my favourite: set prompt = “%m:%~%#” echo ${term} ignoreeof disables Ctrl-D logout echo $V[1] history number of previous commands retained echo $V[2-3] mail how often to check for new mail echo $V[2-] path list of directories where csh will look for commands (†) set W = ${V[3]} noclobber protects from accidentally overwriting files in redirection set V = (abc def ghi 123) noglob turns off file name expansion set N = $#V echo $?name • Shell variables should not to be confused with Environment variables. echo ${?V} S -27 S -28 Variable Expressions File-oriented Expressions • Examples: Usage: set list1 = (abc def) -option filename set list2 = ghi where 1 (true) is returned if selected option is true, and 0 (false) otherwise set m = ($list2 $list1) -r filename Test if filename can be read -e filename Test if filename exists @ i = 10 # could be done with “set i = 10” -d filename Test if filename is a directory @ j = $i * 2 + 5 -w filename Test if filename can be written to @ i++ -x filename Test if filename can be executed -o filename Test if you are the owner of filename • comparison operators: ==, !=, <, <=, >, >=, =~, !~ • See Wang, table 7.2 (page 199) for more S -29 S -30 5 csh Script Execution • Several ways to execute a script: 1) /usr/bin/csh script-file 2) chmod u+x script-file, then: csh a) make first line a comment, starting with “#” – (this will make your default shell run the script-file) b) make first line “#!/usr/bin/csh” – (this will ensure csh runs the script-file, preferred!) • Useful for debugging your script files: “#!/usr/bin/csh -x” or “#!/usr/bin/csh -v” • Another favourite: “#!/usr/bin/csh -f” S -31 S -32 if Command if Command (cont.) • Syntax: • Syntax: if ( test-expression ) command if ( test-expression ) then • Example: shell commands if ( -w $file2 ) mv $file1 $file2 else if ( test-expression ) then shell commands • Syntax: else if ( test-expression ) then shell commands shell commands endif else shell commands endif S -33 S -34 foreach Command while Command • Syntax: • Syntax: while ( expression ) foreach item ( list-of-items ) shell commands shell commands end end • Example: • Example: set count = 0 foreach item ( ‘ls *.c’ ) set limit = 7 cp $item ~/.backup/$item while ( $count != $limit ) end echo “Hello, ${USER}” @ count++ • Special statements: end break causes control to exit the loop • break and continue have same effects as in foreach continue causes control to transfer to the test at the top S -35 S -36 6 switch Command goto Command • Syntax: • Syntax: switch ( test-string ) goto label case pattern1: ... shell commands other shell commands breaksw ... case pattern2: shell commands label: breaksw shell commands default: shell commands breaksw end S -37 S -38 repeat Command Standard Variables • Syntax: $0 Þ calling function name repeat count command