======================= CST8129 Lab Exercise #6 (Week 8) ======================= -IAN! idallen@ncf.ca Due: 14:00 (2pm) Friday, October 18, 2002 Marks: 2% Late penalty: 50% per day Purpose: - practice writing real shell scripts (material in Chapter 8 and 9) Hand in format: online submission only - no paper, no diskettes All scripts described below must be written to conform to the script writing checklist: script_checklist.txt and to the script style given in: script_style.txt. All user input (command line arguments or input via "read") must be fully validated before being used in expressions. Do not process bad input! Echo user input (including command line arguments given) back to the user. This is usually a good idea both for debugging your script and giving the user feedback on what data the script is actually processing. Avoid Linux-only commands and command options. The same script should work without modification on both Linux and ACADUNIX, where possible. (In particular, do *not* use Bash 2.x shell syntax!) Don't assume you can write temporary files into the current directory, unless the script directions say it is safe to do so. Test this by changing directories to a directory into which you cannot write (e.g. /etc or /home) and running your script from there. If you need to write temporary files, write them under "/tmp/" using a unique file name. Real-world scripts must check the error returns on the commands they use. (This is illustrated in the files script_style.txt and exit_status.txt.) At minimum, exit the script if an important command fails. Don't check the exit status of "unimportant" commands, such as most "echo" commands that print on the screen. ------------------ Hand in directory: ------------------ Completed scripts must have permissions "read-write-execute-only" for you, "read-only" permissions for group, no permissions for other people. The following directory is ready to receive your completed scripts: ~alleni/cst8129/02f/lab06exercise/xxxxnnnn/ where xxxxnnnn is your Algonquin userid (e.g. abcd0001). When you have completed a script, copy it into the above directory: cp myscript.sh ~alleni/cst8129/02f/lab06exercise/xxxxnnnn/myscript.sh Files with the wrong name or wrong Unix permissions will be penalized. -------------------- Write these scripts: -------------------- --) Write this executable script named "10_top_five.sh" Validate arguments: Verify that the script has exactly one command line argument. Echo this argument to the user. Then check that the argument is the name of a readable, non-empty file. (It must be a file; check to make sure it is a file.) Sort the data from the file (do not change the original file) and display the first five lines of sorted data. The script should exit with a zero exit status unless there is an error (wrong number of arguments, not a file, file is not readable, file is empty, etc.). --) Write this executable script named "11_string_compare.sh" Make sure there is exactly one command line argument; echo it to the user. Prompt for and read a string. Echo the input read back to the user. Compare this string against the first command line argument and print whether or not the string is an exact match: $ ./foo 'this is four' Argument one is: this is four Enter your one line of input: this is three You entered: this is three String 'this is three' does not match argument 'this is four'. $ ./foo "happy coding" Argument one is: happy coding Enter your one line of input: happy coding You entered: happy coding String 'happy coding' is a match. $ ./foo ./foo: Expecting one argument, found 0: ()" $ ./foo one two "th ree" ./foo: Expecting one argument, found 3: (one two th ree)" The script should exit with return code zero if the strings match and non-zero otherwise. --) Write this executable script named "12_optional_arguments_demo.sh" This script takes zero to three arguments. Without arguments, the contents of three predefined shell keyword variables are displayed. Each command line argument replaces one of the variables in the output: With no arguments, display: PWD MAIL HOME With 1 argument, display: arg1 MAIL HOME With 2 arguments, display: arg1 arg2 HOME With 3 arguments, display: arg1 arg2 arg3 Examples: $ ./foo Output is: /home/idallen/tmp /var/spool/mail/idallen /home/idallen $ ./foo blue Output is: blue /var/spool/mail/idallen /home/idallen $ ./foo blue green Output is: blue green /home/idallen $ ./foo blue green red Output is: blue green red $ ./foo blue green red yellow ./foo: Expecting less than 4 arguments; found 4 (blue green red yellow) Exit with zero status unless there is some error. Restrictions: You may only expand $PWD, $MAIL, and $HOME *once* each in the script. You may only use *one* echo command to produce the output and *one* echo command to produce the error message. Hints: Use three local variables to hold the three output values. Initialize the variables to the values of PWD, MAIL, and HOME at the start of the script. Depending on the number of arguments, replace one or more of the variables with the values from the command line. At the end of the script, use the three variables in your one "echo" output line. --) Write this executable script named "13_path_validator.sh" The purpose of this script is to validate a single command line argument. If the pathname is a file, make sure it is readable, writable, and not zero size. If the pathname is a directory, make sure it is readable and searchable. Print appropriate messages if any of these validations fail. Exit non-zero if any of the tests fail. If the argument passes all the tests, exit with zero status. --) Write this executable script named "14_file_size_classer.sh" Make sure there is exactly one command line argument. Make sure it is readable and that it is a file (make sure it is a file). Classify the file according to the count of the number of lines it contains: 0-99: small 100-499: medium over 499: large Sample output: $ ./foo a File 'a' contains 4 lines and is: small $ ./foo b File 'b' contains 500 lines and is: large $ ./foo c ./foo: Argument 'c' is not a readable file. $ ./foo /bin ./foo: Argument '/bin' is is a directory; nothing done. $ ./foo a b c ./foo: Expecting 1 argument; found 3 (a b c) Exit with a zero status unless there is an error. --) Write this executable script named "15_two_number_sort.sh" Get two inputs from the command line and/or by reading from standard input (via the "read" command). If not enough inputs are given on the command line, prompt for the missing value(s) and read them from the user. (See the sample scripts optional_args_*.sh.txt.) The two inputs will be treated as two integer numbers. Check that the two inputs are really valid integers. (See the sample script test_numeric.sh.txt.) Output the smaller number followed by the larger number, in this form: You entered 100 and 12. In order, they are: 12 100 You may use only one "echo" statement to produce the above message. The "echo" statement that produces the standard output of the script must appear in only one place in the script. Hint: Use variables to hold the smaller and larger numbers. Use the two variables in the "echo" statment at the end of the script. Exit zero unless there is an error (e.g. too many arguments, non-integer input, etc.).