Home > Articles > Home & Office Computing > Mac OS X

  • Print
  • + Share This
This chapter is from the book

Advanced Exercises

  1. Write a sequence of commands or a script that demonstrates that variable expansion occurs before pathname expansion.

  2. Write a shell script that outputs the name of the shell that is executing it.

  3. Explain the behavior of the following shell script:

    $ cat quote_demo
    twoliner="This is line 1.
    This is line 2."
    echo "$twoliner"
    echo $twoliner
    1. How many arguments does each echo command see in this script? Explain.
    2. Redefine the IFS shell variable so that the output of the second echo is the same as the first.
  4. Add the exit status of the previous command to your prompt so that it behaves similarly to the following:

    $ [0] ls xxx
    ls: xxx: No such file or directory
    $ [1]
  5. The dirname utility treats its argument as a pathname and writes to standard output the path prefix—that is, everything up to but not including the last component:

    $ dirname a/b/c/d

    If you give dirname a simple filename (no / characters) as an argument, dirname writes a . to standard output:

    $ dirname simple

    Implement dirname as a bash function. Make sure that it behaves sensibly when given such arguments as /.

  6. Implement the basename utility, which writes the last component of its pathname argument to standard output, as a bash function. For example, given the pathname a/b/c/d, basename writes d to standard output:

    $ basename a/b/c/d
  7. The Mac OS X basename utility has an optional second argument. If you give the command basename path suffix , basename removes the suffix and the prefix from path :

    $ basename src/shellfiles/prog.bash .bash
    $ basename src/shellfiles/prog.bash .c

    Add this feature to the function you wrote for exercise 14.

  • + Share This
  • 🔖 Save To Your Account