Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unix and C Programming Dr. Cong Xing Dept. of Math & CMPS.

Similar presentations


Presentation on theme: "Unix and C Programming Dr. Cong Xing Dept. of Math & CMPS."— Presentation transcript:

1 Unix and C Programming Dr. Cong Xing Dept. of Math & CMPS

2 Access to Unix/Linux Our Unix/Linux – (www.nicholls.edu) telnet in MS Windows Unsecured Not supported any more (by most Unix systems) Putty – freeware, (telnet, ssh) http://www.putty.nl/download.html Login id and password Id: ex: xing (lower case) Password: set it up (first time login) Change your password Type passwd at prompt Exit Unix Type: logout

3 Introduction to Unix Some useful terms: Bit: a binary digit, either 0 or 1 Byte: a grouping of 8 bits Kilo (K): thousand (10^3) Mega (M): million (10^6) Giga (G): billion (10^9) Hertz: used to measure clock speed. 1 hz = 1 pulse per second. 3 Ghz = 3 billion pulse per second.

4 symbolmeaningroughly KiloK2 10 (=1024)10 3 MegaM2 20 10 6 GigaG2 30 10 9 TeraT2 40 10 12 PetaP2 50 10 15

5 Ex: What is the max memory space (in terms of bytes) can a 32-bit CPU address? 2 32 = 2 2 2 10 2 10 2 10 = 4GB 32-bit long register memory

6 Flops: short for floating point operations per second. Also used to measure the speed of computers. The origin of Unix Unix: The best OS people have written so far Dennis Ritchie and Ken Thompson from AT&T Multics – a failed OS, 1969 GE, MIT, and AT&T (joint project)

7 Space Travel – a game Space Travel rewritten on PDP-7 Unix was born in 1971 Naming: Unics (a pun on Multics)  Unix Turning Award (1983) http://awards.acm.org/homepage.cfm?srt=all&awd =140

8

9 Unix and C Unix written initially in assembly language (PDP series) (non-portable) In 1973, Unix was rewritten in C C was made for writing Unix

10 Major Components of Unix Kernel: master control program. It manages resources and handles multitasking. File System: organization of data. Shell: interface between users and kernel. It interprets user commands and passes them to kernel. Utilities: software tools built in Unix.

11 kernel File sys utilities shell user

12 Versions of Unix System V Unix: developed at AT&T. AT&T Unix is the original Unix. Most Unix systems on market are based on this. (e.g. IBM AIX and Sun Solaris) BSD Unix: modified at Berkeley and as popular as AT&T Unix. (MacOS X is based on BSD Unix)

13 Unix-like systems: systems that work much like Unix, but do not use any part of AT&T Unix. (e.g. Linux and Minix) X Window System A GUI for Unix, developed at MIT X terminals: a hybrid I/O device, has CPU and memory and can run X but not a complete computer in itself. (early days, when I was at grad school) X terminal emulator: software simulation of X terminals

14 Example: Mac OS X

15 X terminal in Mac OS X

16 Books? Any introductory Unix book can help Or, any online tutorial Understanding Unix by Stan Kelly-Bootle Unix for Dummies by J.R. Levine and M.L. Young

17 Your Unix Account The system administrator creates your account (and delete your account). System administrator is the “super user” who can access any user’s account Your account info: Account name (id) Password Home directory

18 Group id: you may be assigned to a group of users. Login shell Terminals Called tty (abbr. for teletype) in Unix Putty (use telnet or ssh)

19 Getting Started Use putty ssn or telent to login to eclipse

20 Successful login shows:

21 Use Window’s (old version) telnet to login to eclipse Start  Run  telnet Type o Type host-name type your id and password at prompt

22 Shell prompt $ -- Bourne shell, Korn shell, or bash shell % -- C shell or TC shell Will use $ throughout the slides Script session $script (to start a session) $exit (to end a session) Password change $ passwd Follow instructions

23 Trying out some simple commands $ date Fri Jan 20 11:26:24 CST 2006 $ w (check who is on the system) 11:30am up 116 day(s), 20:30, 3 users, load average: 1.92, 1.70, 1.67 User tty login@ idle JCPU PCPU what xing pts/6 9:11am 27 -bash xing pts/7 11:08am w root pts/4 10Oct05 2days bash

24 $ whoami $ who Email (built-in w/ Unix) $ Mail -- to send an email $ Mail -- to read emails Type x (or ^d or ^D) to quit reading emails Elm: a menu-driven e-mailer (not built-in, needs to be installed) Unix manual $ man -- manual page for the command

25 Logging out $ logout or $ exit or $ ^D One of the above has to work Spelling Unix or UNIX? The latter is traditional, the former acceptable

26 Unix File System File Traditionally: a collection of related data Unix extends the traditional meaning of file To Unix, everything is a file (e.g., printer, disk drive,….) File types Ordinary files: common computer files Special files: device file, represent physical devices.

27 Directory files: ordinary files and special files are organized into directory files or directories. Ordinary files divided into two groups Text files: (also called ASCII files) contains text and created/modified by text editors. Binary files: containing non-textual data. Read and processed by programs.

28 Home and Working directories Home directory: the directory when you log in the system. Each user has a unique home directory E.g.: /home/xing To go to home directory (from anywhere): $ cd Working directory: the current directory in which you are working. Check what is your working directory: $ pwd

29 Unix File Tree Files in Unix are organized in the form of a tree, typically as: bin devetchometmpusr var Users’ home directories Root (/)

