unix and c programming

Post on 13-Jan-2016

78 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Unix and C Programming. Dr. Cong Xing Dept. of Math & CMPS. 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 - PowerPoint PPT Presentation

TRANSCRIPT

Unix and C Programming

Dr. Cong Xing

Dept. of Math & CMPS

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

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.

symbol meaning roughly

Kilo K 210 (=1024) 103

Mega M 220 106

Giga G 230 109

Tera T 240 1012

Peta P 250 1015

Ex: What is the max memory space (in terms of bytes) can a 32-bit CPU address?

232 = 22 210 210 210 = 4GB

32-bit long register

memory

• 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)

• 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

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

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.

kernelFile sys

utilities shelluser

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)

• 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

Example: Mac OS X

X terminal in Mac OS X

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

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

• 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)

Getting Started

Use putty ssn or telent to login to eclipse

Successful login shows:

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

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

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

• $ whoami• $ who

Email (built-in w/ Unix)• $ Mail <address> -- 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 <any command> -- manual page for the command

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

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.

• 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.

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

Unix File Tree• Files in Unix are organized in the form of a

tree, typically as:

bin dev etc home tmp usr var

Users’ home directories

Root (/)

• 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),

• 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 (-)

• 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

root /

home

smith al

cmps math

hw1 hw2

• ~ (tilde) represents the absolute pathname of your home directory

• ~<username> 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

• 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)

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)

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

• 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,

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

name1

name2

name3

name4

Long Listing• More info about files can be shown by

$ ls –ldrwxr-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

drwxr-xr-x 2 xing staff 512 Jan 23 11:40 test

File

typeAccess

control

links owner group Size

(byte)Date & time

name

• 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)

• 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)

• = -- 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

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

(1002=410)

• more ex: rwxrwxrwx = 777

rwxr--r-- = 744

rw-rw---- = 660

--------- = 000

Conversion between binary and decimal numbers

6

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

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

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

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)

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

Returning to the home directory• $ cd or• $ cd ~

Printing your working directory path• $ pwd

Unix common shells

name what when where who

Bourne shell (sh)

Original shell

1979 AT&T Steven Bourne

Korn shell (ksh)

Improved sh

1986 AT&T David Korn

Bourne again shell (bash)

Improved sh

1988 GNU project Brian Fox, Chet Ramey

C shell (csh) BSD Unix 1979 UC Berkely Bill Joy

TC shell (tcsh) Improved csh

1980 CMU, Ohio State

Ken Greer, Paul Placeway

 Debian Almquist shell (dash)

Decendant Almquist Shell (ash)

1977(2002 rename)

BSD Kenneth Almquist

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)

How Shell Processes Commands

Shell displaysprompt (e.g. $) –ready to receive

commands

User types A command

User types return (end

of command)

shell interpretsthe command

(looking for prog)

Kernel runs theprogram and shell

“goes to sleep”

shell “wakes up” when prog

finished. displayprompt again

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

• common options for ls

-a All files including hidden files

-l Long listing.

-F Flag the files. / after dir, * after exeutable

-r List files by reverse order

-h Sizes are scaled to be readable

-d List dir name (not its contents) when argument is a dir, often used w/ -l to see the status of a dir

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)

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.

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

• ex: create a calendar for summer of 2007

with grouping

$ (cal 6 2007; cal 7 2007; cal 8 2007) > sum2007

w/o grouping ?

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

Tees

stdin

stdout file

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

What entered (stdin)

(response to input : stdout ^d to finish)

command1

outfile command2

$ command1 | tee outfile | command2 (two pipes, one tee)

Output from command1 goes to file outfile and to stdin of command2

• 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

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*

• 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.

• to fix this problem

Quote Effect

\ 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

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

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.

• 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 %n send process n to background

fg %n bring process n to foreground

jobs list jobs status

kill %n terminate process n

stop %n suspend process n

ps show status of all processes

note: some commands may only work under certain shells

Summary of job controls

Fg runningprocess

Suspendedprocess

Bg runningprocess

To bring it to foreground

Nothing Fg it Fg it

To send it to background

Suspend it and bg it

Bg it Nothing

To Terminate ^c or^z then kill it

Kill (-9) it Kill (-9) it

To suspend ^z Nothing Stop it

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

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

• $ 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

• 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

Command Summary

echo $SHELL or echo $shell check login shell

shellfile run shell specified in shellfile

exit exit a subsell

!n repeat nth command

!! repeat last command

fil <tab> complete file name starting w/ fil

^c kill a foreground process

^z suspend a foreground process

bg %n send job n to background

fg %n bring job n to foreground

jobs list status of all jobs

kill pid kill process w/ id being pid

kill %n kill job n

kill -9 %n definitely kill job ns

stop %n suspend background job n

wc file count lines, words, and characters in file

sort file sort file in usual order

