------------------------ Week 2 Notes for CST8129 ------------------------ -Ian! D. Allen - idallen@idallen.ca *** Keep up on your readings (Course Outline: average 5 hours/week homework) 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 in Week #5 (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 Entering commands via command lines - the terminal driver - your terminal is two devices (keyboard and screen), loosely coupled - the keys you type all go through Unix before appearing on your screen - programs can choose not to display characters (e.g. passwords) - programs can change what the characters mean (e.g. vim) - many programs can all write to your screen at the same time - the terminal driver intercepts some characters and uses them - in printed documents we use the two-character form ^A to indicate the unprintable control character CTRL-A - when you see ^A it means hold down the CTRL key and type the single letter "a" (upper or lower-case) - some programs let you use this syntax directly, e.g.: stty erase ^H - NOTE: the sequence ^? stands for the ASCII DEL character, not CTRL-? When you see ^? it means use the DEL key, not CTRL-? - default line edit characters: ^H ^? ^U ^W ^R ^L ^H or ^? - backspace and erase one character ^U - erase entire line of typing ^W - erase one word in current line ^R - redraw current line (in case overwritten) ^L - clear/redraw screen (in bash, less, more, and vim) Other special keyboard characters: - these characters operate with most Unix programs; however, some programs (e.g. vim) can change their meaning: ^C - send interrupt signal (SIGINT) to current process The Unix Shell (e.g. "bash" - Bourne Again SHell) - See Notes: unix_shell.txt - The Unix/Linux Shell - purpose: to find and run commands - with some support for programming (scripts) - GLOB patterns, aliases, variables, history, name completion - 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: bash - 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 - See 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 - you can use a semicolon (";") to separate commands: $ sleep 4 ; echo BOO $ date ; who ; echo hi ; wc /etc/passwd - commands are executed left-to-right, even if a command fails $ nosuchcommand ; echo this works The shell can expand file GLOB patterns (similar to DOS wildcard) - See Notes: glob_patterns.txt - GLOB patterns (wildcard pathname matching) - 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 $ echo * a b c $ echo .* . .. .bashrc - like ls, GLOB patterns do not match hidden names - you can see hidden names using: ls -a - echo * - does not match hidden files The shell can expand variables, both local and environment - e.g. $variable as in: echo "$SHELL" - shell does the variable expansion, not the command! $ x='a b c' $ echo $x # <- echo sees three arguments: a, b, and c $ echo "$x" # <- echo sees one argument: a b c Shell quoting to hide special metacharacters - See Notes: quotes.txt - Unix/Linux Shell Command Line Quoting - a shell interprets many punctuation characters ("metacharacters") specially - quotes and backslashes hide special characters from the shell - quote the shell metacharacters, to stop the shell from helping you - 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 are strongest and hide *all* other shell metacharacters $ echo 'This is a \"nice\" day. I live at $HOME.' This is a \"nice\" day. I live at $HOME. - double quotes are weak and hide almost everything, except they don't hide: \" and $variable expansions, e.g. $ echo "This is a \"nice\" day. I live at $HOME." This is a "nice" day. I live at /home/alleni. - only single quotes stop variable expansion - both kinds of quotes stop GLOB (wildcard) expansion and blank splitting - you can also use backslash to hide individual characters from the shell $ echo It\'s a \* nice day \* today! It's a * nice day * today! Quoting affects the number of arguments the shell sees - See Notes: arguments_and_options.txt - See Notes: quotes.txt - Unix/Linux Shell Command Line 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) $ echo " abc "' def ' " ghi " ' jkl ' abc def ghi jkl $ ./argv.sh " abc "' def ' " ghi " ' jkl ' Argument 0 is [./argv.sh] Argument 1 is [ abc def ] Argument 2 is [ ghi ] Argument 3 is [ jkl ] Try these quoted strings using the argv.sh program: $ mkdir empty ; cd empty ; touch a b c d $ ../argv.sh ' * ' $ ../argv.sh '" * "' $ ../argv.sh '"' * '"' $ ../argv.sh '"'" * "'"' $ ../argv.sh ' * ' * " * " $ ../argv.sh \' * \' Listing files - ls - long listing: ls -l /etc/passwd -rw-r--r-- 1 root root 2871 Aug 24 02:14 /etc/passwd * first character indicates type of pathname: file, directory, etc. * next nine characters rw-r--r-- are three sets of permissions * next number 1 is the count of names this pathname has (Unix/Linux files can have multiple names) * next string root is the owner of this file * next string root is the group of this file * next number 2871 is the size of the file in bytes (including all characters, including spaces and newlines) * next comes the modify date and time of the file (Unix does not keep the creation time of any file) * last is the name - to see hidden files use: ls -a Pagination commands - less and more - one small set of commands does all the pagination - pagination is not built in to each command, as in DOS - searching can be done using / in programs "more" and "less" (and vim) Man pages - See Notes: man_page_RTFM.txt - using the man command, man -k, and apropos - know how to read man pages (man page syntax meaning) Commands: - See Notes: unix_command_list.txt - keep an ongoing list of commands and their common uses - keep notes on what the common options do (e.g. "ls -a -l") - keep current on the commands covered; look up what you don't know New and updated files to read: arguments_and_options.txt on Unix Command Lines glob_patterns.txt GLOB patterns (wildcard pathname matching) home_and_HOME.txt Directories: current, HOME, and /home man_page_RTFM.txt Searching for items in the manual pages (RTFM) miscellaneous.txt Miscellaneous Unix Facts pathnames.txt Unix/Linux Pathnames permissions.txt Unix Permissions quotes.txt Unix/Linux Shell Command Line Quoting redirection.txt I/O Redirection (including Pipes) shell_prompt.txt Setting the BASH shell prompt startup_files.txt Setting up Startup Files: .bash_profile and .bashrc unix_command_list.txt Basic Unix/Linux Command List unix_shell.txt The Unix/Linux Shell vi_basics.txt The VI (VIM) Editor - Basics vi_refcard_back.pdf VI reference card vi_refcard_front.pdf VI reference card week02notes.txt Week 2 Notes for CST8129 Still to come: exercise02.txt Readings in "Unix Shells by Example": No new readings this week - finish readings from last week.