30 Bin: short for “binary”. Contains shell software and common Unix commands Dev: short for “devices”. Holds device files. Etc: miscellaneous admin files (such as user list and passwords) Home: holds users’ home directories Tmp: temporary files are kept here. Usr: user-related files, on-line manual are kept here. home directories (for some version of Unix),

31 Var: holds files whose contents varies frequently. Ex: users’ mail boxes are typically in /var/mail directory File Names 1 – 255 character long, combination of: A – Z a – z 0 – 9 period (.), underscore (_), dash (-)

32 avoid using any special symbols and Unix commands in file names. Absolute pathnames full pathnames that identify the location of a file in relation to root ex: (tree on next slide) /home/al/cmps/hw1 /home/smith

33 root / home smithal cmpsmath hw1 hw2

34 ~ (tilde) represents the absolute pathname of your home directory ~ represents the absolute pathname of the home directory of that user Relative pathnames relative to working directory. -- the current working directory.. -- parent of current working directory

35 ex: given the previous tree, assume you are at /home/smith, then $cd../al/cmps/ changes to cmps directory Listing files $ ls -- list all files in working directory $ ls../al/cmps -- lists all files in /home/al/cmps (assume pwd is /home/smith)

36 Hidden Files and Directories a file or directory is hidden if it cannot be listed by ls. $ ls -a -- to list all files and directories Moving Files $ mv oldfilepathname newfilepathname ex: $ mv hw1../math/ ( suppose at /home/al/cmps)

37 Copying Files $ cp filepathname1 filepathname2 ex: : $ cp hw1../math/ $ cp./hw1../math/ ( suppose at /home/al/cmps) Creating Files by Redirecting redirect the “standard output” ex: $ ls > file1 -- result is saved in file1

38 note: redirection into an existing file overwrite the file “double redirection” >> fixes the problem: $ ls >> file1 -- result is appended to file1 Links (hard, and symbolic) One file may have more than one name Each file has a unique i-node – an internal data structure representing the location (disk sector) of the file, the mode of the file,

39 creation date & time, and other info. A (hard) link is a name that refers to a file’s i- node. A link is created using the ln command Ex: $ ln existingfn newfn A symbolic link is a name that refers to another (file) name. Ex: $ ln –s exitingfn newfn

40 name1 name2 name3 name4

41 Long Listing More info about files can be shown by $ ls –l drwxr-xr-x 2 xing staff 512 Jan 23 11:40 t -rw-r--r-- 1 xing staff 8 Apr 26 2004 test drwxr-xr-x 2 xing staff 512 Aug 24 00:06 test1

42 drwxr-xr-x 2 xing staff 512 Jan 23 11:40 test File type Access control linksownergroup Size (byte) Date & time name

43 File access control: First 3 symbols control what owner can do Next 3 symbols control what group can do Last 3 symbols control what public can do for first position: d means directory - means file r (read) – examine (but not change) the contents of a file w (write) – change the contents of a file x (execute) – run the file (program)

44 ex: rwxrwx--- owner and group users have read, write, and execute privileges, public has no privileges r--r--r-- everyone has read privilege only changing file modes (1) u – user (owner) g – group o – others (public) a – all (owner, group, public)

45 = -- assign a permission (remove others) + -- add a permission - -- remove a permission ex: $ chmod u+x filename add execution to user $ chmod g-rw filename remove read, write permission from group $ chmod a=r filename add read to everyone and remove anything else $ chmod u=rw,go=r filename give user read and write, everyone else read

46 changing file modes (2) for each position, 1 indicates yes (permitted), 0 indicate no (not permitted) ex: r--r--r-- is denoted by 100100100 break down into 3 parts and regards each part a binary number, and convert it to decimal then r--r--r-- can be denoted as 444 (100 2 =4 10 )

47 more ex: rwxrwxrwx = 777 rwxr--r-- = 744 rw-rw---- = 660 --------- = 000

48 Conversion between binary and decimal numbers

49

50

51 Working with Files Printing a calendar $ cal -- print current month $ cal month year – prints the given month of the given year -- ex: $ cal 12 2007 $ cal year -- prints the entire year -- ex: $ cal 2006

52 Viewing Files $ cat fname -- (catenate) contents of a file (all the way to the end of a file) $ more fname -- display contents of a file (page by page) -- type q to quit viewing $ less fname and $ page fname -- similar to more

53 Concatenating Files $ cat file1 file2 …. -- display file1 followed by file2 … try/ex: $ cal 6 2007 > june-2006 $ cal 7 2007 > july-2006 $ cat june-2006 july-2007 $ cat june-2006 july-2007 > sum-2007 $ more sum-2007

54 Appending to a File $ command >> fname result produced by command is appended to the end of fname try/ex: $ cal 8 2007 >> sum-2007 Deleting Files $ rm fname -- remove fname Copying and Moving Files $ cp fn1 fn2 $ mv fn1 fn2 (see previous slides)

55 Working with Directories Creating a directory $ mkdir dn --- creates a dir w/ name dn try/ex: $ mkdir cmps $ ls Removing a directory $ rmdir dn --- remove the dir dn Changing working directory $ cd dn --- switch to dir dn

56 Returning to the home directory $ cd or $ cd ~ Printing your working directory path $ pwd

