360 likes | 475 Vues
This lecture covers the fundamentals of UNIX and its advanced programming techniques, focusing on the structure of an operating system, file system navigation, and essential UNIX commands. You will learn how to create and manage user accounts, navigate the filesystem using commands like ls, cd, and mkdir, and understand file permissions. Additionally, we explore shell usage and variables, I/O redirection, pipes, and filtering data. This foundational knowledge will prepare you for more complex programming and system administration tasks in UNIX environments.
E N D
Lecture 1: Introduction, Basic UNIX Advanced Programming Techniques
Why are we here? • What’s a computer • Why do we run programs? • What is needed to create a program?
Applications Shell \ Kernel (OS) Hardware Structure of a typical OS • There are many standard applications: • file system commands • text editors • compilers • text processing
Logging In • Create Acct • www.cs.drexel.edu/Account.php • Cluster • PuTTY/SSH • Log in • Password
Home Directory • The user’s personal directory. E.g., • /home/kschmidt • /home/vzaychik • Location of many startup and customization files. E.g.: .vimrc .bashrc .bash_profile .forward .signature .plan .logout
Unix Filesystem • Files • Directories • Other special files
/ bin etc home/ tmp usr hollid2 scully bin etc netprog unix X ls who The Filesystem (eg)
Pathnames • Unique, on a given filesystem • Absolute vs. relative • ./ ../ ~/
Pathname Examples / bin/ etc/ home/ tmp/ usr/ Hollid2/ scully/ bin/ local/ netprog unix/ X ls who Syllabus /usr/bin/ls /home/hollid2/unix/Syllabus
Commands for Traversing Filesystem • ls • pwd • cd • rm • cp • mv • mkdir • rmdir
Viewing files • cat • less, more • od • Comparing files • diff • cmp
Copying, removing, linking • rm – remove file • mv – move (rename) file • cp – copy file • ln – create hard (inode) or soft (symbolic) links to a file • touch – update file modification time, create an empty file if file doesn’t exist
Commands for directories • mkdir make directory • rmdir remove directory • Directories can also be moved or renamed (mv), and copied (cp –r)
Commands for Archiving • tar – Tape Archive • makes a large file from many files • gzip, gunzip • compression utility • tar on Linux does compression with the z option: $ tar czf 571back.tgz CS571 $ tar xzf assn1.tgz
File Permissions • Three types: • read abbreviated r • write abbreviated w • execute abbreviated x • There are 3 sets of permission: • user • group • other (the world, everybody else)
ls -l and permissions -rwxrwxrwx UserGroup Others Type of file: - –plain file d –directory s – symbolic link
Bourne-again Shell (bash) • Shells • Startup • Upon login (interactive), at the shell prompt • customization files: • /etc/profile • .bash_profile • .bashrc
Command syntax • First token is the “command” • Come in 3 flavors: • alias • shell builtin • External programs (utilities) • $PATH • Use type
Command Options and Arguments commandoption(s) arguments • Options (flags) • Short • Long • Option args • Arguments
man Pages • man • info
Some simple commands • date – print current date • who – print who is currently logged in • finger usr – more information about usr • ls -ao – lists (long) all files in a directory • du -sh – disk usage summary, human readable • quota
Standard I/O • The shell establishes 3 I/O channels: • stdin (0) • stdout (1) • stderr (2) • These streams my be redirected to/from a file, or even another command
Basic control codes • Ctrl-D (^D) • set ignoreeof • Ctrl-C (^C) • Ctrl-U (^U) • Ctrl-Z (^Z) • Ctrl-L (^L)
Shell metacharacters • Some characters have special meaning to the shell: • I/O redirection < > | • wildcards * ? [ ] • others & ; $ ! \ ( ) space tab newline • These must be escaped or quoted to inhibit special behavior
Shell Variables • Values • Assignment • Reading
Shell maintains variables • Some common ones: $PATH – list of directories to search for utilities $PS1 – Primary prompt $HOME – user’s home directory $USER – user’s login name $PWD – current working directory
set command (shell builtin) • The set command with no parameters will print out a list of all the shell variables • Sets options in the shell • -o • -noclobber • -ignoreeof
Quoting • Escape char • Strong quoting • Weak quoting
I/O Redirection • > - output to a file (clobber) • >> - append • < - input from a file • 2> - redirect stderr
Pipes – connecting processes • A pipe is a holder for a stream of data. • A pipe can be used to hold the output of one program and feed it to the input of another. prog1 prog2 STDOUT STDIN
filters • Programs that read some input (but don’t change it), perform a simple transformation on it, and write some output (to stdout) • Some common filters… • wc – word count (line count, character count) • tr – translate • grep, egrep – search files using regular expressions • sort – sorts files by line (lexically or numerically) • cut – select portions of a line • uniq – Removes identical adjacent lines • head, tail – displays first (last) n lines of a file
The Unix Philosophy • Stringing small utilities together with pipes and redirection to accomplish non-trivial tasks easily • E.g., find the 3 largest subdirectories: $ du –sh * | sort –nr | head -3 120180 Files 22652 Zaychik 9472 tweedledee.tgz
pipes and combining filters • Connect the output of one command to the input of another command to obtain a composition of filters • who | wc -l • ls | sort -f • ls -s | sort -n • ls -l | sort -nr -k4 • ls -l | grep ‘^d’
Process Control • Processes run in a subshell • Subshells inherit exported variables • Each process is has an ID (pid) and a parent (ppid) • Use the ps utility to look at some processes: $ ps PID TTY TIME CMD 350 pts/4 00:00:00 bash 22251 pts/4 00:00:00 vim 22300 pts/4 00:00:00 ps
Job Control • The shell allows you to manage jobs • place jobs in the background • move a job to the foreground • suspend a job • kill a job
Editors • A text editor is used to create and modify text files. • The most commonly used editors in the Unix community: • vi (vim on Linux) • $ vimtutor • emac • $ emacs • Then, hit ctrl-h t (that’s control-h, followed by ‘t’) • You must learn at least one of these editors