Unix, C Programming, and (X)HTML · 2019. 10. 24. · Unix-like systems: systems that work much...
Transcript of Unix, C Programming, and (X)HTML · 2019. 10. 24. · Unix-like systems: systems that work much...
Unix, C Programming, and
(X)HTML
Instructor:
Dr. Cong-Cong Xing
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.
but in computing (esp. in terms
of storage)
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 locations
(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, officially) • 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.
kernel File sys
utilities shell user
what is shell (programming)?
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) (east coast)
• BSD Unix (%): modified at Berkeley and as
popular as AT&T Unix. (MacOS X is based on
BSD Unix) (west coast)
• 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 (X11)
• A GUI for Unix, developed at MIT (1984)
• 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
• (Is windowing the original Microsoft’s idea?)
Classic example of X window
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 (recommend))
Getting Started
Use putty ssn or telent to login to eclipse
an old UNIX
server of our
dept.
Login to eclipse.nicholls.edu
Successful login shows:
login to web1.nicholls.edu
Telnet to server
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
Some useful commands
Shell prompt • $ -- Bourne shell, Korn shell, or bash shell
• % -- C shell or TC shell
• Will use $ throughout the slides
Script session, record whatever occurred • $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, monitor, terminal, …)
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
/
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
• three different ways to go back to your home:
• cd
• cd ~
• cd ~cxing
what does this
possibly indicate?
Relative pathnames
• relative to working directory
• . -- the current working directory
• .. -- parent of current working directory
• (Windows use the same thing)
• 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 from 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
i-node
hard link to
name1
hard link to
name1
symbolic link to
name1
deletion of name1
will have no effect
on name2 (or 3), but
does affect name4
ts.txt is a
symbolic
link to t.txt
after t.txt
is
removed,
the
symbolic
is “broken”
the file dsf has
been removed.
dsf-link is ok,
but not dsf-s-
link
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
drwxr-xr-x 2 xing staff 512 Jan 23 11:40 test
File
type
Access
control
links
(hard)
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
l means (soft) link
• 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 (no space ..w,g…)
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
111 111 111
rwxr--r-- = 744
111 100 100
rw-rw---- = 660
110 110 000
--------- = 000
000 000 000
chmod 777 filename
chmod 442 filename
Conversion between binary and
decimal numbers
1112 = 1 × 22 + 1 × 21 + 1 × 20 = 710
1102 = 1 × 22 + 1 × 21 + 0 × 20 = 610
11102 = 1 × 23 + 1 × 22 + 1 × 21 + 0 × 20 = 1410
6
2= 3 𝑟 0
3
2= 1 𝑟 1
1
2= 0 𝑟 1
610 = 1102
14
2= 7 𝑟 0
7
2= 3 𝑟 1
3
2= 1 𝑟 1
1
2= 0 𝑟 1
1410 = 11102
proof that this conversion
scheme always works?
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
• $ page fname
-- similar to more
Concatenating Files
• $ cat file1 file2 ….
-- displays 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 (maybe) at the command line prompt right
after login, if not
• Check what your login shell is
• $ ps
• Or, read contents of /etc/passwd
• Ex: $ grep cxing /etc/passwd
• Or, $ echo $SHELL
• Or, $ echo $0 (current 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)
• $ chsh (follow instructions)
• $ chsh –s /bin/tcsh cxing (follow instructions,
change to tcsh shell)
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
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 to which programs
send output. (monitor by default)
• standard error: the file to which 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 whatever in/from stdin to stdout, and to
file at the same time
What entered w/
return (stdin)
(response to
input : stdin is
sent to stdout .
^d to finish)
the file ss has a copy of stdin
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
ex: nl – add line-numbers
cygwin – a Unix-
like environment
in Windows
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 running
process
Suspended
process
Bg running
process
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(pid)
bring a foreground process to background
bring a background process to foreground
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) id@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 WinSCP, filezilla
• open My Computer | type hostname in the address
bar
• open a web browser, type the hostname in the
URL bar
Free FTP software
• WinSCP
• https://winscp.net/eng/download.php
• filezilla
• https://filezilla-project.org/
WinSCP
Free Unix shell account?
Free Unix account: sdf.org
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 lines of 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
Example of real-world state-of-
the-art research (NFV-SONATA)
Text Editor – Vi (or Vim)
To invoke vi
• $ vi fname
Unix shell
Command
mode
Insert mode
vi fname
i, o esc
: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”, “j” ,“k”,
“l”,.... ? ---Nearby finger tips
Left index
finger
right index
finger
• x ---- delete one character
• dd --- delete entire line
• 0 (zero) – move cursor to the start of a line
• $ -- move the cursor to the end of a 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 current line w/ the next line together
• :w fname --- save file to fname
• “copy & paste” – yank and put • (do the following in command mode)
• ma (at the beginning of block, mark)
• y'a (at the end of block, yank)
• p (put what being yanked, put)
Spell checking (may be unavailable…)
• $ spell fname
• $ ispell fname
Search
• :/ pattern --- find occurrence of pattern
• :/ <return> --- repeat last find
• :? pattern --- works like /, but backwards
• :? <return> --- works like /, but backwards
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
Networking
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
host subnet
organization
Top-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 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
Classic Unix Network Utilities
• rlogin -- remote login
• telnet – remote login
• ftp – file transfer (protocol)
• rcp -- remote copy
Basic ftp commands (putty has sftp – psftp)
• ? (or help) -- 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 or ^[ at the end of each
line when displayed in Unix.
• How to fix it? Type the following
• tr –d ‘\015’ < inputFile > outFile
Octal for ^M
CR (carriage return) Input file name Output file
name
‘\015’ can be
replaced by ‘\033’
(octal for ^[ (ESC))
• 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 ( option –a to see name, not otcal)
• 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?)
client server
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 (tape
archive)
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
ZIP files w/ password protection
make sure zip is installed on your OS
ZIP files w/ password protection
on my Mac OS
Computer Security
Cryptography
• Plaintext: original unencrypted text.
• Ciphertext: encrypted text.
• Algorithm: transform plaintext into ciphertext
(and vice versa). (need MATH here!)
• Key: info, such as a word, a phrase, required
to encrypt or decrypt a message.
Encryption scenarios
• Secret-key encryption (aka: symmetrical
encryption):
use the same (shared secrete) key to encrypt and
decrypt a message
How to send the
secret key over
the Internet?
Encryption scenarios
• Public-key encryption (aka: asymmetric
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 is used to encrypt messages.
• private-key is used to decrypt (public-key-encrypted)
messages.
• no other possibilities (this is the only way to work)
no need to send the
key over the Internet
Encryption scenarios
• Public-key authentication (prove you are who you
claim you are): client sends request to server asking
for access, server encrypts a challenging message
using the public key and return it to the client. If the
client is able to understand the encrypted message,
then it proves that it holds the associated private key.
plaintext ciphertext
ciphertext plaintext
A
A
alg
alg Internet
key
key
/R&
/R&
secret-key encryption
sender
receiver
plaintext ciphertext
ciphertext plaintext
A
A
alg
alg Internet
public key
private key
/R&
/R&
sender
receiver
public-key encryption
plaintext ciphertext
ciphertext plaintext
A
A
alg
alg Internet
private key
public key
/R&
/R&
public-key authentication
client
server
RSA RSA RSA
RSA (the 3 MIT people) • 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
• ready 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!.
WULA!!!!
the 3rd time!
or, unfortunately,
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.
How does SSH work?
Symmetric keys are used by SSH in order to encrypt
the entire connection. Contrary to what some users
assume, public/private asymmetrical key pairs that
can be created are only used for authentication, not
the encrypting the connection. The symmetrical
encryption allows even password authentication to
be protected against snooping.
How does SSH work?
After the symmetrical encryption is established to
secure communications between the server and
client, the client must authenticate to be allowed to
access the server. The server can use the public key
in the relevant file to encrypt a challenge message to
the client. If the client can prove that it was able to
decrypt this message, it has demonstrated that it
owns the associated private key. The server then
can set up the environment for the client.
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 your login shell
TERM Your terminal type
MAIL Pathname of system mail
USER Your user name
HOST The server’s name (or ip address)
Setting Environment Variables (for the current
session)
• 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 (under csh/tcsh) • % set
• To set value of special variable (under csh/tcsh) • % 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 define your own variable, in .profile add:
<your-var>=<value>
ex: web=/home/xing/public_html
• 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 shell
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 of the script, $1 holds the first argument, S2 the second argument, $3 the third argument, … following the script file name
• 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 changes a file’s executable mode.
#!/bin/sh
# change a file’s executable mode
chmod u+x $1
echo $1 ‘can be executed now’
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
command 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 assignment)
• Ex: a=“this is a string”
b=$a
echo $b # prints “this is a string”
Read statement • stores console input 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 4
result is: 7
$ add 4.9 4.3
----error (integer value expected)
if-then statement • Syntax
if cond
then
commands
fi
ex:
• #!/bin/sh
set `date`
if test $1 = Fri
then
echo “Wula, weekend”
fi
test command compares values and checks
file types, and sets the (internal) exit status
depending on result
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 $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
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 over the arguments
• $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
part of a
shell script
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()
As 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: (formatted print)
• 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
for more info: search online C manual/tutorial
width and
precision
positions taken by
precision is included in
width
- will left-justify,
the default is
right-justify
Fundamental Data Types
int
(long)
float
double
(long double)
char
(boolean? String?)
no boolean type
• 0 is “false”,
anything else is
“true”
no string type
• string is char array
• ex: char s[10];
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;
declaration and
initialization done in
“one step”
Basic operations/operators
()
+ (unary) - (unary) ++ --
* / %
+ (binary) - (binary)
= += -= *= /=
(Q: should negative symbol and subtraction
symbol be distinguished in math?)
Assignment statement
Java is the same as C
Syntax
• <var> = expression;
ex: a = 3;
x = x + 1;
etc…
C is not “type safe”
compiler will not
complain, but you
will see a run-time
error.
++ and --
increment / decrement operator
++a and a++ are different
ex:
When a++ or ++a are not alone, the
effect of the difference will be shown
• First all of, the expression a++ or ++a will be
evaluated (to yield a value)
• then, the yield value (i.e. evaluation result) will
be used in the surrounding context (since a++
or ++a are not “alone”)
so, ++a causes a to be increased by 1
first, and the value of the expression ++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 (or, don’t use it)
Quick test using Java
interaction tab
the expression a++ is
being evaluated, and
the result is 1
the expression ++a is
being evaluated, and
the result is 2
A quick question: what is the
difference between a=1 and
a=1; in Java (and C)?
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 + (as in a + b) with
= (as in a = b)
a + b is an expression, so is a = b
a + b has a value, so does a = b
a + (b + c) makes sense,
so does c = (a = b)
but , this style of programming, is not
suggested.
ex:
And, Java follows it
a=b is an expression (and thus will be
evaluated to a value)
a=b; is not an expression (and will not
be evaluated to a value)
this example
should drive the
point to home
char and int (C is not strongly
typed)
character can be considered as int
and vice versa (see next slide)
note: not saying int is the same type as
char (4 bytes vs 1 byte). We cannot
declare a variable as type int and char at
the same time
again, this style of programming is not
suggested (it is 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 are logical operators in C (!,
&&, ||)
So, what is the response to the following
expression in C (and Java)?
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 (so
boolean type is needed)
true in C
false in C
“strange” things ...
In Java, it’s a different story…,
due to STRONG typing in Java
More about what typing can
cause ...
while/if (a = 1)
………… /* do sth */
while/if (a == 1)
….. /* do sth */
both are syntactically correct in C
the first one will run forever, and thus
cause a perplex...
the second one is what we want..
Java won’t compile something
like this
this shows typing is
important !!!
Selections
if-statement
syntax
• if (expression)
statements
ex:
if ( a == 1)
x = y;
if-else statement
syntax
• if (expression)
statements
else
statements
ex:
• if ( x == y)
{
i =1; j =2;
}
else
{ i = j;
}
ex: is the following code syntactically
correct? if not, what is wrong?
if (i == j)
{ i = i+1;
j = j+2;
};
else
i = j-1;
which “if” does the “else” go w/?
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 left one is will be “chosen”.
but, what if we intend to do the other
one?
what if we want to do the “right”
one
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;
}
Is this else-if statement?
if (….)
printf(…);
else if (…)
printf(….);
while-loop
syntax:
• while (expression)
statements
usage:
the same as Java (Java follows C)
for-loop
same as Java
syntax:
• for (expr1; expr2; expr3)
statements
next statement
meaning interpreted in terms of while-
loop
expr1;
while (expr2)
{
statements;
expr3;
}
next statement
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:
what is going on in Java?
It will be evaluated … But, what
would be the type of evaluation
result?
what would be the type
of the expression????
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; or
*p is of type int.
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 doesn't it work?
same thing as before …
this
(changing
p to &p) is
not the
problem.
It is
supposed
to be p (a
pointer).
to fix it:
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
HTML
HTML Basics
HTML – Hypertext Markup Language
Background
• Who: Tim Berners-Lee
• Where: CERN, Swiss
• When: 1989
• http://public.web.cern.ch/Public/Welcome.html
Used to write WWW (web) pages
Structure of a HTML document • <html>
<head><title> …..</title></head>
<body>
…………….
…………..
</body>
</html>
• Note:
• A html file is a plain text file
• <..> -- opening tag
• </..> -- closing tag
• Opening tags are paired with corresponding
closing tags (not so strict)
• Place the html file in the public_html directory on
eclipse
• an example (next page)
Type styles • Tags to typeset documents
• Heading style • <H1> largest and boldest
• <H2>
• …
• <H6>
• <cite> -- citation
• <em> -- emphasis
Down in size
• <strong> -- bold
• <code>, <kbd> -- monospaced font
• <address> -- italic, email, phone #, address
• <big> -- big font
• <small> -- small font
• <sup> -- superscript
• <sub> -- subscript
Ex: <H1> Look Here </H1>
Look Here
B<sup>2</sup>
B2
Horizontal alignment of text
• Syntax:
• <p> makes a paragraph
• <p align = x > …..</p>
x = left, right, center
• <center> ……….</center> (will center and make
a paragraph)
• Ex: <p align=right> this line </p>
Comments (which one, confusing) • <!-- ……… -->
• <! …… >
• Def: • Comment declaration: <! … >
• Comment contents: start with -- end with -- contain no occurrence of –
• A comment is: comment declaration which contains zero or more comment contents
• So <!>, <!----> are all legal comments
• But <!-----> is not
• Unfortunately, not many browsers coded to recognize it.
Font sizes
• Syntax: <font size=x> … </font>
• x can be 1,2,…, 7
• From small to large
• Default size = 3
• Ex: <font size=5> large font </font>
Fixed type styles • Unlike relative style tags (e.g. <h1>), fixed type styles do
not vary from one browser to another.
• style tag
bold <b> </b>
italic <i> </i>
underscore <u> </u>
typewriter <tt> </tt>
strikeout <s> </s>
Line and paragraph breaks
• <br> ends a line w/o adding any space
• <p> ends a line and adds a line space
• <nobr> </nobr>
prevents texts and graphics from
breaking and reflowing
Text links
• <a href=“…...... “> …….…</a>
anchor
Hypertext ref name of link
text to click
ex:
• <a href=“art.html”>click here</a>
• <a href=“lab1.txt”>click here</a>
• <a href=“http://www.nicholls.edu”>click here</a>
• <a href=“mailto:[email protected]"”>click here to mail
me</a>
Graphics
• <img align=x src=“….” width=“..” height=“…”>
• ex: <img src=“clips.gif”>
optional name of pic
x= top, bottom, middle
• Border on graphics
• <img src=“……” border=“…”>
• Graphic links
• <a href=“…..”> <img src=“…..”> </a>
pixels
Name of
resource picture
Background color, text color, and link color • <body bgcolor=“#302030” text=“#246050”
link=“#505050”>
• https://html-color.codes/
• color name(“red”), hex-code (“#ffffff”), rgb-code(“(0,34,0)”)
• List • unordered list
• <ul type=x> x = circle or square w/wo “”
<li> …
<li> ….
</ul>
optional
• Ordered list • <ol type=x> x=1,A,a,i
<li> ….
<li> …..
</ol>
Dividing rules • <hr>
• <hr size=“…” width=“…” align=x>
x= left, right, center
height length
Links within documents
• <a href=“#....”> …. </a>
• <a name=“…”>….</a>
• Ex: <a href=“#comp”>computer</a>
<a name=“comp”> computer def </a>
text
text same
Block quote • <blockquote>
….
</blockquote>
Background (design) • <body background=“…..”>
Preformatted text • <pre>
………….
</pre>
indented
Graphics file
Exact appearance
Example
• Using what we have covered so far, we can
compose the following webpage. (next slide)
Table • <table border>
<caption align= … > ……</caption>
<tr>
<th> … </th> <th> … </th>
</tr>
<tr>
<td>….</td> <td>….</td>
</tr>
</ table>
Top or bottom caption
row Heading cell
Table data
Optional (show or not show border)
Borders, spacing, padding
• <table
border=10
cellspacing=6
cellpadding=10>
Width of frame
Space between 2 cells
Space between text in a cell
and cell gridline
Alignment
• Put align=x (x=left, center, right) in
<th>, <td>, or <tr>
• Ex: <tr align=center>….. </tr>
whole row is centered
<td align=left>…..</td>
only this cell is aligned to left
Width control
• <table width=400>
specifies the width of the table
• <th width=50>
specifies the width of the cell
Spanning rows and/or columns
• <th rowspan=2> …..</th>
• <th colspan=2> …..</th>
• <td rowspan=2> …..</td>
• <td colspan=2> …..</td>
Forms
The <form> element • <form action=“…” method=“…”>
put form elements here
</form>
Input texts into forms • Using the <input> element: Generate various
ways for users to enter info into forms • <input …..some attributes…… >
• Common attributes: type, name, value
• Using the <textarea> element: allows a
paragraph of texts to be entered
• <textarea …some attributes….. >
some default text
</textarea>
Input selections
• Checkbox
• <input type=“checkbox” name=…. value=...>
• Radio button
• <input type=“radio” name=… value=...>
• Drop-down list
• <select name=…size=…>
<option value=…> ….</option>
• </select>
# of lines visible
Submitting and clearing forms
• <input type=“submit”>
• <input type=“reset”>
Generate a submit button and a reset button
respectively
Form Processing -- CGI
CGI programs– Common Gateway Interface
programs. They are used to process forms
CGI programs can be written in any language
supported by the Web server, e.g., C, C++,
Java, Perl.
CGI programs are installed on server and
specified by the action attribute of the form tag.
Ex:
• <FORM METHOD="POST"
ACTION="http://web.mit.edu/bin/cgiecho/wwwdev
/cgiemail/questions3.txt">
• <FORM METHOD=“get" ACTION=“./cgi-bin/test.pl”>
• “get” appends data from form to the end of URL in the
resulting window; “post” does not do so and merely
send form data to the Web server to be processed.
MIT cgiemail program
Free CGI program for Unix
Easy to use
http://web.mit.edu/wwwdev/cgiemail/
It collects data from a form and sends
them to a specified email address in a
specified format
Steps to make cgiemail work on
our Unix
Go to cgiemail website, download binaries cgiecho and cgiemail to your cgi-bin directory, say, /home/xing/public_html/cgi-bin.
Rename them as cgiecho.cgi and cgiemail.cgi
Change file mode of these two files (chmod 755 ….)
Create an email template (an ASCII file) which specifies the format of the email which will be sent to you (or someone else) by the web server.
Here is an example of the email template
From: [email]
Subject: questions three
What is your name? [yourname]
What is your quest? [quest]
What is your favourite colour? [colour]
This allows the form data to be sent to cmps-
[email protected] w/ sender being identified as [email]
Put this file (say file name is quest3.txt) into your public_html directory,e.g., /home/xing/public_html
Test this file with your browser to make sure it is readable.
Create a html form in your public_html directory,say, /home/xing/public_html
An example of the html file is as follows: note that the values of the name attributes MUST be the same as that in the email template
<html>
<head><title> form test</title></head>
<body>
<FORM METHOD="get"
ACTION="http://eclipse.nicholls.edu/~xing/cgi-bin/cgiecho.cgi/~xing/quest3.txt">
Your e-mail address: <INPUT NAME="email"><p>
Your name: <INPUT NAME="yourname"><p>
Your quest: <INPUT NAME="quest"><p>
Your favourite colour: <INPUT NAME="colour"><p>
<INPUT TYPE="submit" value="click to see response">
</FORM>
</body>
</html>
Set the value of action of the form tag to be:
ACTION=http://eclipse.nicholls.edu/~xing/cgi-bin/cgiecho.cgi/~xing/quest3.txt to echo the form data (no email will be sent) or
ACTION=http://eclipse.nicholls.edu/~xing/cgi-bin/cgiemail.cgi/~xing/quest3.txt to send form data as an email
Save the file in your public_html directory, say, /home/xing/public_html/myform.html
Open myform.html using your favorite browser
The email template
The html form file
Executing the html form file
Click it
Response from clicking the
button
What is in my mail box
XHTML
Overview
SGML
HTML
XML
XHTML
SGML • Standard Generalized Markup Language
• Widely used for large-scale documentation project (IRS, DoD, Health care…)
• 1986
HTML • Hyper Text Markup Language
• First language for WWW
• Early 1990
• Limitations (for example)
• Loose syntax, (“” or no “”, close tag or not?,
lowercase or capital?...)
• web browser forgiving, but not new wireless
devices (cell phone, PDA, car navigator…)
• Can’t be extended
XML • Extensible Markup Language
• 1998, by W3C (WorldWideWeb Consortium)
• Motivated by the limitations of HTML
XHTML
• Combination of XML and HTML
• 2000, XHTML 1.0
Converting HTML to XHTML
All mark elements must be lowercase • Ex: HTML: <TITLE> … </TITLE>
XHTML: <title> … </title>
Every open tag must correspond to a close tag • Ex: HTML: <ol>
<li> item1
<li> item2
</ol>
XHTML: <ol>
<li> item1 </li>
<li> item2 </li>
</ol>
Empty-element syntax • Ex: HTML: <BR>
XHML: <br />
Every attribute must have a value. The value needs to be surrounded by single or double quote marks • Ex: HTML: <input type=button checked>
XHTML: <input type=“button” checked=“checked” />
Proper nesting of elements is required • Ex: HTML: <p><em>…</p></em>
XHTML: <p><em> … </em></p>
All XHTML documents must contain DOCTYPE declaration and may, optionally, contain an XML declaration • <?xml version="1.0" encoding="UTF-8"?>
• <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
• "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
The root element <html> must contain
an XHTML namespace
• <html xmlns="http://www.w3.org/1999/xhtml">
Three types of DTD (Document
Type Definition)
Transitional
Frameset
Strict
Replace any by another in DOCTYPE
declaration
example
Validation of XHTML documents
Go to http://validator.w3.org
Submit your document
Example: submitting the previous
sample xhtml document
Introduction to
JavaScript
Background
In early 1990, “www” so “hot” that everyone was talking about “.com”
In 1995, Netscape (Brendan Eich) decided to script the HTML, the scripting language was called LiveScript
Netscape and Sun worked together to implement the scripting language into Netscape Navigator 2.0. The name changed from LiveScript to JavaScript (for marketing purpose)
Microsoft followed this idea, released Jscript the next year
JavaScript vs. Java
JavaScript ≠ Java
JavaScript : loosely typed
Java: strongly typed.
JavaScript: tied w/ web programming
Java: general purpose
Basic Structure
…..<head> ……
<script type="text/javascript" >
js code here
</script>
…………..
</head>
<body>
…………
<script type="text/javascript" >
js code here
</script>
………..
</body>
The <script>…</script>
can be inserted at
different places
or
…..<head> ……
<script type="text/javascript" src=…a-js-file…>
js code
</script>
…………..
</head>
<body>
…………
<script type="text/javascript“ src=…a-js-file…. >
js code
</script>
………..
</body>
JS Syntax and Semantics
Similar to that of Java (skip)
Ex:
• for (i=0; i<10; i++)… for-loop
• while (i < 10)… while-loop
• var a; variable declaration
• a =1; assignment statement
• if (a <=b)… branching
example: loop
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> js test </title>
<script type="text/javascript" >
var i;
for (i=1;i<=10;i++)
document.write("do it"+i+"time <br />");
document.write("<h1> stop the loop! </h1>");
</script>
</head>
<body>
<p>
<a href="http://validator.w3.org/check?uri=referer">
<img
src="valid-xhtml10.gif"
alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a>
</p>
</body>
</html>
Or, put the js into another file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> js test </title>
<script type="text/javascript" src=“./jsfiles/x.js”>
</script>
</head>
<body>
<p>
<a href="http://validator.w3.org/check?uri=referer">
<img
src="valid-xhtml10.gif"
alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a>
</p>
</body>
</html>
path of the file x.js
example: loop
// the x.js file
var i;
for (i=1;i<=10;i++)
document.write("do it"+i+"time <br />");
document.write("<h1> stop the loop! </h1>");
It will do the same thing
example: branching
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> js test </title>
<script type="text/javascript" >
var answer = window.prompt("who are you? Type your name here", "???");
if (answer == "John")
window.alert("Hello, John");
else
window.alert("fatal error! memeory dump! Shut down the system!");
</script>
</head>
<body>
<p>
<a href="http://validator.w3.org/check?uri=referer">
<img
src="valid-xhtml10.gif"
alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a>
</p>
</body>
</html>
example: branching
example: function (rewriting of
previous example)
<head>
<title> js test </title>
<script type="text/javascript" >
function doIt(x)
{
if (x == "John")
window.alert("Hello, John");
else
window.alert("fatal error! memeory dump! Shut down the system!");
}
</script>
</head>
<body>
<script text="text/javascript">
var answer = window.prompt("who are you? Type your name here", "???");
doIt(answer);
</script>
<!– the logo here -->
</body>
</html>
Note:
Browsers load (x)html codes, from top to
bottom, into memory
Common practice: put functions inside
<head></head> element so that the
browser “knows” the functions before
they are invoked.
Check Boxes
Purpose: make multiple (one or more)
choices
How to make checkboxes?
How to check which boxes are checked?
Ex: (2 approaches, both fine)
<script type="text/javascript" >
function showIt(f)
{
f.result.value=""; // reset the result value
if (f.food0.checked)
{ f.result.value="steak"; }
if (f.food1.checked)
{ f.result.value=f.result.value+",seafood"; }
if (f.food2.checked)
{ f.result.value=f.result.value+",vege"; }
}
</script>
<form name="myform">
Select the ones you like: (all that apply) <br />
<input type="checkbox" name="food0" /> steak <br />
<input type="checkbox" name="food1" /> seafood <br />
<input type="checkbox" name="food2" /> vege <br />
<input type="button" value="click to see your choice(s)" onclick="showIt(document.myform);" /> <br />
<input type="text" name="result" value="" />
</form>
<script type="text/javascript" >
function showIt(f)
{
f.result.value=""; // reset the result value
if (f.food[0].checked)
{ f.result.value="steak"; }
if (f.food[1].checked)
{ f.result.value=f.result.value+",seafood"; }
if (f.food[2].checked)
{ f.result.value=f.result.value+",vege"; }
}
</script>
<form name="myform">
Select the ones you like: (all that apply) <br />
<input type="checkbox" name="food" /> steak <br />
<input type="checkbox" name="food" /> seafood <br />
<input type="checkbox" name="food" /> vege <br />
<input type="button" value="click to see your choice(s)" onclick="showIt(document.myform);" /> <br />
<input type="text" name="result" value="" />
</form>
Radio Buttons
Purpose: you can only choose ONE
choice, not allowed to choose more than
one.
How to create radio buttons?
How to check which button is selected?
Ex: make a choice and show what
choice is made.
<script type="text/javascript" >
function showIt(f)
{
if (f.food[0].checked)
{ f.result.value="steak"; }
else
{
if (f.food[1].checked)
{ f.result.value="seafood"; }
else
{if (f.food[2].checked)
{ f.result.value="vege"; }
else
{f.result.value="nothing is chosen";}
}
}
}
<form name="myform">
Make your choice: <br />
<input type="radio" name="food" /> steak <br />
<input type="radio" name="food" /> seafood <br />
<input type="radio" name="food" /> vege <br />
<input type="button" value="click to see your choice" onclick="showIt(document.myform);" /> <br />
<input type="text" name="result" value="" />
</form>
a better treatment might be the following:
function showIt(f)
{
var i;
for (i =0; i<3; i++)
{
if (f.food[i].checked)
{ f.result.value=f.food[i].value;
break;
}
}
}
Or the following:
function showIt(f)
{
alert("# of elements="+f.elements.length);
var i;
for (i =0; i<3; i++)
{
if (f.elements[i].checked)
{ f.result.value=f.elements[i].value;
break;
}
}
}
<form name="myform">
Make your choice: <br />
<input type="radio" name="food" value="steak" /> steak <br />
<input type="radio" name="food" value="seafood" /> seafood <br />
<input type="radio" name="food" value="vege" /> vege <br />
<input type="button" value="click to see your choice" onclick="showIt(document.myform);" /> <br />
<input type="text" name="result" value="" />
</form>
<!-- Note each <input> element has a value now. -->
You will see the same result
onmouseout
onmouseover
Swap to “3l” image every 3 seconds
Swaps to “logo” image every 3 seconds
Lab25 (last one)
Find two of your favorite pictures and
swap them every 2 second.
Print the screen shots and XHTML code
Problems w/ eclipse?