57 Unix common shells namewhatwhenwherewho Bourne shell (sh) Original shell 1979AT&TSteven Bourne Korn shell (ksh) Improved sh 1986AT&TDavid Korn Bourne again shell (bash) Improved sh 1988GNU projectBrian Fox, Chet Ramey C shell (csh)BSD Unix1979UC BerkelyBill Joy TC shell (tcsh)Improved csh 1980CMU, Ohio State Ken Greer, Paul Placeway Debian Almquist shell (dash) Decendant Almquist Shell (ash) 1977 (2002 rename) BSD Kenneth Almquist

58 Your login shell Shown at the command line prompt (check what is your login shell, $ ps) Or, read contents of /etc/passwd Ex: $ grep cxing /etc/passwd Or, $ echo $SHELL Set up by the administrator To change your login shell: chsh $ more /etc/shells (to see available shells) $ chsh /bin/tcsh (to change to tcsh shell)

59 How Shell Processes Commands Shell displays prompt (e.g. $) – ready to receive commands User types A command User types return (end of command) shell interprets the command (looking for prog) Kernel runs the program and shell “goes to sleep” shell “wakes up” when prog finished. display prompt again

60 Options and Arguments options are switches that modify what a command does. the fname that follows a command is referred to as an argument. ex: $ ls -a -l fname or $ ls –al fname check man for option info of a command

61 common options for ls -aAll files including hidden files -lLong listing. -FFlag the files. / after dir, * after exeutable -rList files by reverse order -hSizes are scaled to be readable -dList dir name (not its contents) when argument is a dir, often used w/ -l to see the status of a dir

62 Standard input, output, and error (remember ) : everything is a file to Unix standard input: the file where programs look for input. (keyboard by default) standard output: the file where programs send output. (monitor by default) standard error: the file where programs send error messages. (monitor by default)

63 Redirection revisited $ cal 2007 output is sent to standard output – terminal monitor $ cal 2007 > cal.file standard output is redirected (to cal.file). output is sent to cal.file Mail xing input is taken from the standard input – keyboard Mail xing < afile standard input is redirected to afile. Input is taken from afile.

64 Grouping commands c1; c2;... cn commands are executed one by one consecutively. ex: $ w; ls; cal has the same effect as $ w $ ls $ cal grouping can be useful in redirection

65 ex: create a calendar for summer of 2007 with grouping $ (cal 6 2007; cal 7 2007; cal 8 2007) > sum2007 w/o grouping ?

66 Piping c1 | c2 connects the output from c1 to the input of c2 ex: $ cal 2007; cal 2008; cal 2009 (scrolls too fast) $ (cal 2007; cal 2008; cal 2009) | more (one screen at a time) $ ls | more the output of ls is connected to the input of more

67 Tees stdin stdoutfile tee file -- copies stdin to stdout, making a copy in file

68 What entered (stdin) (response to input : stdout ^d to finish)

69 command1 outfilecommand2 $ command1 | tee outfile | command2 (two pipes, one tee) Output from command1 goes to file outfile and to stdin of command2

70 Ex: $ (cal 2008; cal 2009) | tee mycal | more Filters A filter refers to a utility which takes a stream of data from stdin, transform the data in some way, and sends the result to stdout Ex: cat, more, less, pg, wc, nl

71 Wildcards Characters that can stand for other characters *, ?, [] * -- stands for any sequence of symbols ? – stands for any single symbol [] – stands for any symbol included in the bracket Ex: ls *.exe ls *year*

72 ls temp? ls temp[abc] ls *[m-z] ls –ld *[m-z] Quoting Special Characters Under some shells (e.g. csh), special attention need to be made to echo special symbols ex: % echo What time is it? % echo: No match.

73 to fix this problem QuoteEffect \Cancel the special meaning of next character ‘xxxx’Cancel the special meaning of xxxx “xxxx”Similar to ‘xxxx’, except for $, ``,and \ `xxxx`Execute the command xxxx

74 Ex: $ echo what time is it\?  What time is it $ echo what time is it`date`  what time is itMon Aug 13 ……

75 Background processing Under Unix, a process can run in foreground or background. To run a process in background $ command & ex: $ sleep 60 & (sleep 60 seconds) [1] 1865 where: [1] --- job # 1865 --- process id (PID) At command line, ^c to terminate process, ^z to suspend a process.