sleep n sleep n seconds

command & running in background

ps obtain process status

mkidr dir make directory

mv fi dir move file fi to directory dir

cd dir change to directory dir

cd change to home directory

rmdir dir delete directory

pwd print working directory path

cal m yr show calendar for month m in year yr

cal yr show calendar for year yr

ls list files in working directory

cat fi show contents of file fi

more fi show contents of file fi, one screen at a time

pg fi similar to more

cp fi1 fi2 copy file fi1 to file fi2

mv fi1 fil2 move file1 to asked

rm fi delete file fi

chmod spec fi change permission status of file fi

passwd change your password

date print current date

w check who is on the system

man comm check the manual for command comm

logout logout system

exit logout system

^d logout system

(s)ftp File transfer (upload and download)

telnet remote login

ssh Secure remote login

Text Editor – Vi (or Vim)

To invoke vi• $ vi fname

Unix shell

Comm mode

Insert mode

vi fname

i, oesc

:x

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

Why choose “i”, “h”, “k”,.... ? ---Nearby finger tips

Left index finger

right index finger

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

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)

Spell checking• $ spell fname• $ ispell fname

Search • / pattern --- find occurrence of pattern• / <return> --- repeat last find• ? pattern --- works like /, but backwords• ? <return> --- works like /, but backwords

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

command line editing using vi• % bindkey –v (tcsh shell)• $ set –o vi (bash )• ex:

• $ echo I havee mad a mistake

press <esc> to enter vi mode move the cursor to “havee” to fix it

press <return> to execute the command

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

bus

ring

star

hub

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

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

hostsubnet

organizationTop-level domain

• 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

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

• ex: finger xing

Common Mail Conventions• :-) -- smile• ;-) -- wink• :-( -- frown

(26) xing@eclipse> finger xingLogin name: xingDirectory: /home/xing Shell: /bin/tcshOn since Feb 17 05:49:55 on pts/2 from ip68-11-69-123.no.no.cox.netNew mail received Thu Feb 16 11:47:38 2006; unread since Tue Feb 14 15:20:30 2006

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

• 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.gov

ftp.cac.psu.edu

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’ < inputFile > outFile

Octal for ^M

Input file nameOutput file name

• How to find octal for other control characters?• In vi insert mode, type <ctrl-v>, then type the

<ctrl-character>, 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.unix-manuals.com/refs/misc/ascii-table.html

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)?

• 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?)

clientserver

request

answer

• 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.

File Compression and Archive• Common file compressions

Compression Decompression Suffix exampe

compress uncompress .Z file.Z

gzip gunzip .gz file.gz

pkzip pkunzip .ZIP file.ZIP

tar tar .tar file.tar

• $ 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

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.

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!

plaintextciphertext

ciphertextplaintext

A

A

alg

algInternet

key

key

/R&

/R&

secret-key encryption

sender

receiver

plaintextciphertext

ciphertextplaintext

A

A

alg

algInternet

public key

private key

/R&

/R&

sender

receiver

public-key encryption

plaintextciphertext

ciphertextplaintext

A

A

alg

algInternet

private key

public key

/R&

/R&

public-key authentication

sender

receiver(s)

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?)

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 <hostname>

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

ssh-keygen screen shot

• 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

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)

(or, newer)

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)

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)

Click here

Check here

Click here

Click here to locate pk

(or, newer)

(or, newer)

WULA!!! WULA!!!!

I can relax now.

(or, newer)

WULA!!!!Again!.

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.

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.

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.

Now, relax and enjoy state-of-the-art security, the Turing-award-winning RSA!

Wait a minute…

Quantum computing can break it!!!

what ???

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

• 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

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

• 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)

• 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

• 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

• 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)

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

Common Environment Variables

HOME Pathname of your home directory

PATH Directories where shell looks for commands

SHELL Pathname of shell

TERM Your terminal type

MAIL Pathname of system mail

USER Your user name

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

• 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)

• But csh and tcsh also have their own special variables (e.g. home) (all lower-case)

pwd pathname of current working directory

history size of history list

home pathname of home directory

path dirs where shell looks for commands

term terminal type

shell pathname of shell

user your user name

prompt current prompt symbol

• 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 <var> = <value>

User-defined Variables• To define your own variable, in .login (and .cshrc) add: set <your-var> = <value> ex: set web = /home/xing/public_html (note the usage difference between set and setenv)

• 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

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)

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 #

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?

• 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!!

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/<shell> <shell>=sh, csh, tcsh,…

• 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!

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

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

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

• run this script with (shs0 is the filename of the script)

% shs0 i like it

$0 $1 $2 $3

arguments

• 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:” $*

• 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

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

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

Labeling output from wc• $ wc <file>

gives the number of lines, number of words, and

number of characters in, and the name of <file>• ex

$ wc lab1

