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.
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.
Write a filter that takes a list of files as input and outputs the basename (page 465) of each file in the list.
Write a function that takes a single filename as an argument and adds execute permission to the file for the user.
When might such a function be useful?
Revise the script so that it takes one or more filenames as arguments and adds execute permission for the user for each file argument.
What can you do to make the function available every time you log in?
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?
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.
Write a shell script that displays the names of all directory files, but no other types of files, in the working directory.
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.
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
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.
What might be a problem with running this script twice in the same directory? Discuss a solution to this problem.
Read the bash man or info page, try some experiments, and answer the following questions:
How do you export a function?
What does the hash builtin do?
What happens if the argument to exec is not executable?
Using the find utility, perform the following tasks:
List all files in the working directory and all subdirectories that have been modified within the last day.
List all files that you have read access to on the system that are larger than 1 megabyte.
Remove all files named core from the directory structure rooted at your home directory.
List the inode numbers of all files in the working directory whose filenames end in .c.
List all files that you have read access to on the root filesystem that have been modified in the last 30 days.
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.)
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.