% CST8207 Assignment 06 - quoting, login profiles, aliases, shell options % Ian! D. Allen - - [www.idallen.com] % Fall 2013 - September to December 2013 - Updated Sat Oct 26 04:54:06 EDT 2013 Due Date and Deliverables ========================= - **Due Date**: `08h00 (8am) Monday October 28, 2013 (start of Week 9)` - You have 10 days to do this assignment, but your next assignment will be available next week and will overlap this assignment by a week. Don’t delay! - Late assignments or wrong file names may not be marked. Be accurate. - **Available online**: - Version 1 – 02:30am Friday October 18, 2013 - Version 2 – 23:45pm Sunday October 20, 2013 – added some Hints; introduce `eval` - **Prerequisites**: - All [Class Notes] since the beginning of term. - All your previous [Assignments]. - an ability to **READ ALL THE WORDS** to work effectively - **Deliverables**: 1. One text file uploaded to Blackboard according to the steps in the [Checking Program] section below. 2. Directory structure created and left for marking on the [Course Linux Server] (**CLS**).\ **Do not delete any assignment work from the CLS until after the term is over!** **WARNING:** Some inattentive students upload Assignment #6 into the Assignment #5 upload area. Don’t make that mistake! Be exact. Purpose of this Assignment ========================== This assignment is based on your weekly [Class Notes]. 1. Practice [Quoting] strings as command line arguments. 2. Create your [Startup Files] profile files with sysadmin aliases and options. Remember to **READ ALL THE WORDS** to work effectively and not waste time. Introduction and Overview ========================= This is an overview of how you are expected to complete this assignment. Read all the words before you start working. 1. Complete the **Tasks** listed below. 2. Verify your own work before running the [Checking Program]. 3. Run the [Checking Program] to help you find errors. 4. Submit the output of the [Checking Program] to Blackboard before the due date. 5. **READ ALL THE WORDS** to work effectively and not waste time. You will create file system structure in your CLS home directory containing various directories and files. You can use the [Checking Program] to check your work as you do the tasks. You can check your work with the checking program as often as you like before you submit your final mark. **Some task sections below require you to finish the whole section before running the checking program; you may not always be able to run the checking program successfully after every single task step.** When you are finished the tasks, leave these files, directories, and links in place on the CLS as part of your deliverables. **Do not delete any assignment work from the CLS until after the term is over!** Edit a file to fix the [Quoting] to protect all shell meta-characters. Use a text editor to create your [Startup Files] login profile files `.bashrc` and `.bash_profile`. Assignments may be re-marked at any time on the CLS; you must have your term work available on the CLS right until term end. > Since I also do manual marking of student assignments, your final mark may > not be the same as the mark submitted using the current version of the > [Checking Program]. I do not guarantee that any version of the [Checking > Program] will find all the errors in your work. Complete your assignments > according to the specifications, not according to the incomplete set of the > mistakes detected by the [Checking Program]. The Source Directory -------------------- All references to the “Source Directory” below are to the CLS directory `~idallen/cst8207/13f/assignment06/` and that name starts with a *tilde* character `~` followed by a userid with no intervening slash. The leading tilde indicates to the shell that the pathname starts with the HOME directory of the account `idallen` (seven letters). Tasks ===== - Do the following tasks in order, from top to bottom. - These tasks must be done in your account on the [Course Linux Server]. - **READ ALL THE WORDS!** and do not skip steps. - Your instructor will mark on the due date the work you do in your account on the CLS. Leave all your work on the CLS and do not modify it. - **Do not delete any assignment work from the CLS until after the course is over.** Set Up ------ 1. Create the following directory structure in your CLS HOME directory and record (for study purposes) the series of Unix commands you used to create it. Spelling and capitalization must be exactly as shown: CST8207-13F `-- Assignments `-- assignment06 **This directory is the base directory for most pathnames in this assignment. Store your scripts and answers here.** Run the [Checking Program] to verify your work so far. Part A - Quoting exercise I --------------------------- 1. Make the above `assignment06` directory your current directory and copy the script file `test.sh` from the [Source Directory] into your own file `test.sh` (and remember to always use the file copy option that preserves file attributes such as timestamps). 2. Execute this file (enable execute permissions and then run it) or use `bash` to read the file so that all the commands inside it execute: $ chmod ugo+x test.sh $ ./test.sh ** - OR USE BASH: - ** $ bash ./test.sh 3. Note that the output doesn’t match what is written in the script file. (For example: the quotes are missing, GLOB characters and variables are expanding, and the spacing of the words is different.) Fix the [Quoting] inside the file so that every line is fully quoted to appear exactly as written, blanks included, with no meta-character expansion by the shell. The correct output should give eight lines and look *exactly* like this when you are finished (including all the extra spaces between some of the words): Where is the question mark after the file /etc/passwd? Is one also missing after /etc/group? The shell prompt is contained in the $PS1 variable. This isn't appearing on my screen properly. It's missing some quotes. This is also "missing" some quotes. It is not "right" yet. This is also "missing" quotes. It's output that doesn't make sense. This isn't working either. The shell gives an error message. *** This is a file to practice shell quoting. *** You must edit the file to make the entire line of text on each line *one* argument to `echo`. You can check your success by temporarily substituting the [`argv.sh`] program from the [Class Notes] for the `echo` command. (You have to put the `argv.sh` program into the same directory as `test.sh` under the name `argv.sh` and make it executable. Read the `test.sh` file for details on using an alias in this file to run this program. Remember to return the script to using only `echo` after your testing.) You’ll know you got it mostly right when you see this `wc` and `sum` output (but you’ll need to verify *one-argument-per-echo* using the `argv.sh` program): $ ./test.sh | wc 8 77 461 $ ./test.sh | sum 39169 1 If your word count is correct but the number of characters is less, you probably failed to make the entire text *one* argument to `echo` on each line. You must use [Quoting] to hide *all* the blanks and special characters from the shell on each of the lines. The `argv.sh` program will tell you if you got it right. 4. When the edited script output is correct, run the script and redirect the script output into file `testout.txt` Run the [Checking Program] to verify your work so far. Part B - Creating profile files ------------------------------- 1. Review the [Class Notes]: - [Shell Variables - Basics (non-scripting)] - [Login and Shell Startup Files, aliases, options: .bash\_profile and .bashrc][Startup Files] 2. Use a text editor to create your `.bash_profile` and `.bashrc` files with some or all of the suggested content described in the page [Startup Files]. Read the man pages for each command to know what the options to each command mean. The `cp -p` option is very useful for sysadmin. **Do not set any options or aliases in your `.bashrc` that you do not understand!** If you don’t know the meaning of a setting, don’t use it. You can RTFM in the `bash` man page for all BASH settings, and RTFM in command man pages to learn about options to commands. I will be spot-checking your knowledge of your aliases. Students using aliases they don’t understand will experience much confusion trying to do future assignments. Only use aliases you understand. 3. Using the `PS1` variable from [Worksheet #2 HTML], set your prompt to include your user name, your computer name, and the basename of your current working directory. Put this setting at the bottom of your `.bashrc` file. 4. Verify that nothing prints on your screen after you enter your password when you run the non-interactive shell connection using `ssh localhost true` (as described in the section on [Non-interactive shells and PS1]: $ ssh localhost true *** COURSE LINUX SERVER *** user@localhost's password: $ For non-interactive commands to work properly, there must be **no** output on your screen after you enter your password using the above non-interactive command line using the `true` command. Your instructor will mark the `.bashrc` and `.bash_profile` files in your account on the assignment due date. Do not upload them to Blackboard. Leave them there on the CLS. Do not delete anything. Run the [Checking Program] to verify your work so far. Part C - Quoting exercise II ---------------------------- On the CLS run an SSH remote shell command to `localhost` to verify that it works: $ ssh localhost "date ; whoami ; echo Hello World" *** COURSE LINUX SERVER *** abcd0001@localhost's password: Thu Oct 17 21:11:44 EDT 2013 abcd0001 Hello World Above, we use quotes to hide the semicolons from the local shell so that they get passed to the remote shell, when they are again treated as metacharacters and allow multiple commands to be written on one line. The remote shell executes three commands and sends the three outputs back to our screen. Two shells are involved; one on the local machine to prepare the command line, and another on the remote machine to execute it. The act of using SSH to send a command to a remote machine strips two levels of quoting from the line being sent. You can see this by putting an `echo` in front of the command, to see what is actually being sent to the remote system’s shell: $ echo ssh localhost "date ; whoami ; echo Hello World" ssh localhost date ; whoami ; echo Hello World The above `echo` output shows that the shell running on the remote machine will get the line without any quotes around it, and so the remote shell will expand all the metacharacters and execute three commands. To quote something on the *remote* machine in the *remote* shell requires careful thought. One level of quotes isn’t enough. You need one level of quotes for the shell you’re typing into on the local machine, and another level of quotes that get sent to the remote shell: $ echo "echo Hello World" # only one level of quotes echo Hello World # note lack of quotes $ ssh localhost "echo Hello World" Hello World # no quotes; blanks disappear $ echo "echo 'Hello World'" # two levels of quotes echo 'Hello World' # now it has quotes $ ssh localhost "echo 'Hello World'" Hello World # quoted blanks are preserved Sending the line to a remote login shell works similarly to echoing the command line from the current shell into another local bash shell through a pipe (except in the above case, the bash shell is on a remote machine). Locally, it looks like this, and again two shells are involved and two levels of quotes are stripped: $ echo "date ; whoami ; echo Hello World" date ; whoami ; echo Hello World $ echo "date ; whoami ; echo Hello World" | bash Thu Oct 17 21:12:44 EDT 2013 abcd0001 Hello World You can also see this two-level quote stripping by echoing a doubly-quoted string from the current shell into a second local shell: $ echo "echo Hello World" # only one level of quotes echo Hello World # note lack of quotes $ echo "echo Hello World" | bash # two levels of shell Hello World # no quotes; blanks disappear $ echo "echo 'Hello World'" # two levels of quotes echo 'Hello World' # now it has quotes $ echo "echo 'Hello World'" | bash # two levels of shell Hello World # quoted blanks are preserved Quotes that you want sent to the remote shell themselves need quoting to protect those quotes from the local shell. One strategy is to single-quote all the double quotes and double-quote all the single quotes: $ echo echo '"' # one level of shell echo " # note lack of quotes $ echo echo "'"'"'"'" # quote all the quotes echo '"' # now it has quotes $ ssh localhost echo "'"'"'"'" # send to remote shell " # quoted quote is preserved $ echo echo "'"'"'"'" | bash # or use two levels of shell " # quoted quote is preserved Here is your assignment: Instead of having the remote shell echo the words `Hello World`, have it echo exactly the outputs shown below. Create `ssh` command lines that follow `ssh localhost` with a correctly quoted remote shell `echo` command that will produce each of the shown outputs below exactly, including the extra blanks between the words. The first solution is given to you (several solutions are possible – you only need to find one): 0. Echo this: `Eat ; Pray ; Linux`\ Solution: `$ ssh localhost echo "'Eat ; Pray ; Linux'"`\ Output: `Eat ; Pray ; Linux`\ Solution: `$ ssh localhost echo '"Eat ; Pray ; Linux"'`\ Output: `Eat ; Pray ; Linux`\ Solution: `$ ssh localhost echo Eat\\\ \\\ \\\;\\\ \\\ Pray\\\ \\\ \\\;\\\ \\\ Linux`\ Output: `Eat ; Pray ; Linux` 1. Echo this: `Glob /etc/passwd* matches two files.`\ Solution 1? `$ ssh localhost ...`\ Output: `Glob /etc/passwd* matches two files.` 2. Echo this: `I'm a happy camper.`\ Solution 2? `$ ssh localhost ...`\ Output: `I'm a happy camper.` 3. Echo this: `One environment variable is $SHELL (the shell)`\ Solution 3? `$ ssh localhost ...`\ Output: `One environment variable is $SHELL (the shell)` 4. Echo this: `It's evident that "love" > $$`\ Solution 4? `$ ssh localhost ...`\ Output: `It's evident that "love" > $$` Each of the above problems has at least three solutions, one using each of the three types of quoting. You only need to find one quoting solution for each question, and your solution might also mix quoting styles. Use a text editor to put each of your four `ssh` command line solutions into a file of the form `quoteN.sh` where `N` is replaced by the question number, e.g. the file `quote0.sh` would contain the one line: ssh localhost echo "'Eat ; Pray ; Linux'" (Note the extra blanks required between the words! Count your blanks to match the question!) You can test each file by handing the file to the shell for execution, e.g. run `sh quote0.sh`. Hints: First make sure you can use `echo` or `argv.sh` without SSH to display the line correctly on your screen, then add the extra quoting needed to pass the line and all its quoting to a remote shell via SSH. While you are testing, you can get the same quoting output results more quickly using the shell keyword `eval` in place of `ssh localhost` above: $ ssh localhost echo Eat\\\ \\\ \\\;\\\ \\\ Pray\\\ \\\ \\\;\\\ \\\ Linux *** COURSE LINUX SERVER *** abcd0001@localhost's password: Eat ; Pray ; Linux $ eval echo Eat\\\ \\\ \\\;\\\ \\\ Pray\\\ \\\ \\\;\\\ \\\ Linux Eat ; Pray ; Linux $ eval ./argv.sh Eat\\\ \\\ \\\;\\\ \\\ Pray\\\ \\\ \\\;\\\ \\\ Linux Argument 0 is [./argv.sh] Argument 1 is [Eat ; Pray ; Linux] You can use `argv.sh` to verify that the string is one single argument to the remote `echo` command. Making the output one argument is not a requirement of this assignment, but doing so confirms that you know how to properly escape a remote shell command. Run the [Checking Program] to verify your work so far. When you are done ----------------- That is all the tasks you need to do. Check your work a final time using the [Checking Program] and save the output as described below. Submit your mark following the directions below. Part D - CentOS Configured Installation (future) ------------------------------------------------ Carefully and precisely follow the directions to [install CentOS 6.4 server edition] into a virtual machine on your mobile device. The installation notes give you ways to verify that your configuration is correct. If you have any doubts, contact your instructor. This work will not be checked until your next assignment. Checking the installation is not part of the current assignment. Checking, Marking, and Submitting your Work =========================================== **Summary:** Do some tasks, then run the checking program to verify your work as you go. You can run the checking program as often as you want. When you have the best mark, upload the marks file to Blackboard. 1. There is a [Checking Program] named `assignment06check` in the [Source Directory] on the CLS. You can execute this program by typing its (long) pathname into the shell: `$ ~idallen/cst8207/13f/assignment06/assignment06check` 2. Execute the above “check” program. This program will check your work, assign you a mark, and display the output on your screen. (You may want to paginate the long output so you can read all of it.) You may run the “check” program as many times as you wish, to correct mistakes and get the best mark. **Some task sections require you to finish the whole section before running the checking program at the end; you may not always be able to run the checking program successfully after every single task step.** 3. When you are done with checking this assignment, and you like what you see on your screen, redirect the output of the [Checking Program] into the text file `assignment06.txt` under your `assignment06` directory on the CLS. Use the *exact* name `assignment06.txt` in your `assignment06` directory. Case (upper/lower case letters) matters. Be absolutely accurate, as if your marks depended on it. Do not edit the file. Make sure the file actually contains the output of the checking program! 4. Transfer the above `assignment06.txt` file from the CLS to your local computer and verify that the file still contains all the output from the checking program. Do not edit this file! No empty files, please! Edited or damaged files will not be marked. You may want to refer to your [File Transfer] notes. 5. Submit the `assignment06.txt` file under the correct Assignment area on Blackboard (with the exact name) before the due date. Upload the file via the **assignment06** “Upload Assignment” facility in Blackboard: click on the underlined **assignment06** link in Blackboard. Use “**Attach File**” and “**Submit**” to upload your plain text file. No word-processor documents. Do not send email. Use only “Attach File”. Do not enter any text into the **Submission** or **Comments** boxes on Blackboard; I do not read them. Use only the “**Attach File**” section followed by the **Submit** button. (If you want to send me comments about your assignment, use email.) 6. Your instructor may also mark the `assignment06` directory in your CLS account after the due date. Leave everything there on the CLS. **Do not delete any assignment work from the CLS until after the term is over!** Use the *exact* file name given above. Upload only one single file of plain text, not HTML, not MSWord. No fonts, no word-processing. Plain text only. Did I mention that the format is plain text (suitable for VIM/Nano/Pico/Gedit or Notepad)? **NO EMAIL, WORD PROCESSOR, PDF, RTF, or HTML DOCUMENTS ACCEPTED.** No marks are awarded for submitting under the wrong assignment number or for using the wrong file name. Use the exact name given above. WARNING: Some inattentive students don’t read all these words. Don’t make that mistake! Be exact. **READ ALL THE WORDS. OH PLEASE, PLEASE, PLEASE READ ALL THE WORDS!** -- | Ian! D. Allen - idallen@idallen.ca - Ottawa, Ontario, Canada | Home Page: http://idallen.com/ Contact Improv: http://contactimprov.ca/ | College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/ | Defend digital freedom: http://eff.org/ and have fun: http://fools.ca/ [Plain Text] - plain text version of this page in [Pandoc Markdown] format [www.idallen.com]: http://www.idallen.com/ [Class Notes]: indexcgi.cgi#XImportant_Notes__alphabetical_order_ [Assignments]: indexcgi.cgi#XAssignments [Checking Program]: #checking-marking-and-submitting-your-work [Course Linux Server]: 070_course_linux_server.html [Quoting]: 440_quotes.html [Startup Files]: 350_startup_files.html [Source Directory]: #the-source-directory [`argv.sh`]: 440_quotes.html#using-argv.sh-to-count-command-line-arguments [Shell Variables - Basics (non-scripting)]: 320_shell_variables.html [Worksheet #2 HTML]: worksheet02.html [Non-interactive shells and PS1]: 350_startup_files.html#non-interactive-shells-and-ps1 [install CentOS 6.4 server edition]: 000_centos_install.html [File Transfer]: 015_file_transfer.html [Plain Text]: assignment06.txt [Pandoc Markdown]: http://johnmacfarlane.net/pandoc/