76 To terminate a process: $ kill pid kill -9 pid surely kill $ kill %n (n is the job #) kill -9 %n surely kill some useful commands bg %nsend process n to background fg %nbring process n to foreground jobslist jobs status kill %nterminate process n stop %nsuspend process n psshow status of all processes note: some commands may only work under certain shells

77 Summary of job controls Fg running process Suspended process Bg running process To bring it to foreground NothingFg it To send it to background Suspend it and bg it Bg itNothing To Terminate^c or ^z then kill it Kill (-9) it To suspend^zNothingStop it

78 History and filename completion $ history lists recently typed commands $ !# To re-run a command, # is the number $ !! to re-run last command Trick Under csh, tcsh, bash, filenames can automatically be completed by hitting the tab key

79 To check your login shell ps or echo $SHELL or echo $shell To leave a shell $ exit or $ logout ftp and telnet (or ssh) built-in Unix utilities telnet (or ssh): remote login to a networked computer ftp (sftp): file transfer protocol $ telnet hostname

80 $ ftp (sftp) [-port] hostname [-port] get – download files put – upload files ascii – set to ascii transfer mode binary – set to binary transfer mode bye (or quit) – to end ftp MS Windows ftp and telnet also exist in (old) MS Windows they work similarly as in Unix

81 for ftp, an easier way may be to use the GUI program, e.g.: free software WS_FTP32, WinSCP open My Computer | type hostname in the address bar open a web browser, type the hostname in the URL bar

82 Command Summary echo $SHELL or echo $shellcheck login shell shellfilerun shell specified in shellfile exitexit a subsell !nrepeat nth command !!repeat last command fil complete file name starting w/ fil ^ckill a foreground process ^zsuspend a foreground process

83 bg %nsend job n to background fg %nbring job n to foreground jobslist status of all jobs kill pidkill process w/ id being pid kill %nkill job n kill -9 %ndefinitely kill job ns stop %nsuspend background job n wc filecount lines, words, and characters in file

84 sort filesort file in usual order sleep nsleep n seconds command &running in background psobtain process status mkidr dirmake directory mv fi dirmove file fi to directory dir cd dirchange to directory dir cdchange to home directory

85 rmdir dirdelete directory pwdprint working directory path cal m yrshow calendar for month m in year yr cal yrshow calendar for year yr lslist files in working directory cat fishow contents of file fi more fishow contents of file fi, one screen at a time pg fisimilar to more

86 cp fi1 fi2copy file fi1 to file fi2 mv fi1 fil2move file1 to asked rm fidelete file fi chmod spec fichange permission status of file fi passwdchange your password dateprint current date wcheck who is on the system man commcheck the manual for command comm

87 logoutlogout system exitlogout system ^dlogout system (s)ftpFile transfer (upload and download) telnetremote login sshSecure remote login

88 Text Editor – Vi (or Vim) To invoke vi $ vi fname Unix shell Comm mode Insert mode vi fname i, o esc :x

89 Basic commands i ---- go to insert mode esc --- go to command mode h --- move left j --- move down k --- move up l --- move right o --- go to insert mode, move to next line

90 Why choose “i”, “h”, “k”,.... ? --- Nearby finger tips Left index finger right index finger

91 x ---- delete one character dd --- delete entire line u --- undo most recent change :q! --- quit w/o saving :x --- quit w/ saving

92 More commands :w --- save file w/o quitting a --- append text, go to insert mode J --- join two lines together :w fname --- save file to fname “copy & paste” – yank and put (do the following in command mode) ma (at the beginning of block) y'a (at the end of block) p (put what being yanked)

93 Spell checking $ spell fname $ ispell fname Search / pattern --- find occurrence of pattern / --- repeat last find ? pattern --- works like /, but backwords ? --- works like /, but backwords

94 global substitution :%s/old/new/g --- replace every occurrence of old by new in the file Number line :set nu :set nonu Jumping around nG --- jump to nth line G --- jump to bottom

95 command line editing using vi % bindkey –v (tcsh shell) $ set –o vi (bash ) ex: $ echo I havee mad a mistake press to enter vi mode move the cursor to “havee” to fix it press to execute the command

96 Network Terms LAN – Local Area Network WAN – Wide Area Network Unix was designed to be a network-oriented OS (back in what year?) Network Topology Bus Ring Star

97 bus ring star hub

98 Internet History ( see http://en.wikipedia.org/wiki/Internet ) Internet Protocols Protocols --- data communication rules TCP – Transmission Control Protocol IP – Internet Protocol PPP --- point-to-point protocol SLIP – serial line internet protocol High speed Low speed

99 IP Address and Domain Name IP address: four numbers separated by “.” Each host on the Internet has a unique IP ex: 128.46.126.96 Each host can also be identified by its domain name Ex: juno.eecs.tulane.edu host subnet organization Top-level domain

100 Common top-level domains com – commercial organization edu -- school gov -- government mil -- military org – nonprofit organization net – network support organization Country names ca – Canada ch -- Switzerland cn – China fr -- France

101 E-mail (built-in in Unix) mail – original Mail – Berkeley mail mailx – System V mail elm – alternative to standard Unix mailers pine – similar to elm Check to see if one has read his mail finger userid

102 ex: finger xing Common Mail Conventions :-) -- smile ;-) -- wink :-( -- frown (26) xing@eclipse> finger xing Login name: xing Directory: /home/xing Shell: /bin/tcsh On since Feb 17 05:49:55 on pts/2 from ip68-11-69-123.no.no.cox.net New mail received Thu Feb 16 11:47:38 2006; unread since Tue Feb 14 15:20:30 2006

103 Classic Unix Network Utilities rlogin -- remote login telnet – remote login ftp – file transfer rcp -- remote copy Basic ftp commands ? -- for help ascii – for ASCII file transfer binary – for binary file transfer

104 put fn --- upload file fn (send) get fn – download file fn mget fn – multiple get other Unix commands also work under ftp, such as Cd, pwd, ls Anonymous ftp (public ftp) Loign name: anonymous or guest Password: none Ex: ftp.census.govftp.census.gov ftp.cac.psu.edu

105 A problem with ftping files between Windows and Unix. Windows text files (say, generated by Notepad) will show ^M at the end of each line when display in Unix. How to fix it? Type the following tr –d ‘\015’ outFile Octal for ^M Input file name Output file name

106 How to find octal for other control characters? In vi insert mode, type, then type the, then save the file (as fileName). Then, at the command line, type the following to see the octal od –b fileName http://www.neurophys.wisc.edu/comp/docs/ascii/ to check binaries, octals, etc… http://www.neurophys.wisc.edu/comp/docs/ascii/ http://www.unix-manuals.com/refs/misc/ascii- table.html http://www.unix-manuals.com/refs/misc/ascii- table.html

107 World Wide Web (WWW) A way of presenting info on the Internet in the form of hypertext documents. WWW ≠ Internet (why not?) When was WWW born? Was WWW designed by computer scientists? Was WWW designed in the U.S. (as the Internet)?

108 How does WWW work? Client-server model URL (Uniform Resource Locator) http://www.nicholls.edu ftp://eclipse.nicholls.edu (what does the header “http” or “ftp” mean?) ftp://eclipse.nicholls.edu client server request answer

109 Specifically, You instruct the client program (the browser, e.g. IE or Firefox) on your local computer to get a Web document. The client locates the remote Web site. The client sends a request over the Internet to the server. The server on the Web sends a copy of the document you specified. The client program formats the document and displays it.

110 File Compression and Archive Common file compressions CompressionDecompressionSuffixexampe compressuncompress.Zfile.Z gzipgunzip.gzfile.gz pkzippkunzip.ZIPfile.ZIP tar.tarfile.tar

111 $ gzip fn – compress file fn $ gunzip fn – uncompress file fn $ tar –cf fn.tar dir (or files) – create an archive file fn.tar from dir or (files) $ tar –tf fn.tar list contents of the archive $ tar –xf fn.tar restores the archived directory

112 Computer Security Cryptography Plaintext: original unencrypted text. Ciphertext: encrypted text. Algorithm: transform plaintext into ciphertext (and vice versa). Key: info, such as a word, a phrase, required to encrypt or decrypt a message.

113 Encryption scenarios Secret-key encryption: use the same key to encrypt and decrypt a message Public-key encryption: message is encrypted using public key (by the sender) and decrypted using the private key (by the receiver). (the public-key and private key are a matching pair) Public-key authentication: message/request is encrypted using private key (by the sender) and is decrypted using the public key (by the receivers). (Typical situation when login to a server) How to send the secret key over the Internet? Here we go!

114 plaintext ciphertext plaintext A A alg Internet key /R& secret-key encryption sender receiver

115 plaintext ciphertext plaintext A A alg Internet public key private key /R& sender receiver public-key encryption

116 plaintext ciphertext plaintext A A alg Internet private key public key /R& public-key authentication sender receiver(s)

117 RSA RSA RSA RSA The most well-known public-key encryption algorithm Based on the fact that there is no (currently) efficient way to factor a large number Industrial-strength de facto standard Built into web browsers (IE and others) and other software products RSA= Rivest, Shamir, and Adleman in 1977 ACM Turing award winners!!! Restricted by US government for exporting to foreign countries (really?)

118 ssh (secure shell) vs. telnet both ssh and telnet are remote login utilities. telnet = plaintext (becoming/already obsolete) ssh = ciphertext, offers RSA public-key authentication. It also offers userid-password combination. to use ssh in Unix: % ssh

119 public-key authentication w/ ssh conventional authentication : password secure authentication: public-key In Unix, generate a key pair: % ssh-keygen and follow the instructions. copy the public key to the remote host, append it to the file ~/.ssh/authorized_keys

120 ssh-keygen screen shot

121 change file permission: % chmod u=rwx,go=rx ~/.ssh % chmod u=rw,go=r ~/.ssh/authorized_keys eady to go: % ssh user-name@host-name note: once public-key auth is set up, passwd auth will disappear (not needed) In PuTTY Refer to the handout (Putty manual) or http://the.earth.li/~sgtatham/putty/0.55/htmldoc/Chapter8.html

122 Open putty-keygen, choose ssh2RSA Generate the key-pair save the private key to somewhere in your hard drive Save the public key (may be optional)

123

124

125 (or, newer)

126 telnet to eclipse (the server), append the public-key to the file.ssh/authorized_keys in your home directory. (create such a file if you do not have one) Make sure no one has right to write authorized_keys except you. (chmod go- w authorized_keys) Logout eclipse (the server)

127

128

129 Open putty, put eclipse.nicholls.edu in the host name field Make sure ssh is selected Click the Auth under Connection-SSH, click the browse button to open your private key file Select “keyboard-interactive” (ssh2) for authentication methods Click open button to start the login. (Note: eclipse will not ask you password for authentication, instead, a short message about public-key auth is displayed)

130 Click here

131 Check here Click here Click here to locate pk

132 (or, newer)

133

134 WULA!!! WULA!!!! I can relax now.

135 (or, newer) WULA!!!! Again!.

136 More about public-key authentication and password (authentication) The following is taken from http://winscp.net/eng/docs/public_key Public key authentication is an alternative means of identifying yourself to a login server, instead of typing a password. It is more secure and more flexible, but more difficult to set up.

137 In conventional password authentication, you prove you are who you claim to be by proving that you know the correct password. The only way to prove you know the password is to tell the server what you think the password is. This means that if the server has been hacked, or spoofed, an attacker can learn your password.

138 Public key authentication solves this problem. You generate a key pair, consisting of a public key (which everybody is allowed to know) and a private key (which you keep secret and do not give to anybody). The private key is able to generate signatures. A signature created using your private key cannot be forged by anybody who does not have that key; but anybody who has your public key can verify that a particular signature is genuine. generate a key pair

139 Now, relax and enjoy state-of-the-art security, the Turing-award-winning RSA! Wait a minute… Quantum computing can break it!!! what ???

140 FYI: Why didn’t Bill Gates receive Turing Award? Which of the following brilliant ideas which have significant impacts on our life is originally from Microsoft (Bill Gates)? GUI for OS (windowing) Internet, TCP/IP Email, ftp WWW, http, html Scripting html document Web browser (graphical and non-graphical) Security, RSA Fundamental computer architecture Answer : 0. Bill Gates is a (successful) businessman rather than a computer scientist

141 GUI for OS: Mac had GUI before Windows Internet, TCP/IP: back to 1960-70 Email: Internet utility, ftp: Internet utility WWW, http, html: Tim Berners-Lee, CERN http://www.cern.ch Scripting html document: JavaScript, Netscape Web browser (graphical and nongraphical): Netscape (Mosaic) Security, RSA: obvious Computer fundamental layout: John von Neumann http://www-groups.dcs.st- and.ac.uk/~history/Mathematicians/Von_Neumann.html http://www-groups.dcs.st- and.ac.uk/~history/Mathematicians/Von_Neumann.html

142 Unix Startup Files Startup Files allow users to customize working environment Types of Startup Files System-wide startup files Created by system administrator Contains commands to be used by your login shell Executed (if exist) first by your login shell. Login initialization files created by users resides in your home directory

143 executed when users login Shell initialization files created by user resides in your home directory executed when a subshell starts up (whether it is a login shell or not) System-wide Startup Files Reside in /etc (typically)

144 File name depends on login shell sh, ksh, bash /etc/.profile csh, tcsh /etc/.login, /etc/.cshrc, /etc/csh.login, /etc/csh.cshrc Login Initialization Files Depends on login shell sh, ksh, bash.profile

145 csh, tcsh.login Shell Initialization Files Also called environment files Executed whenever a subshell is invoked Name depends on shell (name) sh does not have a shell initialization file ksh :.kshrc csh:.cshrc

146 tcsh:.tcshrc; if.tcshrc not found, look for.cshrc bash:.bashrc Order of Execution system-wide startup file shell initialization files login initialization files (order of last two may vary depending on systems)

147 Variables Values of variables are setup in startup files to customize the working environment types of variables environment variables: predefined system shell variables user-defined variables: personal variables created by users

148 Common Environment Variables HOMEPathname of your home directory PATHDirectories where shell looks for commands SHELLPathname of shell TERMYour terminal type MAILPathname of system mail USERYour user name

149 Setting Environment Variables depends on shell sh, ksh, bash ex: TERM=vt100 csh, tcsh ex: setenv TERM vt100 Listing Values of Environment Vars sh, ksh, bash set

150 csh, tcsh setenv Check the value of a specific Environment Variable ex : echo $TERM echo $SHELL Special Variables of csh and tcsh csh and tcsh use the same environment variables as the sh family. (e.g. HOME) (all capitals)

151 But csh and tcsh also have their own special variables (e.g. home) (all lower-case) pwdpathname of current working directory historysize of history list homepathname of home directory pathdirs where shell looks for commands termterminal type shellpathname of shell useryour user name promptcurrent prompt symbol

152 note: some variables have a uppercase counterpart as environment variable (e.g. home). For such variables, if their values are changed, their uppercase counterparts will be changed too (and vice versa, at least under tcsh. Note: commands for setting env vars and special vars are different ) To list the values of all special variables % set To set value of special variable % set = User-defined Variables To define your own variable, in.login (and.cshrc) add: set = ex: set web = /home/xing/public_html (note the usage difference between set and setenv)

153 To use the newly defined variable % ls $web (note the $ ) % cd $web (note the $) Calendar Utility (questionable in Linux) don’t confuse it w/ cal utility In.login (or.cshrc), add the line calendar create a file named calendar in your home which contains a line involving today’s or tomorrow’s date. To run.login w/o logout, type source.login

154 History Utility % history gives some previously typed commands % !n to re-execute the command, where n is the number or the first letter of the command you look for. % !! Execute the last command size is adjustable in.login and/or.chsrc add set history = n (n is a number)

155 Aliases used to shorten long command ex: in.login and/or.cshrc add alias h “history” In.login and/or.cshrc add alias rem “/bin/rm –i” rem will prompt you with yes/no, so it is a way to safely remove files. Comments lines start with #

156 Shell Scripting What is a shell script? A file that contains some shell commands and is to be executed by the shell. Ex: # a simple script: shs cal date w How to execute shell scripts?

157 suppose shs is the name of above script submit shs as an argument for sh (or other shells, csh, bash,…) % sh shs (or % csh shs) or % chmod u+x shs (mark shs executable) % shs (or./shs) Now you are scripting!!

158 Subshells when a script is executed, the login shell actually calls another shell (a subshell) to process the script. Regardless of your login shell, you may use any shell to process your shell scripts. To specify which shell to use to process a script file, add the following line at the top of your script file. #!/bin/ =sh, csh, tcsh,…

159 ex: #!/bin/sh # a shell script cal date w note #! is “one” character, cannot be separated (#! is called “shebang” ) blank space and new-line space matter here!

160 Shell as a programming language Variables Input/output Arithmetic operations Conditional expressions Selection structures Loop structures

161 Variables Three kinds of variables can be used in scripts. Environment variables: e.g. HOME User-defined variables: for users’ own needs Positional parameters: store values from command-line arguments

162 Positional parameters indexed from 0 to 9: 0, 1, 2, …, 9 $0 holds the filename, $1 holds the first argument, S2 the second argument, $3 the third argument, … ex: #!/bin/sh # a shell script, name: shs0 cal date w echo $0 $1 $2 $3

163 run this script with (shs0 is the filename of the script) % shs0 i like it $0 $1 $2 $3 arguments

164 Two special positional parameters $* -- lists all command-line arguments $# -- gives the number of arguments $0 is not considered an argument (here), but is the file name (or command) ex: echo “you typed” $# “arguments:” $*

165

166 as a simple application, we can write a script that makes a file execute. #!/bin/sh # make a file executable chmod u+x $1 echo $1 ‘is now executable’ ls -l $1 Save it as “myscript”, it can make itself executable % sh myscript myscript

167 Set command and positional parameters set can be used to assign values to positional parameters. the whole set of “original” arguments (if any) following the script name will be replaced by that produced by the set command ex: set `date` # note the ` and ` pair, the command # date will be executed echo “Time:” $4 $5 echo “Day:” $1 echo “Date:” $3 $2 $6

168 date produces Fri Aug 20 10:33:34 EST 2005 which is caught by set and consequently assigned to $1 $2 $3 $4 $5 $6

169

170 Labeling output from wc $ wc gives the number of lines, number of words, and number of characters in, and the name of ex $ wc lab1 5 17 84 lab1 want to write a script “mywc”, s.t. $ mywc lab1

171 shows: File: lab1 Lines: 5 Words: 17 Characters: 84 How can we do this?

172 #!/bin/sh # label output from wc set `wc $1` # execute wc $1 and put the result # as the positional parameters echo “File: $4” echo “Lines: $1” echo “Words: $2” echo “Characters: $3”

173 User-defined variables combination of lower case letters, upper case letters, underscore, and digits. first character cannot be digits ex: a, b1,c2, f_var Assignment = (no space in between) Ex: a=“this is a string” b=$a echo $b # prints “this is a string”

174 Read statement stores command line arguments into variables (different from positional arguments) ex: #!/bin/sh # read example echo ‘what is your name?’ read name echo ‘Well’, $name, “you typed “ $# “arguments” echo “and they are:” $*

175

176 The expr Utility syntax : expr arg1 arg2 can be: +, -, \*, / (int division), % (int remainder) meaning: evaluates the expression ex: expr 3 + 4  7 (note space around +) expr 3 / 4  0 expr 3 % 4  3 Shell script can be written for simple arithmetic operations.

177 #!/bin/sh # add two numbers sum=`expr $1 + $2` echo “result is: “ $sum $ add 3 4 result is: 7 $ add 4.9 4.3 ----error (integer value expected)

178 if statement Syntax if cond then commands fi ex: #!/bin/sh set `date` if test $1 = Fri then echo “Wula, weekend” fi

179 Arguments of test command -d filefile is a directory -f filefile is a file -r filefile is readable -s filefile size > 0 -x filefile is executable -w filefile is writable ! -d filefile is not a directory ! -f filefile is not a file ! -r filefile is not readable ! -s filefile size not > 0 ! -x filefile is not executable ! -w filefile is not writable

180 n1 –eq n2n1 = n2 n1 –ge n2n1 >= n2 n1 –gt n2n1 > n2 n1 –ne n2n1 \= n2 n1 –lt n2n1 < n2 n1 –le n2n1 <= n2 n1, n2 are integers s1 = s2s1 equals s2 s1 != s2s1 is not equal to s2 s1, s2 are strings

181 If-then-else syntax if cond then commands else commands fi ex: #!/bin/sh # if example set `date` if test $1 = Fri then echo “Wula, weekend” else echo “still have to work” fi

182 Remove files #!/bin/sh # file del # delete files interactively if test ! –f $1 then echo “no such file” $1 else echo “do you want to delete” $1 “(y/n)” read choice if test $choice = y then rm $1 echo $1 removed else echo $1 “not removed” fi

183 Case statement Syntax case var in p1) comd1; …; comdn ;; p2) comd1;…; comdn;; ….. pn) comd1;…; comdn ;; *) comd1; …; comdn ;; esac ex: #!/bin/sh # case ex set `date` case $1 in Fri) echo "Friday";; Sat | Sun) echo "weekend too"; echo "go fishing";; *) echo "not weekend"; echo "need to work";; esac

