1、Unix Systems Programming (32547),Introduction to UNIX,Lecture 1: Introduction,What is systems programming Why Unix? Introduction to the Unix shell A first set of useful Unix commands A one-slide introduction to Perl,What is Systems Programming?,Systems programming, in a strict sense, means programmi
2、ng system software software that is part of or related to an Operating System (OS). An OS is the basic software that permits the operation of a computer. It offers services to the application programs (by means of application programming interfaces (APIs) and system calls) and the user (by means of
3、user interfaces) In this subject, we interpret system programming in a broader sense, as programs that are aware of OS features. In application programming, instead, no significant knowledge of the OS is expected or needed,An OS architecture: the Unix “onion” model,What the model means?,Operating sy
4、stems can be constructed in a set of layers: Kernel central component of the OS Controls the hardware directly Virtualization: provides access for other programs which do not need to be aware of the hardwares features Examples of kernels: Windows NT Kernel and Linux Kernel for i386/Pentium Sun OS an
5、d Solaris for SUN SPARC stations,Shell - also known as Command Line Interface (CLI) (or Interpreter) A program that makes a set of commands available to the user Examples: sh, bash, csh on top of Unix/Linux Windows Command Prompt/MS-DOS Graphical User Interface (GUI) Windows GUI Gnome, KDE, and Xfce
6、 desktop environments on top of X-Windows on top of Unix,CLI versus GUI,However, they are absolutely inconvenient for repeated operations (the user has to repeat all operations step by step) CLIs are much more flexible and powerful for that,GUIs have become increasingly popular since the first Apple
7、 Macintosh made its appearance in 1984 They are very convenient for certain operations such as browsing a directory (aka folder), selecting a file, changing its name,This image is sourced from Wikipedia and depicts copyrighted software; its use for the purpose of illustrating the software is fair un
8、der US law,CLI: example,Example: find the text file in this directory with the greatest number of lines containing the word “food” With shell scripting its easygrep -cw food * | sort -n -k2 -t: | tail -n 1See further comments on Notes Page,Why Unix?,Born in 1969 (AT&T Bell Labs) Microsoft: DOS 1981,
9、 Windows - 1983 Built by programmers for programmers Microsoft: for non-programmers word processing, spreadsheet, graphics Flexible, Scalable, Networked - basis of the Internet for international communication Microsoft: networking late in the piece Stability: almost never crashes Security: most used
10、 by adtministrators and maintained, therefore fewer viruses,Warning! The history of Unix,courtesy of Wikipedia, http:/en.wikipedia.org/wiki/File:Unix_history.en.svg,Warning: 2,Unix is not single, monobrand OS unlike Windows Many main versions have been released Furthermore, many different shells hav
11、e been produced: sh, csh, tcsh, ksh, bash and others Across different Unix versions, even the same shell performs somehow differently Check the differences between: ls on bash on charlie with SunOS ls on bash on a Linux box Ought to be the same, but it isnt! http:/www.bellevuelinux.org/unix_upper.ht
12、ml http:/en.wikipedia.org/wiki/Single_UNIX_Specification,Linux,Linux is becoming a pseudo-standard Unix because: Its free: so everyone is pleased to use it Its open source: so if a user finds a bug, theyll often fix it If theres something new required: a user will develop itAs a result, Linux is gen
13、erally whats being used on many new systems 2. Bash is also becoming a pseudo-standard because it incorporates the best features of older shells such as the C shell and Korn shell,Our conventions,In this subject (particularly in tests), when we say Unix we generally mean Linux (although charlie is S
14、unOS) Shell we always mean bash,Unix shell commands,The general syntax of a Unix shell command is: command options arguments command is the command name The options are typically preceded by a “-” and modify the behaviour of the command The arguments identify the data (usually a file or files) upon
15、which the command performs Examples: ls al list all files in this directory in long listing format cat n dates.txt displays file dates.txt preceding each line with a number,Getting help (man pages),mans your best friend,User Commands man(1)NAMEman - find and display reference manual pagesSYNOPSISman
16、 - -adFlrt -M path -T macro-package -s section name .man -M path -k keyword .man -M path -f file .DESCRIPTIONThe man command displays information from the referencemanuals. It displays complete manual pages that you selectby name, or one-line summaries selected either by keyword(-k), or by the name
17、of an associated file (-f). If nomanual page is located, man prints an error message.,mans pages are often installed on the Unix system or can be found in websites (be careful youre using the right ones),Example,You want to ensure that when you move a file to a new destination, you will be prompted
18、by the system if a file with the same name already exists at the destination You search the web for “unix move command” and discover that the needed command is mv. Then, man mv:,bash-4.0$ man mvUser Commands mv(1)NAMEmv - move filesSYNOPSIS/usr/bin/mv -fi source target_file .-i mv will prompt for co
19、nfirmation whenever the movewould overwrite an existing target.,mv i source destination is the right answer,A filesystem organises your data in files and directories and stores them across sessions A filesystem is logically organised like an inverted tree:An absolute pathname refers to a filesystem
20、object from the trees root. Example: /massimo/USP/food1.txt,Filesystem basics,/ (root),massimo,ryan,doc.pdf,CVIP,USP,food1.txt,image.png,Filesystem basics (2),At any given time, a user is in a certain location in the filesystem At the start of the session, its in its home directory From the current
21、users location, a filesystem object can be referred to with a relative pathname: If in /massimo, USP/food1.txt is the same object as before You can use absolute or relative paths at your will Path completion prompting,Definitions,Filename, path, pathname: any character string that is used to identif
22、y a file uniquely Basename: the “primary” filenameExamples: food1.txt, doc.pdf, CVIP Absolute filename: a filename relative to the root directoryExamples: /massimo/USP/food1.txt, /doc.pdf Relative filename: a filename relative to the working directoryExamples from working directory /massimo/CVIP: im
23、age.png, /USP/food1.txt, /doc.pdf,Filesystem navigation: pwd, cd,You can know where you are:pwd(displays the absolute path of the working directory) You can change your current location with cd:cd /home/kmfoskey/Docscd /Docscd /Docscdcd /hkmfDoc,Create/destroy directories: mkdir/rmdir,NB: cannot rem
24、ove a non-empty directory,cd mkdir ./NewDir cd /NewDir rmdir /home/davidg/NewDir,Remove files: rm,Be VERY careful when you use rm YOU DONT GO BACK rm and wildcards can ruin your day can do recursive (-r) and force (-f) modes can soften it with interactive (-i) mode GUI file manager tools have a tras
25、h can,rm -rf /home/kmfoskey/Junk , or cd /home/kmfoskey rm -rf Junk rm -rfi Junk (safer),File permissions,The UNIX filesystem has three categories of access: user: user owning the file group: the other users who are members of the group to which the owner belongs others: any other users Permissions:
26、 each file can be (r)eadable, (w)ritable, e(x)ecutable. Permissions are specified for each access category for a total of 3 x 3 = 9 combinations Permissions are often expressed as octal numbers: 4 readable, 2 writable, 1 executable, 5 readable + executable, 6 readable + writable, 0 nothing, 7 all, S
27、ee further comments on Notes Page,Changing file permissions: chmod,chmod changes the permissions of a file mode settings can be absolute or relative to current setting,chmod 705 public_html (u/g/o; absolute) chmod u+rwx o+rx public_html (relative),Text file editors on FEIT Unix systems,Command line
28、editors: vi, vim very powerful, requires time to learn nano very easy, ok for simple editing emacs, ex, edit, view, vedit, X Window editors: nedit xemacs others depending on your window manager (e.g. fvwm95, gnome, kde) Dont miss on a good laugh about the editor war! (http:/en.wikipedia.org/wiki/Edi
29、tor_war),Other useful commands,cp (to copy) ls (to list) find (to find) lp and lpr (to print) tar (to pack) gzip (to compress) command line filters: tail, head, sort,HelloWorld.sh bash scripting,Type and save as HelloWorld.sh:#!/bin/bash#Hello world exampleecho “Hello, World!“Then, add execute permi
30、ssion:chmod +x HelloWorld.shFinally, run:HelloWorld.shYou will see: Hello, World!,An one-slide introduction to Perl,Practical extraction and reporting language A powerful, not-so-elegant language Combines the function of C and shell (and more) Initially designed by Larry Wall a linguist turned programmer Since then extended, part of the open software movement July 2009 stable release: 5.10.0,