unix and c programming
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 PresentationTRANSCRIPT
![Page 1: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/1.jpg)
Unix and C Programming
Dr. Cong Xing
Dept. of Math & CMPS
![Page 2: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/2.jpg)
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
![Page 3: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/3.jpg)
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.
![Page 4: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/4.jpg)
symbol meaning roughly
Kilo K 210 (=1024) 103
Mega M 220 106
Giga G 230 109
Tera T 240 1012
Peta P 250 1015
![Page 5: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/5.jpg)
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
![Page 6: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/6.jpg)
• 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)
![Page 7: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/7.jpg)
• 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
![Page 8: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/8.jpg)
![Page 9: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/9.jpg)
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
![Page 10: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/10.jpg)
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.
![Page 11: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/11.jpg)
kernelFile sys
utilities shelluser
![Page 12: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/12.jpg)
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)
![Page 13: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/13.jpg)
• 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
![Page 14: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/14.jpg)
Example: Mac OS X
![Page 15: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/15.jpg)
X terminal in Mac OS X
![Page 16: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/16.jpg)
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
![Page 17: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/17.jpg)
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
![Page 18: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/18.jpg)
• 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)
![Page 19: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/19.jpg)
Getting Started
Use putty ssn or telent to login to eclipse
![Page 20: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/20.jpg)
Successful login shows:
![Page 21: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/21.jpg)
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
![Page 22: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/22.jpg)
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
![Page 23: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/23.jpg)
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
![Page 24: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/24.jpg)
• $ 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
![Page 25: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/25.jpg)
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
![Page 26: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/26.jpg)
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.
![Page 27: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/27.jpg)
• 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.
![Page 28: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/28.jpg)
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
![Page 29: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/29.jpg)
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 (/)
![Page 30: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/30.jpg)
• 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),
![Page 31: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/31.jpg)
• 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 (-)
![Page 32: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/32.jpg)
• 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
![Page 33: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/33.jpg)
root /
home
smith al
cmps math
hw1 hw2
![Page 34: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/34.jpg)
• ~ (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
![Page 35: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/35.jpg)
• 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)
![Page 36: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/36.jpg)
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)
![Page 37: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/37.jpg)
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
![Page 38: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/38.jpg)
• 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,
![Page 39: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/39.jpg)
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
![Page 40: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/40.jpg)
name1
name2
name3
name4
![Page 41: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/41.jpg)
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
![Page 42: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/42.jpg)
drwxr-xr-x 2 xing staff 512 Jan 23 11:40 test
File
typeAccess
control
links owner group Size
(byte)Date & time
name
![Page 43: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/43.jpg)
• 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)
![Page 44: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/44.jpg)
• 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)
![Page 45: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/45.jpg)
• = -- 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
![Page 46: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/46.jpg)
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)
![Page 47: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/47.jpg)
• more ex: rwxrwxrwx = 777
rwxr--r-- = 744
rw-rw---- = 660
--------- = 000
![Page 48: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/48.jpg)
Conversion between binary and decimal numbers
![Page 49: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/49.jpg)
6
![Page 50: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/50.jpg)
![Page 51: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/51.jpg)
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
![Page 52: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/52.jpg)
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
![Page 53: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/53.jpg)
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
![Page 54: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/54.jpg)
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)
![Page 55: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/55.jpg)
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
![Page 56: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/56.jpg)
Returning to the home directory• $ cd or• $ cd ~
Printing your working directory path• $ pwd
![Page 57: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/57.jpg)
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
![Page 58: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/58.jpg)
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)
![Page 59: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/59.jpg)
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
![Page 60: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/60.jpg)
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
![Page 61: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/61.jpg)
• 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
![Page 62: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/62.jpg)
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)
![Page 63: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/63.jpg)
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.
![Page 64: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/64.jpg)
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
![Page 65: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/65.jpg)
• ex: create a calendar for summer of 2007
with grouping
$ (cal 6 2007; cal 7 2007; cal 8 2007) > sum2007
w/o grouping ?
![Page 66: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/66.jpg)
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
![Page 67: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/67.jpg)
Tees
stdin
stdout file
tee file -- copies stdin to stdout, making a copy in file
![Page 68: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/68.jpg)
What entered (stdin)
(response to input : stdout ^d to finish)
![Page 69: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/69.jpg)
command1
outfile command2
$ command1 | tee outfile | command2 (two pipes, one tee)
Output from command1 goes to file outfile and to stdin of command2
![Page 70: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/70.jpg)
• 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
![Page 71: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/71.jpg)
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*
![Page 72: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/72.jpg)
• 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.
![Page 73: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/73.jpg)
• 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
![Page 74: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/74.jpg)
Ex:• $ echo what time is it\?• What time is it• $ echo what time is it`date`• what time is itMon Aug 13 ……
![Page 75: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/75.jpg)
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.
![Page 76: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/76.jpg)
• 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
![Page 77: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/77.jpg)
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
![Page 78: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/78.jpg)
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
![Page 79: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/79.jpg)
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
![Page 80: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/80.jpg)
• $ 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
![Page 81: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/81.jpg)
• 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
![Page 82: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/82.jpg)
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
![Page 83: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/83.jpg)
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
![Page 84: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/84.jpg)
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
![Page 85: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/85.jpg)
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
![Page 86: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/86.jpg)
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
![Page 87: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/87.jpg)
logout logout system
exit logout system
^d logout system
(s)ftp File transfer (upload and download)
telnet remote login
ssh Secure remote login
![Page 88: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/88.jpg)
Text Editor – Vi (or Vim)
To invoke vi• $ vi fname
Unix shell
Comm mode
Insert mode
vi fname
i, oesc
:x
![Page 89: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/89.jpg)
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
![Page 90: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/90.jpg)
Why choose “i”, “h”, “k”,.... ? ---Nearby finger tips
Left index finger
right index finger
![Page 91: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/91.jpg)
• x ---- delete one character• dd --- delete entire line• u --- undo most recent change• :q! --- quit w/o saving• :x --- quit w/ saving
![Page 92: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/92.jpg)
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)
![Page 93: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/93.jpg)
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
![Page 94: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/94.jpg)
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
![Page 95: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/95.jpg)
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
![Page 96: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/96.jpg)
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
![Page 97: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/97.jpg)
bus
ring
star
hub
![Page 98: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/98.jpg)
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
![Page 99: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/99.jpg)
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
![Page 100: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/100.jpg)
• 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
![Page 101: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/101.jpg)
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
![Page 102: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/102.jpg)
• 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
![Page 103: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/103.jpg)
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
![Page 104: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/104.jpg)
• 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
![Page 105: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/105.jpg)
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
![Page 106: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/106.jpg)
• 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
![Page 107: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/107.jpg)
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)?
![Page 108: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/108.jpg)
• 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
![Page 109: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/109.jpg)
• 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.
![Page 110: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/110.jpg)
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
![Page 111: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/111.jpg)
• $ 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
![Page 112: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/112.jpg)
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.
![Page 113: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/113.jpg)
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!
![Page 114: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/114.jpg)
plaintextciphertext
ciphertextplaintext
A
A
alg
algInternet
key
key
/R&
/R&
secret-key encryption
sender
receiver
![Page 115: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/115.jpg)
plaintextciphertext
ciphertextplaintext
A
A
alg
algInternet
public key
private key
/R&
/R&
sender
receiver
public-key encryption
![Page 116: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/116.jpg)
plaintextciphertext
ciphertextplaintext
A
A
alg
algInternet
private key
public key
/R&
/R&
public-key authentication
sender
receiver(s)
![Page 117: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/117.jpg)
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?)
![Page 118: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/118.jpg)
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>
![Page 119: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/119.jpg)
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
![Page 120: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/120.jpg)
ssh-keygen screen shot
![Page 121: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/121.jpg)
• 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
![Page 122: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/122.jpg)
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)
![Page 123: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/123.jpg)
![Page 124: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/124.jpg)
![Page 125: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/125.jpg)
(or, newer)
![Page 126: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/126.jpg)
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)
![Page 127: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/127.jpg)
![Page 128: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/128.jpg)
![Page 129: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/129.jpg)
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)
![Page 130: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/130.jpg)
Click here
![Page 131: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/131.jpg)
Check here
Click here
Click here to locate pk
![Page 132: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/132.jpg)
(or, newer)
![Page 133: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/133.jpg)
(or, newer)
![Page 134: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/134.jpg)
WULA!!! WULA!!!!
I can relax now.
![Page 135: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/135.jpg)
(or, newer)
WULA!!!!Again!.
![Page 136: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/136.jpg)
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.
![Page 137: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/137.jpg)
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.
![Page 138: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/138.jpg)
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.
![Page 139: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/139.jpg)
Now, relax and enjoy state-of-the-art security, the Turing-award-winning RSA!
Wait a minute…
Quantum computing can break it!!!
what ???
![Page 140: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/140.jpg)
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
![Page 141: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/141.jpg)
• 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
![Page 142: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/142.jpg)
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
![Page 143: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/143.jpg)
• 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)
![Page 144: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/144.jpg)
• 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
![Page 145: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/145.jpg)
• 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
![Page 146: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/146.jpg)
• 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)
![Page 147: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/147.jpg)
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
![Page 148: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/148.jpg)
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
![Page 149: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/149.jpg)
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
![Page 150: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/150.jpg)
• 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)
![Page 151: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/151.jpg)
• 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
![Page 152: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/152.jpg)
• 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)
![Page 153: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/153.jpg)
• 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
![Page 154: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/154.jpg)
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)
![Page 155: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/155.jpg)
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 #
![Page 156: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/156.jpg)
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?
![Page 157: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/157.jpg)
• 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!!
![Page 158: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/158.jpg)
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,…
![Page 159: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/159.jpg)
• 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!
![Page 160: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/160.jpg)
Shell as a programming language• Variables• Input/output• Arithmetic operations• Conditional expressions• Selection structures• Loop structures
![Page 161: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/161.jpg)
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
![Page 162: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/162.jpg)
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
![Page 163: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/163.jpg)
• run this script with (shs0 is the filename of the script)
% shs0 i like it
$0 $1 $2 $3
arguments
![Page 164: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/164.jpg)
• 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:” $*
![Page 165: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/165.jpg)
![Page 166: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/166.jpg)
• 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
![Page 167: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/167.jpg)
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
![Page 168: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/168.jpg)
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
![Page 169: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/169.jpg)
![Page 170: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/170.jpg)
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
![Page 171: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/171.jpg)
shows:
File: lab1
Lines: 5
Words: 17
Characters: 84
How can we do this?
![Page 172: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/172.jpg)
• #!/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”
![Page 173: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/173.jpg)
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”
![Page 174: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/174.jpg)
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:” $*
![Page 175: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/175.jpg)
![Page 176: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/176.jpg)
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.
![Page 177: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/177.jpg)
• #!/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)
![Page 178: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/178.jpg)
if statement• Syntax
if cond then commands fi
ex:• #!/bin/sh set `date` if test $1 = Fri then echo “Wula, weekend” fi
![Page 179: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/179.jpg)
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
![Page 180: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/180.jpg)
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
![Page 181: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/181.jpg)
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
![Page 182: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/182.jpg)
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
![Page 183: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/183.jpg)
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
![Page 184: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/184.jpg)
For-loops• Syntax
for var in list
do
commands
done
Ex• #!/bin/sh
for name in $*
do
finger $name
echo *********
done
![Page 185: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/185.jpg)
Ex:
#!/bin/sh b="1 2 3 4 5 6 7 8 9 10" for a in $b do echo "value is" $a done
![Page 186: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/186.jpg)
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
![Page 187: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/187.jpg)
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
![Page 188: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/188.jpg)
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
![Page 189: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/189.jpg)
![Page 190: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/190.jpg)
![Page 191: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/191.jpg)
C
![Page 192: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/192.jpg)
Overview
The simplest “hello world” program
# include <stdio.h> main() { printf("Hello World\n"); }
![Page 193: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/193.jpg)
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
![Page 194: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/194.jpg)
![Page 195: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/195.jpg)
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()
![Page 196: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/196.jpg)
A variation, what would be the output?
# include <stdio.h> main(){ printf("Hello\nWorld”); }
![Page 197: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/197.jpg)
![Page 198: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/198.jpg)
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
![Page 199: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/199.jpg)
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
![Page 200: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/200.jpg)
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
![Page 201: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/201.jpg)
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
![Page 202: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/202.jpg)
![Page 203: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/203.jpg)
![Page 204: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/204.jpg)
![Page 205: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/205.jpg)
![Page 206: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/206.jpg)
Fundamental Data Types
int (long) float double (long double) char (boolean? String?)
![Page 207: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/207.jpg)
Identifiers
rule: {letter | _ }1{letter | _ |digit}0+
ex: a, _a, month_of_year
but not
not#me
101_n
-plus
![Page 208: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/208.jpg)
declaration of variables
syntax:
type var1, …, varn;
ex: int x, y;
double x1;
int x, y=3;
![Page 209: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/209.jpg)
Basic operations/operators
() + (unary) - (unary) ++ -- * / % + (binary) - (binary) = += -= *= /=
![Page 210: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/210.jpg)
Assignment statement
Java is the same as C ex: a = 3;
x = x + 1;
etc…
![Page 211: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/211.jpg)
![Page 212: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/212.jpg)
![Page 213: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/213.jpg)
++ and --
increment / decrement operator ++a and a -- different
ex:
![Page 214: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/214.jpg)
![Page 215: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/215.jpg)
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.
![Page 216: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/216.jpg)
![Page 217: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/217.jpg)
sizeof () operator
gives the size (in byte) required in memory for fundamental types
ex: (next slide)
![Page 218: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/218.jpg)
![Page 219: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/219.jpg)
![Page 220: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/220.jpg)
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,
![Page 221: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/221.jpg)
so is c = (a = b) but , this style of programming, not
suggested.
![Page 222: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/222.jpg)
ex:
![Page 223: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/223.jpg)
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)
![Page 224: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/224.jpg)
![Page 225: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/225.jpg)
Relational , equality, and logical operators
< > <= >= == != ! && ||
same meaning as in Java false = any zero-value
(0, 0.0, or NULL, …) true = any non-zero value
![Page 226: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/226.jpg)
ex:
![Page 227: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/227.jpg)
![Page 228: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/228.jpg)
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?
![Page 229: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/229.jpg)
![Page 230: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/230.jpg)
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
![Page 231: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/231.jpg)
more about what typing can cuase
if (a = 1)
………… /* do sth */
if (a == 1)
….. /* do sth */
![Page 232: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/232.jpg)
both are syntactically correct the first one will run forever…. the second one is what we want..
![Page 233: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/233.jpg)
Selections
if-statement syntax
• if (expression)
statement ex:
if ( a == 1)
x = y;
![Page 234: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/234.jpg)
if-else statement syntax
• if (expression)
statement
else
statement
![Page 235: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/235.jpg)
ex:• if ( x == y)
{
i =1; j =2;
}
else
{ i = j;
}
![Page 236: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/236.jpg)
ex: is the following code syntactically correct?
if (i == j)
{ i = i+1;
j = j+2;
};
else
i = j-1;
![Page 237: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/237.jpg)
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;
![Page 238: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/238.jpg)
rule : the else matches with nearest if. So the first one is the “right” one.
![Page 239: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/239.jpg)
Is this else-if statement?
if (….)
printf(…);
else if (…)
printf(….);
![Page 240: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/240.jpg)
while-loop
syntax:• while (expression)
statement
usage:
the as Java
![Page 241: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/241.jpg)
![Page 242: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/242.jpg)
![Page 243: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/243.jpg)
for-loop
same as Java syntax:
• for (expr1; expr2; expr3)
statement
next statement meaning interpreted in terms of while-
loop
![Page 244: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/244.jpg)
expr1;
while (expr2)
{
statement;
expr3;
}
![Page 245: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/245.jpg)
ex:
![Page 246: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/246.jpg)
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.
![Page 247: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/247.jpg)
ex:
![Page 248: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/248.jpg)
functions (modular programming)
#include <….>
function prototypes (optional )
function defs
main ()
{…..}
![Page 249: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/249.jpg)
ex:
![Page 250: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/250.jpg)
functions
![Page 251: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/251.jpg)
recursive functions
![Page 252: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/252.jpg)
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’
![Page 253: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/253.jpg)
example of strings• “abcde” is stored as
‘a’ ‘b’ ‘c’ ‘d’ ‘e’ ‘\0’
![Page 254: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/254.jpg)
![Page 255: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/255.jpg)
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)
![Page 256: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/256.jpg)
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
![Page 257: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/257.jpg)
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.
![Page 258: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/258.jpg)
example
next slide, study it carefully, as it involves some fundamental facts about pointers.
![Page 259: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/259.jpg)
![Page 260: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/260.jpg)
Now, why does it not work?
![Page 261: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/261.jpg)
type synonym : typedef
give a new name for existing types: ex:
• typedef char * string;• typedef int number;
• string s1;• number n;
![Page 262: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/262.jpg)
![Page 263: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/263.jpg)
structure (or record)
syntax• struct name
{
type field;
…..
}
name is optional
![Page 264: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/264.jpg)
ex:
after this, struct complex (together) is used as a type
![Page 265: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/265.jpg)
accessing members of a structure• use the “.” operator
structure_var . member
or use the “->” operator
ptr_to_structure -> member
![Page 266: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/266.jpg)
![Page 267: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/267.jpg)
or, equivalently
![Page 268: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/268.jpg)
or, (maybe better)
![Page 269: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/269.jpg)
or, w/ pointers
![Page 270: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/270.jpg)
or, w/ header file
Put definition (and other things) of type complex is in a header file, say, complex.h.
![Page 271: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/271.jpg)
then, to have better moularity
![Page 272: Unix and C Programming](https://reader036.vdocuments.us/reader036/viewer/2022081506/568145ae550346895db2aa51/html5/thumbnails/272.jpg)
Use structure w/ functions