184 For-loops Syntax for var in list do commands done Ex #!/bin/sh for name in $* do finger $name echo ********* done

185 Ex: #!/bin/sh b="1 2 3 4 5 6 7 8 9 10" for a in $b do echo "value is" $a done

186 Ex: #!/bin/sh # an improved spelling script for word in `spell $1` do line=`grep -n $word $1` echo " " echo "misspelled word: $word" echo "$line" done grep –n word file find and print each line in file that contains word

187 While-loops Syntax while cond do commands done Ex: #!/bin/sh c=10 while test $c –gt 0 do echo value: $c c=`expr $c – 1` done

188 shift command shifts positional parameters ($1,$2,…) one position to the right $0 is not shifted ex: % mycomm 1 2 3 er ds $0 $1 $2 $3 $4 $5 shift % mycomm 1 2 3 er ds $0 $1 $2 $3 $4

189

190

191 C

192 Overview The simplest “hello world” program # include main() { printf("Hello World\n"); }

193 How to compile and execute C programs? (gcc can be replaced by cc) $ gcc test.c $ a.out Or $ gcc –o name test.c $ name

194

195 Understanding the program # include main() { printf("Hello World\n"); } #-line: preprocessing directives. Cause pre-processor to include the header file stdio.h which contains info about printf() The function main() where execution begins Print the string “Hello Word” on the screen (and advance to the next line), by the function printf()

