Outline ● UNIX History ● UNIX Today? Unix System Programming ● UNIX Processes and the Login Process ● Shells: Command Processing, Running Programs ● The File Introduction ● The Process ● System Calls and Library Routines 1 2 Maria Hybinette, UGA Maria Hybinette, UGA UNIX History UNIX History (cont) ● Developed in the late 1960s and 1970s at Bell Labs ( the most ● 1973 UNIX philosophy versatile, powerful an flexible OS in the word). K. Thomson, developed: D. Ritchie, McIlroy, Ossanna (nroff) and later Canaday » Write programs that do one ● UNICS - a pun on MULTICSn time share system (Multiplexed thing and do it well Information and Computer Service) which was supposed to » Write programs that work support 1000 on line users but only handled a few (barely 3). together (MULTI-UNiplexed) » Write programs that handle ● Thomson writes first version of UNICS in assembler for a text streams, because that is PDP-7 in one MONTH which contains a new type of file the universal interface system (initial motivation was the game space travel) » Kernel (notion of processes) Dennis Ritchie (standing) and Ken Thomson » shell » editor and the » assembler K.I.S.S. ● 1969 Thomson writes interpreter B based on BCPL -- Ritchie improves on B and called it � C � (but first NB). Keep It Simple, Stupid! ● 1972 UNIX is rewritten in C to facilitate porting Thomson Ritchie 3 4 Maria Hybinette, UGA Maria Hybinette, UGA UNIX Today What Unix Gets Wrong (Raymond) ● Supports many users running many ● UNIX files have no structure above byte level programs at the same time, all sharing the ● File deletion is irrevocable same computer system ● Unix security model is too primitive ● Information Sharing ● There are too many different kind of names for things ● Having a file system at all may have been the wrong ● Geared towards facilitating the job of creating choice new programs ● Final choices are pushed to the as far toward the user as ● Sun: SunOS, Solaris; GNU: Linux; SGI: IRIX; possible (user know better than OS designers what their Free BSD; Hewlett Packard: HP-UX; Apple: own need are) OS X (Darwin) » Loosing non-technical users » But maybe longevity because competitors are more tied to one soet of policy or interface choicdes that fades from view 5 6 Maria Hybinette, UGA Maria Hybinette, UGA
What Unix Gets Right (Raymond) User UNIX Interface: SHELL ● Evidence - the Linux revolution ● Provides command line as an interface between the user and the system ● Open Source Software (cooperative, re-usable) » Key to UNIX � s success ● Is simply a program that starts automatically » David Eckel � agree, his books are freely available and the most when you login profitable! ● Uses a command language ● Cross-Platform portability an open standards » Consistent API across heterogeneous mix of computers » Allows programming (shell scripting) within the shell » Scales environment ● Internet and the WWW » Uses variables, loops, conditionals, etc. » DoD contract for TCP/IP production went to the UNIX development » Accepts commands and often makes system calls to group because of its open source! carry them out ● Flexibility all the way down (glue program together) ● Unix is fun to hack ● The lessons of UNIX can be applied elsewhere 7 8 Maria Hybinette, UGA Maria Hybinette, UGA Various UNIX shells The Korn Shell (ksh) ● sh (Bourne shell) ● I will frequently be using ksh as the standard shell for examples in this class ● ksh (Korn shell) ● Language is a superset of the Bourne shell ● csh (C shell) (sh) ● tcsh ● bash ● … ● Differences mostly in scripting details 9 10 Maria Hybinette, UGA Maria Hybinette, UGA Changing Shell Environment variables ● On most UNIX machines: ● A set of variables the shell uses for certain operations » which ksh (note path) » chsh ● Variables have a name and a value ● On the some machines: ● Current list can be displayed with the env » which ksh (note path /bin/ksh) command » ypchsh ● A particular variable � s value can be displayed » May need to contact system administrator with echo $<var_name> ● Some interesting variables: HOME, PATH, PS1, USER, HOSTNAME, PWD 11 12 Maria Hybinette, UGA Maria Hybinette, UGA
Setting environment variables Aliases ● Set a variable with ● Aliases are used as shorthand for frequently- used commands » Ksh/bash: <name>=<value> » tcsh: setenv <name> <value> ● Syntax: ● Examples: » ksh: alias <shortcut>=<command> » TERM=vt100 » tcsh: alias <shortcut> <command> » PS1=myprompt> ● Examples: » PS1=$USER@$HOSTNAME: » alias ll= � ls -lF � » PS1= � multiple word prompt> � » alias la= � ls -la � » PATH=$PATH:$HOME » alias m=more » DATE=`date` » alias up= � cd .. � » alias prompt= � echo $PS1 � 13 14 Maria Hybinette, UGA Maria Hybinette, UGA Repeating commands Editing on the command line ● Use history to list the last 16 commands ● Some command lines can be very long and complicated - if you make a mistake you ● tcsh: traverse command history: don � t want to start all over again » <CNTRL>-P previous history ● You can interactively edit the command line » <CNTRL>-N next history in several ways ● ksh: ESC, then k (up), j (down) RETURN » set -o vi allows you to use vi commands to edit the command line (ksh) » set -o vi-tabcomplete also lets you complete commands/filenames by entering a TAB 15 16 Maria Hybinette, UGA Maria Hybinette, UGA Login scripts Example .profile (partial) ● You don � t want to enter aliases, set # set ENV to a file invoked each time sh is started for # interactive use. environment variables, set up command line ENV=$HOME/.kshrc; export ENV editing, etc. each time you log in HOSTNAME=`hostname`; export HOSTNAME PS1="$USER@$HOSTNAME>" ● All of these things can be done in a script that is run each time the shell is started alias 'll'='ls -l' ● For ksh: alias 'la'='ls -la' alias 'ls'='ls -F' » ~/.profile - is read for a login shell alias 'rm'='rm -i' » ~/.kshrc alias 'm'='more' ● For tcsh set -o vi » ~/.login echo ".profile was read" » ~/.cshrc 17 18 Maria Hybinette, UGA Maria Hybinette, UGA
stdin, stdout, and stderr Redirecting stdout ● Each shell (and in fact all programs) ● Instead of writing to the terminal, you can tell automatically open three � files � when they a program to print its output to another file start up using the > operator » Standard input (stdin): Usually from the keyboard ● >> operator is used to append to a file » Standard output (stdout): Usually to the terminal ● Examples: » Standard error (stderr): Usually to the terminal » man ls > ls_help.txt ● Programs use these three files when reading » echo $PWD > current_directory (e.g. scanf() ), writing (e.g. printf() ), or » cat file1 >> file2 reporting errors/diagnostics 19 20 Maria Hybinette, UGA Maria Hybinette, UGA Redirecting stderr Redirecting stdin ● Instead of reading from the terminal, you can ● Instead of reading from the terminal, you can tell a program to read from another file using tell a program to read from another file using the: the < operator » ksh: 2> operator ● Examples: » tcsh: &> operator » mail user@domain.com < message ● Example: suppose j is a file that does not exist » interactive_program < command_list {atlas} ls j ls: j: No such file or directory {atlas} ls j &> hello.txt {atlas} cat hello.txt ls: j: No such file or directory 21 22 Maria Hybinette, UGA Maria Hybinette, UGA Pipes and filters Examples of piping and filtering ● Pipe: a way to send the output of one ● ls -la | more command to the input of another ● cat file | wc ● Filter: a program that takes input and ● man ksh | grep � history � transforms it in some way ● ls -l | grep � maria � | wc » wc - gives a count of words/lines/chars ● who | sort > current_users » grep - searches for lines with a given string » more » sort - sorts lines alphabetically or numerically 23 24 Maria Hybinette, UGA Maria Hybinette, UGA
Recommend
More recommend