------------------------ Week 2 Notes for NET2003 ------------------------ -Ian! D. Allen - idallen@idallen.ca Remember - knowing how to find out an answer is more important than memorizing the answer. Learn to fish! RTFM! (Read The Fine Manual) Reminder: Midterm Test #1 begins Week #4 (see the course home page). Unix is an O/S designed by programmers for programmers - command-line driven (programmers didn't use the GUI) - things work silently (no confirmation) - messages appear only when things fail - most command names are cryptic abbreviations! - hard to learn but easy to use The Unix Shell (e.g. "bash" - Bourne Again SHell) - purpose: to find and run commands - with some support for programming (scripts) - Notes: unix_shell.txt - The Unix/Linux Shell - shell is an unprivileged program - standard Linux shell is named "bash"; you can start up multiple copies of bash by typing its name as a command - shell prompts when it reads from your keyboard - shell does not prompt if reading input from a file! - most other Unix programs do *not* prompt when they read your keyboard - Notes: shell_prompt.txt - Setting the BASH shell prompt - shell splits your command line on blanks (white space) - first non-redirection word is the command name - the rest of the arguments are passed to the command - Notes: arguments_and_options.txt - Options and Arguments on Command Lines - shell can expand file GLOB patterns (similar to DOS wildcard) - shell helps with wildcards (GLOB) - * matches zero or more characters - ? matches a single character (any character, even a space) - [abc] matches a single character from the list abc - GLOB patterns do not match names that start with a period (hidden pathnames), unless you explicitly put the period into the file name - like ls, GLOB patterns do not match hidden names - echo * - does not match hidden files - Notes: glob_patterns.txt - GLOB patterns (wildcard pathname matching) - shell can expand variables, both local and environment - e.g. $variable as in echo "$SHELL" - shell does the expansion, not the command! - you can use ; to separate commands: sleep 4 ; echo BOO Quoting the metacharacters, to stop the shell from helping you - quotes and backslashes hide things from the shell - quotes delimit each argument; they tell the shell where it starts/ends - quotes are not part of the argument - two different strengths of quotes - single quotes hide all characters - double quotes hide everything *except* \" and $variable expansions - only single stops variable expansion - both kinds stop GLOB expansion and blanks - you can also use backslash to hide characters from the shell - Notes: quotes.txt - Unix/Linux Shell Command Line Quoting Entering commands - the terminal driver - your terminal is two devices, loosely coupled - control chars (unprintables) syntax: ^H ^? - backspace to erase one char - stty erase - line edit: ^H ^? ^W ^U ^R ^L - ^U - erase entire line - ^W - erase word - ^R - redraw line (in case overwritten) - ^C - interrupt - ^D - EOF end of input - ^L - clear screen (in bash shell, less, more, and vim) You can tell the shell to redirect the input and/or output of any command: see Notes file redirection.txt - simple output redirection: sort /etc/passwd >out - simple input redirection: sort out - simple pipe: ls /usr/bin | wc - "standard output" is usually your screen - "standard input" is usually your keyboard - redirection is never counted as arguments to a command - the shell does the redirection and removes the syntax - the command never sees any of the redirection syntax Important: Most Unix commands that take file names as arguments will read standard input (usually your keyboard) if no file names are given. Unlike the shell, the commands will *not* prompt when reading your keyboard. See Notes file miscellaneous.txt - learn various ways of getting out of programs - EOF and Interrupting Processes $ sort - note difference between ^C and ^D $ wc - note difference between ^C and ^D $ cat - less difference - Unix line ends are \n characters (not \r or \r\n) $ echo hi | wc -c # prints 3 not 2 because of \n at end Pagination commands - one set of commands does all the pagination - pagination is not built in to each command, as in DOS - searching can be done using / in MORE and LESS (and VIM) Man pages - using the man command, man -k, and apropos (man_page_RTFM.txt) Process listing - ps -f - pstree Commands: see Notes file unix_command_list.txt - use "rm -r dir" to remove an entire directory tree and all files - keep a list of commands and their common uses - know how to read man pages (man page syntax meaning) - don't send binary (executable machine code) files to your screen - check the contents with the "file" command first - grep pattern file - crude Data Mining: selecting content by looking for lines - using grep on course notes: grep ssh ~idallen/public_html/teaching/net2003/05w/notes/*.txt - grep -v finds lines that do *not* contain the pattern Other commands covered: head(-5) tail(-5) apropos(man -k) du sum tree find cal(try 9 1752) hostname who last ps pstree wc(-lwc) ls(-ld) grep(-v) sort(-nr) rm(-r) - "sort" sorts all its arguments (together) to standard output - the files themselves are not changed - never use redirection to direct output into an input file! - "sort" sorts alphabetically (usually ASCII collating order) - need to use -n option to sort by leading number - "ls -ld" is needed to see the attributes of a directory (instead of seeing the attributes of the directory contents) Review: difference between command names, options, and file names $ date >wc ; >wc date ; wc -wc wc >ls ; ls -ls ls >wc Shell quoting - use argv.sh to see how shell creates arguments (can also use ls -d) (fetch argv.sh.txt from the course Notes area and make it executable) see Notes: arguments_and_options.txt see Notes: quotes.txt $ echo " abc "' def ' " ghi " ' jkl ' $ ./argv " abc "' def ' " ghi " ' jkl ' $ mkdir empty ; cd empty ; touch a b c d $ ./argv ' * ' $ ./argv '" * "' $ ./argv '"' * '"' $ ./argv '"'" * "'"' $ ./argv ' * ' * " * " $ ./argv \' * \' Output redirection: see Notes: redirection.txt - you can only redirect what you can see - if you see nothing, adding redirection will create an empty file! - redirection is done first and then removed from command line - it is never passed to the command; it is never a command argument $ date ; date >out $ cp a b ; cp a b >out $ head /etc/passwd >out ; sort out ; sort out >out $ rm out >out $ date >out ; ls -l out >out - appending using >> Pipes - standard output to standard input (commands must be compatible) - send standard output of one command to standard input of another - $ date > out ; wc