CTS 1106 (Intro to Unix/Linux) Homework Assignment #6

 

Due: by the start of class on the date shown on the syllabus

Description:

Answer the following questions as briefly (but completely) as possible.  (Note the material covered by this homework is from the book and the on-line script resource.)  Note that for most of these questions, there is nothing to run or type on a computer.  Questions that ask “How should you ...” just require you to think about the answer, not actually try any commands.

  1. If your login script contains statements like “PATH=$PATH:$HOME/bin” and “PS1='$PWD$ '” and you make any changes to the file, how should you activate the changes?
  2. Suppose you've created a shell script named /usr/bin/myscript.  How can you prevent any user from executing this script?  Should you remove the executable permissions from the script file?
  3. If the shell variable x has the value 5, and you reassign it with “x="expr $x + 10"”, what is the new value of x?  What would have been the value if single quotes were used instead?  What is the most likely mistake in the above?
  4. A script named test containing only the two commands df and du displays nothing (not even an error message) when executed as “$ test”.  Why might that happen?  State two ways of making this script behave properly.
  5. Write a script that accepts one or more filenames as arguments, and displays the filenames in uppercase like this:
    $ upcase file1 readme README
    FILE1 README README

    [Hint:  Use tr and positional parameters.]
  6. If the command “set $(cat foo)” generates the error “invalid option”, what could be the reason, assuming that foo is a short, readable text file?
  7. Write a script which accepts one filename as an argument, and displays the last modification time if the file exists and a suitable error message if it doesn't.
    [Hints:   It is acceptable to display the whole output of ls -l file.  Remember the file argument is accessed by using a positional parameter.  Use the test command in an if statement to see if a file exists.]

    To extract just a file's modification time from the ls -l output, you can set LC_TIME environment variable to POSIX; then the modification time is in the 6th, 7th, and 8th fields of the output.  You can extract that by resetting the positional parameters from the output of the ls -l, using set and command substitution.  (There are easier ways but they require knowledge of awk or other advanced filter commands.)

  8. Why won't the exit command terminate the script, when placed in a shell script like this:
         ( statements; exit ) 
    How would you fix this, so the script exits after running the statements?
  9. You have a small script called cd2man containing these lines:
         #!/bin/sh -
         x=$(find $HOME -name $1 -print)
         cd $x 
    The script has correct permissions and PATH is set correctly so the script can be found.  When you run “cd2man man1”, you find that the current directory hasn't changed even though the directory man1 exists somewhere in your home directory tree.  No error messages appeared either.  Why did that happen, and what should you do to make cd2man change the current working directory?
  10. You have to run a script “foo” and need to have both the output and error messages saved in the single file named “foo.out”.  How should you run the script?
  11. Write a shell script for rm called saferm which uses the interactive mode only if you use it with more than one filename.  [Hint:  use an appropriate if statement that runs rm -i "$@" when the number of command line arguments is greater than one. ]

To be turned in:

Email your homework assignment, by copy-and-paste (no attachments please!) to .  If possible use the “text” and not the “HTML” mode of your email program.  Please use the subject similar to “Intro to Unix/Linux Homework Assignment #6 Submission”, so I can tell which emails are submitted homework assignments.

Homework assignments will not be returned.  Please do not send as attachments.  Do not send to wpollock@YborStudent.hccfl.edu.  Refer to the Homework and the Submitting Assignments sections of your syllabus for more information.

Confused?  Send questions about the homework assignment to .  Please use a subject similar to “Intro to Unix/Linux Homework Assignment #6 Questions” so I can tell which emails are questions about assignment quiz (and not submissions).