Escolar Documentos
Profissional Documentos
Cultura Documentos
2. Basic concepts.
2.1. The UNIX nut: kernel and shell.
The main control program in a UNIX OS is called the kernel. However the kernel does not allow the user to give it commands directly; instead when the user types commands on the keyboard they are read by another program in the OS called a shell which parses, checks, translates and massages them in various ways to be described later, then passes them to the kernel for execution. (The analogy is with a nut - as in walnut - the important part of which is the kernel inside; the shell is merely the face that the nut presents to the outside world!) Modern shells aim to reduce the amount the user has to type by providing facilities such as command recall and edit, command spelling correction, command or filename completion, and "wildcard" characters (characters in filenames that represent multiple possibilities). There are a number of different shells available, with names such as sh, csh, tcsh, ksh, bash, each with different rules of syntax; these are partly though not completely responsible for the diversity of UNIX. Once the command has been interpreted and executed, the kernel sends its reply,
which may simply be a prompt for the next command to be entered, either directly to the display monitor, or more usually if a windowing system (e.g. X-windows) is in operation, via a display manager (e.g. xdm). This is a program responsible for deciding where and in what form the output will appear on the display monitor. If for any reason the kernel cannot perform the command requested (wrong syntax, for example), the reply will be an error message; the user must then re-enter the corrected command.
also make use of pipes and other control structures such as variables, condition tests and loops. UNIX also has extensive facilities for inter-process communication (in addition to the pipe mechanism).
several disks; the boundaries between physical disks cannot be seen merely by looking at the directory hierarchy. Your system administrator will decide where your home directory is to be both physically and logically located. Every file in the hierarchy is identified by a pathname, this is merely a description of the the path you have to traverse to get from the root directory to the file. E.g. the file junk.data (not a wise choice for a file containing data you wish to keep!) in directory /d/user1/tom/keep has the pathname /d/user1/tom/keep/junk.data . This is an absolute pathname, because it starts with a / . The pathname of a directory may be optionally terminated by a / ; e.g. in the example above /d/user1/ means the same as /d/user1 . Strictly, a filename is distinct from a pathname; a filename is just one of the components of the pathname delimited by /'s. Relative pathnames which have a start point anywhere but / in the hierarchy may be used instead, and are often more convenient. Starting from /d/user1/tom/keep the relative pathname of the previous example is just junk.data or ./junk.data (because "." means the current directory). Starting from user tom's home directory it would be keep/junk.data. User dick in his home directory might refer to the same file either by its absolute pathname or by the name ../tom/keep/junk.data (".." means the parent directory which contains the current directory). User harry residing in /d/user2/harry would have to use the absolute pathname or ../../user1/tom/keep/junk.data . A useful shorthand for the home directory is ~ (though this syntax is not supported by all shells), so Tom can also refer to the same file by the absolute pathname ~/keep/junk.data, and Harry can use ~tom/keep/junk.data . Another shorthand is the use of "wildcard" characters in filenames, technically called filename globbing. The * character in a filename represents any string of characters, including no characters; the ? character represents any single character; a string of characters between [] represents any one of those specific characters. These wildcard characters may be used more than once, and may appear in combination in a pathname. Thus * by itself would be expanded to a complete list of filenames in the current directory, whereas ? would be expanded only to a list of 1 character filenames (if any), ?? to a list of 2 character filenames, and so on. If there are files called "fee", "fie", "foe" and "fum" in the current directory, the strings f* and f?? would be expanded to the full list of names, whereas f?e and "f[eio]e" would be expanded to "fee fie foe" ("fum" doesn't match the last two patterns). It is important to understand that it is the shell that is responsible for this wildcard expansion, and it does it completely mechanically without regard for any "intended" meaning (see example of the mv in Unix exercises section). Last, but not least, note that UNIX is always fussy about the case of letters in commands, usernames, passwords and filenames; so Junk.data is not the same file as junk.data .
to any other process, whether parent or child. Global variables are inherited from the parent by the child, but not vice versa. Local variables are conventionally given lower case names, while global variables have upper case names. Global variables include the home directory (HOME), the path (PATH), the current directory (PWD), the shell (SHELL), and the terminal type (TERM). The path is a list of directory filenames which is searched for external commands or scripts whenever the command or script name does not contain a directory specification; if there is more than one instance of the file in the path, the first one found is executed. Note that the alias list is always searched first, followed by the list of built-in commands, followed by the directories in the path in sequence. If the command contains a directory specification (e.g. ./run) none of the alias, built-in command or path lists are checked.
Commands may be piped using a vertical bar (|) to separate them. As an alternative to piping the output may be redirected to write to a file using >filename or appended to the file using >>filename. This only redirects the standard output stream; redirection of error messages (standard error stream) requires a different syntax which is shell-dependent. The standard input stream may be redirected to read from a file using <filename. Either or both standard input and output may be redirected. In a script the standard input stream may be redirected to read from the script by using <<terminator, e.g.:
command [option(s)] [argument(s)] Input data goes here. EOD <<EOD
The terminator above must match the one given on the command line exactly and must start in the first character position. Any sequence of characters may be used in the terminator, except for special characters like &|<>'"\ . If it is preceded by a backslash (\) this has a special meaning: variables beginning with $ in the input data are not treated as shell variables.
3. UNIX commands.
3.1. Process and screen control.
Note that some of the Ctrl key functions may have been altered by an stty command; use stty -a to get the current settings. Key Function
Ctrl-c Kill foreground process Ctrl-z Suspend foreground process Ctrl-d Terminate input, or exit shell Ctrl-s Suspend output
Backspace or Delete Delete character to left of cursor Ctrl-d Ctrl-u Ctrl-k Ctrl-w Ctrl-y Ctrl-t Ctrl-r Delete character under cursor Delete whole line Delete line from cursor on Delete line to left of cursor Paste deleted text Transpose characters Refresh line
Tab
Ctrl-d List filenames matching partial name Note that Ctrl-d has 4 possible functions, depending on the context.
Function Redirect standard input from file Redirect standard input from command source Redirect standard output to file Redirect standard output and overwrite file Redirect standard output and append to file Redirect standard output to file or append to file Redirect standard output/error to file Redirect standard output/error and append to file Redirect standard output/error to file or append to file Pipe standard output to standard input Pipe standard output/error to standard input Replace command with its output
chkdsk [] dir/w [] dir [] dir/o:-d [] dir/s [] dir/p/s [] cd cd {} cd .. cd \ md {} rd {} type {} type {} | more tv {} copy {} {} rename {} {} move {} {} sort {} edit {} del {} del/p {} deltree {} pkzip {} pkunzip {} msbackup []
df [] ls [] ll [] ll -t [] ll -R [] ll -R [] | more pwd cd {} cd .. cd mkdir {} rmdir {} cat [] [] more {} [] less {} [] cp {} {} mv {} {} mv {} {} sort {} emacs {}& rm {} [] rm -i {} [] rm -r {} gzip {} gunzip {} tar cv .
Show disk space Wide directory listing Long directory listing Ditto in reverse creation order Recursive long listing Ditto with pausing Show current directory Change directory Change to parent directory Change to home directory Create empty directory Delete empty directory List file(s) Ditto with pausing Ditto Copy file Rename file Move file Sort file Edit file Delete file(s) Ditto with confirmation Recursive delete directory Compress file(s) Decompress file(s) Make backup
msbackup [] attrib {} {} fc {} {} find "{}" {} print {} print print/c doskey/h doskey {}={} set set {}={} path path {} for {} {} command {} [] rem [] echo {} if {} {} goto {} cls Ctrl-c Ctrl-z Pause Space exit
tar xv chmod {} {} diff {} {} grep {} {} lpr -P{} [] lpq -P{} lprm -P{} {} history alias {} {} set set {}={} echo $PATH
Restore from backup Change file attribute(s) List file differences Search file(s) for character(s) Print file List printer queue Delete print job Show command history Create command alias Show local variables Set local variable Show executable path
setenv PATH {} Set executable path foreach {} {} tcsh [] {} [] # [] echo {} if {} {} goto {} Ctrl-l Ctrl-c Ctrl-d Ctrl-s Ctrl-q exit [] Loop command Start child shell Run program or script Use comment in script Display message in script Conditional command Jump to label in script Clear screen Kill foreground process Signal end of input Suspend output to screen Resume output to screen Exit from script or shell
3.6.2. Process control. Unix command yppasswd stty bindkey alloc limit limit {} {} umask {} xargs {} [] tr {} {} setenv setenv {} {} unset {} unsetenv {} echo ${} nice {} [] renice {} {} Ctrl-z jobs ps [] bg [] fg [] stop {} kill {} Function Change password Show or set terminal characteristics Show or set key bindings Show memory allocation Show process limits Change process limit Change permission for new files Execute command with passed arguments Translate character(s) Show global variables Set global variable Unset local variable Unset global variable Show value of variable Run command at low priority Reduce priority of command Suspend foreground process List background jobs Show process status Put job in background Put job in foreground Suspend background process or job Signal or kill process or job
Submit command script to batch queue List logged-in users Wait for time interval Time a command Conditionally execute loop Terminate current session
3.6.3. Networking. Unix command finger [] talk [] pine ftp [] telnet [] rlogin [] rsh {} [] rcp {} {} Function List user information Exchange messages with other user Use e-mail Transfer file(s) via network Remote login via network Ditto Remote shell command via network Copy file(s) via network
3.6.4. Programming. Unix command ci {} co {} make [] f77 {} fsplit {} Function Check file into revision control Check file out of revision control Update program Compile Fortran 77 program Split Fortran source into subroutines
cc {} c++ {} perl {}
Compile C program Compile C++ program Compile and run Perl program
3.6.5. Miscellaneous. Unix command date cal bc Function Show today's date Show calendar Evaluate arithmetic expression(s)
Ctrl-x Ctrl-f Ctrl-x Ctrl-s Ctrl-x Ctrl-w Ctrl-x s Ctrl-x i Ctrl-x Ctrl-b Ctrl-x b Ctrl-x k
Open file in new buffer Save active buffer to existing file Save active buffer to specified file Prompt to save changed buffers to their files Insert another file List all buffers Select specified buffer Kill specified buffer
Note that the above file and buffer manipulation commands are not needed for versions of Emacs
that run in an X-window and have a File/Buffers menu bar. In the following "motion" commands note that the Alt Gr key sometimes works as Alt. Emacs command Up/down arrow Left/right arrow Alt-f Alt-b Ctrl-e Ctrl-a Alt-> Alt-< Ctrl-v Alt-v Ctrl-l Alt-x goto-line n Function Move up/down 1 line Move left/right 1 character column Move forwards one word Move backwards one word Go to end of line Go to beginning of line Go to end of file Go to beginning of file Go down one screenful Go up one screenful Centre current line Go to line number n
Incremental search forwards Incremental search backwards Global replace with prompt Cancel current operation
Delete character before cursor Delete character under cursor Kill to end of line or delete blank line Paste in (yank) last block of text killed or selected Paste in any previous block of text killed or selected
Undo last change (may be repeated) Set mark Select region between mark & cursor Kill region (cut) between mark & cursor
Note that the above cut and paste commands are not needed for versions of Emacs that run in an X-window and have an Edit menu. Also the mouse can be used to select text by dragging with the left button down, moving the mouse cursor to the insertion point and using the middle button to paste. Emacs command Ctrl-c Ctrl-r Ctrl-c Ctrl-w Ctrl-x 2 Ctrl-x 3 Ctrl-x o Ctrl-x ^ Ctrl-x } Ctrl-x 1 Ctrl-x 0 Alt-x shell Ctrl-x Ctrl-c Function Column numbers (Fortran mode only - starts at 0!) Make window 72 columns wide for Fortran Split window vertically Split window horizontally Select another window Make window taller Make window wider Kill all but active window Kill active window Start shell in Emacs window Exit Emacs
Some of the above functions are not implemented in some versions of Emacs.
:set ai :set sm
Move up/down 1 line Move left/right 1 character column Go to 1st/last column of current line Go down to 1st printable character of next line Move right/left n words (1 word if n omitted) Go to line n (end of file if n omitted) Page forward/backward 1 screen Page forward/backward half a screen Go to beginning of current/next C function
/expressionEnter
?expressionEnter Search backwards for expression n N Repeat last / or ? command in same/reverse direction
ytarget Y
Insert text before cursor Open new line below cursor and insert text Replace character under cursor with next typed Replace text
Backspace
x X nx nX dd ndd D
Delete character under/before cursor Delete n characters under and to right of cursor Delete n characters before cursor Delete current line Delete n lines Delete from cursor to end of line
p P J :m,n s/old/new/gc u
Put back yanked or deleted text below/above current line Join current and next lines Global replace (g=every occurrence on line, c=prompt) m=. means from current position, n=$ means to EOF Undo last change
Quit, provided no changes were made Quit without saving Save (write) changes Save lines m through n (default=all) to file Save changes and quit
6. Unix exercises.
# <-- Note this means a comment - you don't have to type the comments! # DON'T FORGET TO USE LINE RECALL AND EDIT - IT WILL SAVE YOU A LOT OF TYPING! # Set variable P to the PDB data directory. setenv P /public/pdb/data # Do this once for each instance of the shell.
cp $P/pdb6lyz.pdb . # ... and the character after "6" is an ell not a one. # Set mode to write: chmod +w pdb6lyz.pdb # Don't forget to use filename completion. # Define display for X application (once per shell): setenv DISPLAY lundynn:0 # Edit the file in background; note the ampersand (&): emacs pdb6lyz.pdb& # You'll find it easier if you make your shell and Emacs windows wider.
'($1=="ATOM") {print $6,$7,$8}' pdb6lyz.pdb '($1=="ATOM") {print $4}' pdb6lyz.pdb '($1=="ATOM") {if ($5!=s) {print $4; s=$5}}' 6lyz.seq
pdb6lyz.pdb >!
6lyz.seq
alias run '(time tcsh \!^:r.csh) >&\! \!^:r.log&' # Now make a script with emacs, e.g. "job.csh" and type "run job".
-i".
unalias
mv
cat file1.c cat file2.c mv file?.c file?.C # Note Unix distinguishes lower and upper case! cat file1.C # What happened to your .c files? Explain why! cat file2.C
find $P -name \*.pdb -exec grep ASPARTIC {} \; # This is too slow! So just select a few files: find $P -name pdb1pp\?.pdb -exec grep ASPARTIC {} # No filenames are printed: # Insert -print before -exec # Now you get all filenames! find $P -name pdb1pp\?.pdb # The right way to do it! above (use line recall!). -print | xargs grep
\;
ASPARTIC
unix-beg.html