
Homework 2
Due: Thursday, Apr. 13, at midnight.
In this assignment you'll gain some more experience with shell commands and with writing short shell scripts.
General hint: Before trying to write and debug a script, it's very helpful to first work out the necessary commands by experimenting in a shell window. Also, look at man pages and other descriptions of commands. Sometimes options are available that allow a single command to do something you want instead of having to use several commands or writing loops or complex control structures in a script.
- (Commands and output) Use each of the following commands such
that
tweet(and nothing more) is printed on standard out, and nothing is printed on standard error when the command is entered. You can precede your commands with other commands (e.g., to create a file, run other commands, etc.) and/or pass options or arguments to your commands. Solve this problem for each of the listed commands individually.
echo, cat, ls, grep, !!
In a text file calledproblem1describe your solutions, including each command you use and a very brief explanation of it, including information about any additional commands or operations that were done before executing that command. And, yes, "nothing more" in the output means "nothing more".
- (An alias, defined in a script) Create a bash alias
privatesuch that when you runprivate foo, the entire subtree of the file-system starting atfoo(so justfooif it is a file, butfooand all of its files and subdirectories recursively if it is a directory) has its permissions changed as follows:
- The user's (owner's) permissions are unchanged.
- The group permissions are changed to match the owner's.
- The world (others) permissions are changed to remove write
permissions. Existing read and execute permissions should remain
unchanged.
defprivatesuch that enteringsource defprivatewould makeprivateavailable in the current shell (i.e.,source defprivatemakes thedefprivatecommand available for use in the current shell, but does not actually execute it immediately).
Hint:g=u.
- (Script) Create a bash script
combinethat takes 2 or more arguments, call themf1, f2, ..., fn. Scriptcombineshould work as follows:
- All arguments are treated as filenames.
- If fewer than two arguments are given, print a suitable
error message on
stderrand exit with a return code of 1. - If a file or directory
f1already exists, print "Error: first file exists" onstderrand exit with a return code of 1. - Otherwise concatenate the contents of
f2, ..., fnand copy them tostdout. Do not print any error messages from this (for example if some file does not exist or is a directory). Instead, any such error messages should be written tof1. You do not need to set an explicit return code from your script in this case. - None of the input files should be modified in any way, nor should their permissions.
-
The script should always exit successfully, unless it is given
an incorrect number of arguments, or
f1already exists. In that case it should exit with error code 1. -
The script should never create any files, other
than
f1, which should be created if and only if there are errors. Hint: just like you can test the existence of a file, there's a way to test if a file is empty. - The script must support filename arguments that contain spaces.
Restriction: You may not use the file names/dev/stdoutor/dev/stderr. These are not portable across *nix systems. Although they are found on most versions of Linux the problem can be solved without them.
Hint: Put filenames in double-quotes in case they contain "funny characters" (such as spaces). Your script should work with any file names, no matter what they contain.
Hints:shift, $@, -lt, -a.
Hint: Hints are just ideas you might find useful, not things that must appear in your solution.
- (Script) Create a bash script called
datedlinecountthat works as follows:
- If it is given fewer than two arguments, it prints an appropriate error message and exits with a return code of 1.
- Assume all the arguments are filenames for text files; you do not need to check for this.
- Append to the file indicated by the first argument the
following information:
- The time and date
- One line for each of the second-through-last arguments, containing the number of lines in the file and then the name of the file
- If there were three or more arguments (i.e., two or more
files to be counted), one additional line with the total
number of lines in all the files and then the
word
total.
For example, executing:./datedlinecount log foo bar; ./datedlinecount log foo*; cat logmight produce something like:
Mon Jan 16 20:42:16 PDT 2017 4 foo 17 bar 21 total Mon Jan 16 20:42:17 PDT 2017 4 foo 3 food 7 totalHints:shift, date, wc, $@.
Extra credit: The output should include a total line even if only a single file appears in the list.
Assessment: Your solutions should be:
- Correct scripts, etc., that run with
bashon either of our reference systems (klaatuor the current CSE Linux virtual machine). - In good style, including indentation and line breaks.
- Readable, such that it's easy to understand what's happening.
- Of reasonable size.
- Contain identifying information for you. (see below)
Identifying information including your name, CSE 374 Homework 2, the problem number, and the date should appear as comments at the top of each of your files.
Turn-in Instructions: Use the turn-in drop box
link on the main course web page to submit your files. Be sure to
submit each file separately. Make sure each file is
named
The drop box will allow you to turn in your homework up to two days late, if you choose to use one or two of your late days, but you are strongly advised to save your late days for later in the quarter when they are almost certain to be much more useful.
- General
- Communication
- Documentation
Computer Science & Engineering University of Washington Box 352350 Seattle, WA 98195-2350 (206) 543-1695 voice, (206) 543-2969 FAX
Comments to adminanchor