196 A variation, what would be the output? # include main() { printf("Hello\nWorld”); }

197

198 Basic Input and Output Input: syntax: scanf(control string, other-args); ex: scanf("%d%d", &x, &y); semantics: read and convert two inputs from the input stream into variables x and y in the format specified in the control string

199 scanf() Format (conversion char) To what? ca character da decimal integer fa floating point number (float) lfa floating point number (double) sa string

200 Basic Input and Output output: syntax: printf(control string, other-args); ex: printf("%3c%5c\n", ‘A’, ‘C’); semantics: print out characters A and C in the format specified in the control string _ _ A _ _ _ _ C

201 printf() Format (conversion char) how to print the arguments? cas a character das a decimal integer fas a floating point number eas a floating point number in scientific notation sas a string

202

203

204

205

206 Fundamental Data Types int (long) float double (long double) char (boolean? String?)

207 Identifiers rule: {letter | _ } 1 {letter | _ |digit} 0+ ex: a, _a, month_of_year but not not#me 101_n -plus

208 declaration of variables syntax: type var1, …, varn; ex: int x, y; double x1; int x, y=3;

209 Basic operations/operators () + (unary) - (unary) ++ -- * / % + (binary) - (binary) = += -= *= /=

210 Assignment statement Java is the same as C ex: a = 3; x = x + 1; etc…

