Exercises

 < Day Day Up > 

1.

Rewrite the journal script of Chapter 8 (question 5, page 335) by adding commands to verify that the user has write permission for a file named journal-file in the user's home directory, if such a file exists. The script should take appropriate actions if journal-file exists and the user does not have write permission to the file. Verify that the modified script works.

2.

The special parameter "$@" is referenced twice in the out script (page 442). Explain what would be different if the parameter "$*" were used in its place.

3.

Write a filter that takes a list of files as input and outputs the basename (page 465) of each file in the list.

4.

  1. Write a function that takes a single filename as an argument and adds execute permission to the file for the user.

  2. When might such a function be useful?

  3. Revise the script so that it takes one or more filenames as arguments and adds execute permission for the user for each file argument.

  4. What can you do to make the function available every time you log in?

  5. Suppose that, in addition to having the function available on subsequent login sessions, you want to make the function available now in your current shell. How would you do so?

5.

When might it be necessary or advisable to write a shell script instead of a shell function? Give as many reasons as you can think of.

6.

Write a shell script that displays the names of all directory files, but no other types of files, in the working directory.

7.

Write a script to display the time every 15 seconds. Read the date man page and display the time, using the %r field descriptor. Clear the window (using the clear command) each time before you display the time.

8.

Enter the following script named savefiles, and give yourself execute permission to the file:

 $ cat savefiles #! /bin/bash echo "Saving files in current directory in file savethem." exec > savethem for i in *         do         echo "==================================================="         echo "File: $i"         echo "==================================================="         cat "$i"         done 

  1. What error message do you get when you execute this script? Rewrite the script so that the error does not occur, making sure the output still goes to savethem.

  2. What might be a problem with running this script twice in the same directory? Discuss a solution to this problem.

9.

Read the bash man or info page, try some experiments, and answer the following questions:

  1. How do you export a function?

  2. What does the hash builtin do?

  3. What happens if the argument to exec is not executable?

10.

Using the find utility, perform the following tasks:

  1. List all files in the working directory and all subdirectories that have been modified within the last day.

  2. List all files that you have read access to on the system that are larger than 1 megabyte.

  3. Remove all files named core from the directory structure rooted at your home directory.

  4. List the inode numbers of all files in the working directory whose filenames end in .c.

  5. List all files that you have read access to on the root filesystem that have been modified in the last 30 days.

11.

Write a short script that tells you whether the permissions for two files, whose names are given as arguments to the script, are identical. If the permissions for the two files are identical, output the common permission field. Otherwise, output each filename followed by its permission field. (Hint: Try using the cut utility.)

12.

Write a script that takes the name of a directory as an argument and searches the file hierarchy rooted at that directory for zero-length files. Write the names of all zero-length files to standard output. If there is no option on the command line, have the script delete the file after displaying its name, asking the user for confirmation, and receiving positive confirmation. A f (force) option on the command line indicates that the script should display the filename but not ask for confirmation before deleting the file.

     < Day Day Up > 


    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    ISBN: 131478230
    EAN: N/A
    Year: 2005
    Pages: 213

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net