5 17 84 lab1• want to write a script “mywc”, s.t.

$ mywc lab1

shows:

File: lab1

Lines: 5

Words: 17

Characters: 84

How can we do this?

• #!/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”

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• <var>=<value> (no space in between)• Ex: a=“this is a string” b=$a echo $b # prints “this is a string”

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:” $*

The expr Utility• syntax : expr arg1 <op> arg2

<op> 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.

• #!/bin/sh # add two numbers

sum=`expr $1 + $2` echo “result is: “ $sum

$ add 3 4result is: 7$ add 4.9 4.3----error (integer value expected)

if statement• Syntax

if cond then commands fi

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

Arguments of test command

-d file file is a directory

-f file file is a file

-r file file is readable

-s file file size > 0

-x file file is executable

-w file file is writable

! -d file file is not a directory

! -f file file is not a file

! -r file file is not readable

! -s file file size not > 0

! -x file file is not executable

! -w file file is not writable

n1 –eq n2 n1 = n2

n1 –ge n2 n1 >= n2

n1 –gt n2 n1 > n2

n1 –ne n2 n1 \= n2

n1 –lt n2 n1 < n2

n1 –le n2 n1 <= n2

n1, n2 are integers

s1 = s2 s1 equals s2

s1 != s2 s1 is not equal to s2

s1, s2 are strings

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

Remove files

#!/bin/sh # file del # delete files interactively

if test ! –f $1then echo “no such file” $1else 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 fi

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

For-loops• Syntax

for var in list

do

commands

done

Ex• #!/bin/sh

for name in $*

do

finger $name

echo *********

done

Ex:

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

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

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

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

C

Overview

The simplest “hello world” program

# include <stdio.h> main() { printf("Hello World\n"); }

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

Understanding the program

# include <stdio.h> 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()

A variation, what would be the output?

# include <stdio.h> main(){ printf("Hello\nWorld”); }

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

scanf()

Format (conversion char)

To what?

c a character

d a decimal integer

f a floating point number (float)

lf a floating point number (double)

s a string

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

printf()

Format (conversion char)

how to print the arguments?

c as a character

d as a decimal integer

f as a floating point number

e as a floating point number in scientific notation

s as a string

Fundamental Data Types

int (long) float double (long double) char (boolean? String?)

Identifiers

rule: {letter | _ }1{letter | _ |digit}0+

ex: a, _a, month_of_year

but not

not#me

101_n

-plus

declaration of variables

syntax:

type var1, …, varn;

ex: int x, y;

double x1;

int x, y=3;

Basic operations/operators

() + (unary) - (unary) ++ -- * / % + (binary) - (binary) = += -= *= /=

Assignment statement

Java is the same as C ex: a = 3;

x = x + 1;

etc…

++ and --

increment / decrement operator ++a and a -- different

ex:

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.

sizeof () operator

gives the size (in byte) required in memory for fundamental types

ex: (next slide)

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,

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

suggested.

ex:

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)

Relational , equality, and logical operators

< > <= >= == != ! && ||

same meaning as in Java false = any zero-value

(0, 0.0, or NULL, …) true = any non-zero value

ex:

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?

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

more about what typing can cuase

if (a = 1)

………… /* do sth */

if (a == 1)

….. /* do sth */

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

Selections

if-statement syntax

• if (expression)

statement ex:

if ( a == 1)

x = y;

if-else statement syntax

• if (expression)

statement

else

statement

ex:• if ( x == y)

{

i =1; j =2;

}

else

{ i = j;

}

ex: is the following code syntactically correct?

if (i == j)

{ i = i+1;

j = j+2;

};

else

i = j-1;

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;

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

Is this else-if statement?

if (….)

printf(…);

else if (…)

printf(….);

while-loop

syntax:• while (expression)

statement

usage:

the as Java

for-loop

same as Java syntax:

• for (expr1; expr2; expr3)

statement

next statement meaning interpreted in terms of while-

loop

expr1;

while (expr2)

{

statement;

expr3;

}

ex:

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.

ex:

functions (modular programming)

#include <….>

function prototypes (optional )

function defs

main ()

{…..}

ex:

functions

recursive functions

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’

example of strings• “abcde” is stored as

‘a’ ‘b’ ‘c’ ‘d’ ‘e’ ‘\0’

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)

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

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.

example

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

Now, why does it not work?

type synonym : typedef

give a new name for existing types: ex:

• typedef char * string;• typedef int number;

• string s1;• number n;

structure (or record)

syntax• struct name

{

type field;

…..

}

name is optional

ex:

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

accessing members of a structure• use the “.” operator

structure_var . member

or use the “->” operator

ptr_to_structure -> member

or, equivalently

or, (maybe better)

or, w/ pointers

or, w/ header file

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

then, to have better moularity

Use structure w/ functions

top related