300 likes | 480 Views
CSC 382: Computer Security. Introduction to UNIX Programming. Topics. What is UNIX? Logging on. Basic Commands. The UNIX Shell Compiling on UNIX. What is UNIX?.
E N D
CSC 382: Computer Security Introduction to UNIX Programming CSC 382: Computer Security
Topics • What is UNIX? • Logging on. • Basic Commands. • The UNIX Shell • Compiling on UNIX CSC 382: Computer Security
What is UNIX? UNIX:: /yoo'niks/ [In the authors' words, "A weak pun on Multics"] n. (also `Unix') An interactive time-sharing system originally invented in 1969 by Ken Thompson after Bell Labs left the Multics project, originally so he could play games on his scavenged PDP-7. Dennis Ritchie, the inventor of C, is considered a co-author of the system. CSC 382: Computer Security
Logging on to UNIX General Categories • Console • Network For the first assignment, we will log on to zappa.nku.edu CSC 382: Computer Security
1. Console login: your_username <Enter> password: your_password <Enter> Last login: Sun Aug 28 19:35:32 2005 from foo.com. You have new mail. Terminal type? [vt100] <Enter> Sun Microsystems Inc. SunOS 5.9 Generic May 2002 NOTICE: April 19, 2005 – The upgrade to Java JDK 1.5.2 has been completed. $ CSC 382: Computer Security
2. Network Login using Putty on MS Windows client CSC 382: Computer Security
3. Insecure Network Login via Telnet f/ Win client CSC 382: Computer Security
Structure of a UNIX command #command [[ - ] option(s)] [option argument(s)] [command argument(s)] Examples: • $ ls • $ ls -la • $ ls -la m* • $ lpr -Pspr -n 3 proposal.ps CSC 382: Computer Security
File Maintenance Commands Creating, Deleting and Managing Files • cp, mv, rm, ls # cp myfile myfile2 # mv myfile2 renamed_file # mv “latest revisions october.txt” laterevs.txt # rm renamed_file # ls Desktop Mail myfile myfile2 # ls –al CSC 382: Computer Security
File Maintenance Commands Viewing the Contents of Files • cat, more, less # cat > myfile This is an example of how to use the cat command to add plain text to a file <Ctrl-D> # more myfile This is an example of how to use the cat command to add plain text to a file CSC 382: Computer Security
File Maintenance Commands Creating, Deleting and Managing Directories • mkdir, cd, pwd, rmdir # mkdir first # cd first # pwd /home7/smithj/first # cd # pwd /home7/smithj # cp myfile myfile2 # ls my* myfile myfile2 # rmdir first rmdir: first: Directory not empty CSC 382: Computer Security
Obtaining Help with man CSC 382: Computer Security
Obtaining Help with man man [options][-s section] command-list # man ls User Commands ls(1) NAME ls - list contents of directory SYNOPSIS /usr/bin/ls [-aAbcCdfFghilLmnopqrRstux1@] [file...] /usr/xpg4/bin/ls [-aAbcCdfFghilLmnopqrRstux1@] [file...] DESCRIPTION For each file that is a directory, ls lists the contents of the directory. For each file that is an ordinary file, ls repeats its name and any other information requested. The output is sorted alphabetically by default. When no argument is given, the current directory is listed. … CSC 382: Computer Security
Other Forms of Help • whatis # whatis login setenv login login (1) - sign on to the system setenv set (1) - shell built-in functions to determine the characteristics for environmental variables of the current shell and its descendents • apropos # apropos web neon neon (3) - HTTP and WebDAV client lib installer installer (1m) - Solaris Web Start installer smcwebserver smcwebserver (1m) - start the Sun console wbem wbem (5) - Web-Based Enterprise Mgmt CSC 382: Computer Security
What is a shell? A command interpreter. • Runs external commands like cp and rm. • Built-in commands change shell environment: • cd – change directory • VAR=value • I/O redirection. • cat /etc/shells >shells • Ease of use • Command line editing, tab completion, history. • Programming • Conditionals, loops, etc. CSC 382: Computer Security
Environment Variables CSC 382: Computer Security
Shell Initialization Files • Configure shell settings at login. • Create aliases. • Set environment variables. • bash initialization files • /etc/profile System-wide for sh and bash. • /etc/bashrc System-wide for bash. • ~/.bashrc User startup file. CSC 382: Computer Security
Globbing • ? Matches any one character. • * Matches zero or more characters. • [] Matches list of characters inside brackets. CSC 382: Computer Security
Globbing > ls *html announce.html guidelines.html readings.html sites.html assignments.html index.html schedule.html > cd assignments > ls a[2-3]?html a2.html a3.html CSC 382: Computer Security
Command History Up-arrow Previous command Down-arrow Next command history List old commands !! Previous command !# Command # !$ Last arg of previous command CSC 382: Computer Security
Command line editing Ctrl-a Beginning of line Ctrl-e End of line Left-arrow Move back one character Right-arrow Move forward one character Ctrl-u Erase line CSC 382: Computer Security
Filename completion TAB Completes filename TAB-TAB Show list of possible completions. CSC 382: Computer Security
UNIX C Programming $ cat >hello.c #include <stdio.h> int main(int argc, char *argv[]) { printf("Hello, world!\n"); return 0; } CSC 382: Computer Security
UNIX C Programming $ gcc –o hello hello.c $ ./hello Hello, world! CSC 382: Computer Security
gcc Flags -ansi Use ANSI C 99 standard. -pedantic Disallow C extensions. -Wall Print all warnings. -o file Name output file. -g Include debugging info. -ggdb Add extra GDB debug info. CSC 382: Computer Security
Command Line Arguments argc – integer number of arguments argv – array of character string arguments CSC 382: Computer Security
printargs.c $ cat >printargs.c #include <stdio.h> int main(int argc, char *argv[]) { int i; for(i=0; i<argc; i++) printf("arg[%d] = %s\n", i, argv[i]); return 0; } CSC 382: Computer Security
printargs $ gcc –ansi –pedantic –Wall –o printargs printargs.c $ ./printargs a b c 1 2 3 arg[0] = ./printargs arg[1] = a arg[2] = b arg[3] = c arg[4] = 1 arg[5] = 2 arg[6] = 3 CSC 382: Computer Security