211

212

213 ++ and -- increment / decrement operator ++a and a -- different ex:

214

215 so, ++a causes a to be increased by 1 first, and the value of ++a is whatever stored in a. a++, the value of the expression a++ is the current value of a. Then a is incremented by 1 after (the evaluation of this expression) use ++ or -- with caution.

216

217 sizeof () operator gives the size (in byte) required in memory for fundamental types ex: (next slide)

218

219

220 more about assignment = is an operator in C, and has the associated value compare + in a + b, with = in a = b a + b is an expression, so is a = b a + b has a value, so is a = b (a + b) + c makes sense,

221 so is c = (a = b) but, this style of programming, not suggested.

222 ex:

223 char and int character can be considered as int and vice versa note: not saying int is the same type as char. We cannot declare a variable as type int and char at the same time again, this style of programming is not suggested (allowed of course)

224

225 Relational, equality, and logical operators < > <= >= == != ! && || same meaning as in Java false = any zero-value (0, 0.0, or NULL, …) true = any non-zero value

226 ex:

227

228 Consider this…. There is no logical type (boolean type) in C But, there logical operators in C (!, && ||) So, what about the following expression 3 < j < 5 when j =7?

229

230 3 < j < 5 is the same as (3 < j) < 5 3 < j yields 1 (true), when j=7. so (3 < j) < 5 is the same as 1 < 5, which yields 1 (true) lesson learned: typing is important

