Problems 1
Home Up Review Problems 1 Problems 2 Problems 3 Problems 4 Problems 5 Problems 6 Problems 7 Problems 8 Problems 9 Problems 10 Problems 11 Problems 12 Problems 13 Problems 14 Problems 15 Problems 16 Subnets

 

Shell Skills & Shell Scripts

  1. Can a file and directory have the same name?
    Why, or why not?
  2. Count how many Unix manual pages contain the word "file" in their descriptions.
  3. What is a safe way to find out what files a shell pattern matches?
  4. What do these shell file patterns match?
  5. ~
    *
    .*
    .??*
    ../*
    ./* 
  6. What is the difference, if any, between these Unix pathnames?
    Could they refer to different files?
  7. .profile
    ./.profile
    ././././.profile 
  8. If "data" is a subdirectory of the current directory, then what are the
    differences, if any, among the following commands?
  9. $ ls data/..
    $ ls .
    $ ls 
  10. What single Unix commands would you use to:
  11. change to your home directory
    display the list of files in the current directory
    display "my name is fred" onto the screen
    put the file "tmp.dat" in the current directory down into the directory "data" underneath your home directory, leaving a copy behind in your current directory? (Both the current directory and the "data" subdirectory each have a separate copy of the file.)
    put the file "tmp.dat" in the current directory down into the directory "data" underneath your home directory, without leaving a copy behind in your current directory? (After the command, the file will only exist in the "data" subdirectory.)
  12. What will the following Unix commands do?
    (Don't execute a Unix command if you aren't sure what it is going to do!)
  13. rmdir ~ 
    cat /etc/passwd 
    ls ../../fred/doc/tmp 
    diff .profile .profile
    sort /etc/passwd
    sort /etc/passwd >happy 
  14. One of your users has created a file called "-tmp", with a leading dash.
    (The command "echo hi >-tmp" will do it.) They now want to get rid of
    this file. Why might the user have difficulty removing this file? (Try
    it yourself.) How would you remove the file?
  15. Sometimes the tabs on your terminal aren't set correctly, causing
    text to move off the right edge of the screen and the screen to look
    badly formatted. Despite its name, the Unix "reset" command may be
    a cause of this. It happens like this:
  16. $ reset
    $ who
    ian        console      Oct  1 08:31            (
    :0)
    ian        pts/7        Oct  1 08:38            (
         )
    ian        pts/5        Oct  1 08:31            (
    :0.0)
    ian        pts/2        Sep 27 00:32            (
    sun4-4a:2.0)
    ian        pts/6        Oct  1 08:31            (
    :0.0)

    The file /usr/lib/tabset/vt100 contains some mostly unprintable control characters that, when displayed on your terminal, will reset the tabs correctly on your terminal.

    $ cat /usr/lib/tabset/vt100
    $ who
    ian        console      Oct  1 08:31    (:0)
    ian        pts/7        Oct  1 08:38    (     )
    ian        pts/5        Oct  1 08:31    (:0.0)
    ian        pts/2        Sep 27 00:32    (sun4-4a:2.0)
    ian        pts/6        Oct  1 08:31    (:0.0)

    Following the example at the start of Chapter 10 in the text,
    write an executable shell script file named "tabset" that displays the
    contents of this tab fixing file. Copy and modify the script "whocat"
    developed in class as a starting point. (Do not change the contents of
    whocat.)

    For best results, a shell script file must start with a first line
    saying: #!/bin/sh (see p.285/286)
    The line must start in the first column; no blanks allowed.
    You must turn on execute permissions on the file to run it.

    Put your new "tabset" shell script in a directory named "bin" under
    your home directory. (You may have to create "bin" first.)

    Add your "bin" directory to the beginning of your search path by
    modifying the PATH variable in your shell (p.293). Do not put a colon
    (':') as the last (or first) character in your PATH. It is a security
    risk to have the trailing colon, since it means that files in your
    current directory may be found and executed as commands.

    Make sure that typing "tabset" at the shell prompt causes your new
    "tabset" command to execute from within your "bin" directory.

    Modify your .profile file to add your "bin" directory to your PATH for
    all your logins. Telnet to your machine, login (which causes your new
    .profile to run), and make sure that typing "tabset" at the shell
    prompt causes your new "tabset" command to execute.

  17. The "cat" command has an option to turn control characters (normally invisible) into printable escape sequences. Using this option, you can see printable representations of the unprintable characters in the file /usr/lib/tabset/vt100.
  18. What is the option letter that does this?

    When you display the file with cat using this option, does the file still reset the tabs on your terminal? How can you know?

  19. Sorting a file will place lines that are identical one after the other. (They sort to the same place.) The "uniq" command removes duplicate lines from a file, if they are one after the other. The "wc" command counts lines. Write an executable shell script called "uniqwc" (unique word count) that uses the "sort", "uniq", and "wc" commands to count the number of unique executable lines read from standard input.

    You would use your shell script like this:

  20. $ cat .profile | ./uniqwc 
    22    87    560
    $ ./uniqwc <.profile
    22    87    560 

    (Your output may differ, depending on the exact contents of your file ".profile".)

    For example, given the following input file:

    a
    b
    a
    b
    c
    b
    c 

    the output of your shell script should be: 3 3 6
    (The first number 3 correctly indicates the input has three unique lines.)