36-711 Using Unix
Total Page:16
File Type:pdf, Size:1020Kb
36-711 Using Unix References: http://www.sun.rhbnc.ac.uk/unix/unix.html, http://www.ussg.iu.edu/usail/index/concepts.html, http://www.mcsr.olemiss.edu/unixhelp/ (Frankly, http://www.google.com is another good choice!) 8 major aspects of Unix 1. Input/Output. This is how UNIX goes about translating data between screens, keyboards, disks, tapes and so on. 2. The Command Interpreter is that part that of the operating system that inter- faces with the user. It deals with keyboard input and calling programs. In UNIX it is called the SHELL. 3. Data Management is that part of the operating system that allows you to use files and directories and the tools to manipulate them. 4. Program Development Tools in UNIX include compilers, debuggers and ed- itors and source code revision systems. There are also many non-essential tools that tend to get added over the years. 5. Time Sharing - UNIX is a time-sharing operating system - time is shared between the processes that can run at any one time. 6. Security - all multiple user operating systems and those that connect to other computers by modems or networks need to consider security. In UNIX this has been designed into the file system. Every user has a username and a group that (s)he belongs to and the users and groups can restrict access to others. 7. Communications - UNIX uses the TCP/Internet suite of protocols, for elec- tronic mail, file transfer, logging on to other machines, etc. 8. Accounting - UNIX provides accounting facilities that are required in a multi-user environment to control resource utilization like CPU time and use of file space. 1 Features of the Kernel Creation and management of processes A filesystem Communications A means to start the system Features of Shells (Command Interpreters) Program execution Variable and filename expansion I/O redirection Pipeline hookup Environment control Interpreted programming language (scripts) There are several common shells: sh, csh, tcsh, bash. You may be able to see which one you are using by typing echo $version. The csh Shell 1. Initialization: .cshrc (.login) 2. Entering commands (a) tab cause filename completion; ctrl-d shows completion list (b) semicolon separates multiple commands (c) arrows, backspace, ctrl-d, ctrl-k, etc. are active (d) end with enter 3. Command interpretation (a) line of command input is read and broken into words by looking for ¢ separating space or &, , ;, ¡ , , (, or ) (b) words are placed on the history list (c) words are parsed: i. history substitution: !!, !n, !str or !?str ii. command alias substitution (alias dir ’ls -al | less’) 2 iii. I/O redirection iv. variable substitution ($str), except inside single quotes v. command substitution (backquotes) ¡ ¢ ¢ vi. filename substitution (*, ?, [...], str,str,... , , user) (d) Find first word, find it in hash table based on $PATH and execute it; remaining words are the command/runstring arguments; terminating ampersand (&) causes program to run in the background (e) ctrl-c cancels a foreground job, ctrl-z suspends a foreground job (f) override special characters with backslash, e.g. \& 4. Built in-commands (not run in a new process) (a) echo, history, printenv (b) alias, unalias, set, unset, setenv, unsetenv (c) jobs, fg, bg, nice, kill (d) exit, popd, pushd, rehash, source (e) if, switch, foreach, while, shift 5. Utility programs (find program file and run in a new process) (a) whoami, apropos, man, whatis, where, which, ps, expr (b) pwd, cd, ls, file, cat, more, less, head, tail, wc (c) mkdir, cp, mv, rm, chmod, rmdir (d) emacs (or xemacs), clean i. Searching: ctrl-s, ctrl-r, alt-% (or esc, %) ii. Files: ctrl-x then ctrl-f, ctrl-r, ctrl-s iii. Splitting: ctrl-x then 2, 1, £ (e) find pathname -name filename [-type d] -print (f) grep [-n -v] what filename (g) sftp, mail, pine, exmh, ssh (h) sort, uniq, diff (i) gcc, f77 6. Redirection (a) stdin, stdout, stderr ¢ ¢ ¡ ¢ (b) ¢ , , , , with &, with grep 3 Shell Scripts (batch files, basic scripting tool) Create with emacs; line 1 is “#!/bin/csh”; do “chmod u+x myname”. # is the comment character To first line, add -n to check only, -v for verbose (before substitution), -x for ver- bose (after substitution). Use $1, $2, etc. for runstring parameters. Use $# for number of runstring parame- ters. ¡ Can use n for newline, t for tab, f for formfeed, for backslash if (myexpression) mycommand if (myexpression) then ... else if (myexpression2) then ... else ... endif In more complicated cases, use switch/endsw. Expressions can use ! for “not”, ==. !=, && for “and”, for “or”, +, -, *, /, and % for “remainder”. (Surround by spaces.) Expressions can include -e myfilename for “file exists”, -d myfilename for “file is a directory”, etc. ¡ The expression mycommand is true if the command executes successfully. Script files often use “set” to create variables. Be sure to precede the variable name with a dollar sign when using it after it is set. Variables can have multiple elements: set junk = (fee fi fo fum) echo $junk[2] set junk = ‘wc ˜/.login‘ echo $junk if ($junk[2] > 20) echo My .login has more than 20 words. shift [myvar] drops the first element from the runstring or myvar. 4 The at sign is used to replace the contents of an existing c-shell variable with the results of a calculation, e.g. set one = 1 set three = 1 @ three = $three + 2 set sum @ sum = $three + $one echo $sum unset one three Other control structures: foreach myvar (mylist) ... end while (myexpression) ... end 5.