231 more about what typing can cuase if (a = 1) ………… /* do sth */ if (a == 1) ….. /* do sth */

232 both are syntactically correct the first one will run forever…. the second one is what we want..

233 Selections if-statement syntax if (expression) statement ex: if ( a == 1) x = y;

234 if-else statement syntax if (expression) statement else statement

235 ex: if ( x == y) { i =1; j =2; } else { i = j; }

236 ex: is the following code syntactically correct? if (i == j) { i = i+1; j = j+2; }; else i = j-1;

237 ex: is the following code semantically clear? if (i == j) if (a == 2) i = i+1; else j = j-1; if (i == j) if (a == 2) i = i+1; else j = j-1;

238 rule : the else matches with nearest if. So the first one is the “right” one.

239 Is this else-if statement? if (….) printf(…); else if (…) printf(….);

240 while-loop syntax: while (expression) statement usage: the as Java

241

242

243 for-loop same as Java syntax: for (expr1; expr2; expr3) statement next statement meaning interpreted in terms of while- loop

244 expr1; while (expr2) { statement; expr3; }

245 ex:

246 the conditional operator ?: ?: is ternary syntax: expr1 ? expr2 : expr3 if expr1 is true, then expr2 will be the result of the entire expression, otherwise expr3 will be the result of the expression.

247 ex:

248 functions (modular programming) #include function prototypes (optional ) function defs main () {…..}

249 ex:

250 functions

251 recursive functions

252 Pointers, 1-d Arrays, Strings Pointers variables holding memory addresses 1-d arrays vectors (same as Java) index starts from 0 (not 1) Strings 1-d array of characters last value of a string is ‘\0’

253 example of strings “abcde” is stored as ‘a’ ‘b’ ‘c’ ‘d’ ‘e’ ‘\0’

254

255 pointer declaration: ex: int *p; meaning: p is a pointer to type int; or p is a variable of type int *; or p refers to some memory location which holds an int value. p ? (NULL)

256 if p is a pointer, then *p (called pointer dereferencing) means the memory location (cell) which is pointed by p. ex: int q=1, *p; p = &q; p *p q 1

257 so, int *p; can be understood in both ways: as (int *) p; i.e., p is variable of type int *. or as int (*p); i.e., *p is a variable of type int.

258 example next slide, study it carefully, as it involves some fundamental facts about pointers.

259

260 Now, why does it not work?

261 type synonym : typedef give a new name for existing types: ex: typedef char * string; typedef int number; string s1; number n;

262

263 structure (or record) syntax struct name { type field; ….. } name is optional

264 ex: after this, struct complex (together) is used as a type

265 accessing members of a structure use the “.” operator structure_var. member or use the “->” operator ptr_to_structure -> member

266

267 or, equivalently

268 or, (maybe better)

269 or, w/ pointers

270 or, w/ header file Put definition (and other things) of type complex is in a header file, say, complex.h.

271 then, to have better moularity

272 Use structure w/ functions


Download ppt "Unix and C Programming Dr. Cong Xing Dept. of Math & CMPS."

Similar presentations


Ads by Google