--------------------------------------------------- Exercise #5 for DAT2330 - Ian Allen - idallen@ncf.ca ---------------------------------------------------- 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 null, empty, or bad input! Echo user input (including any 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. (If it works on ACADUNIX, it will probably work just fine on Linux.) Test your scripts. The sample inputs and output shown below are not a complete test suite. I will try to find test cases using glob patterns and blanks that will make your scripts abort or misbehave. Scripts without *useful* block comments will be severely penalized. See the script_style.txt file for the expected commenting style. See the file "submit.txt" for details on how to submit your script for marking. ------------------------------------------------------ Write this executable script named "integer_sorter.sh" ------------------------------------------------------ This script will sort three numbers and put the output into a new output file. This scripting is straightforward; the correct sorting algorithm to put three numbers in order is the item most students find difficult to get right. You probably need to write a correct pseudocode/PDL algorithm to do this first, before you write the script to do it. You might also verify your pseudocode with a small C program, first. *) Accept three or less arguments on the command line. If there are less than three arguments, prompt for and read the missing arguments (up to three). (If there are more than three arguments, print an appropriate error and exit the script with return code 2.) In other words: If the user supplies three command line arguments, don't prompt for any input; transfer the three arguments to variables and use them as the three numbers to sort. If the user supplies only two command line arguments, transfer the two arguments to variables and prompt for and read the missing third argument. If the user supplies only one command line argument, transfer it to a variable and prompt for and read the missing two arguments. If the user supplies no command line arguments, prompt for and read all three missing arguments. See the optional_args_*.sh.txt scripts in the Notes for examples of how to do optional arguments. (This section takes approximately 26 lines of shell code.) *) Echo the three input strings back to the user before using them, e.g. You entered these inputs: '156' '23' '9823' *) If any of the three input strings are empty (null string), print an error message and exit the script with return code 2. *) Using a series of IF statements (not the sort command), rearrange the three integers in order from smallest to largest. (This section takes approximately 18 lines of shell code.) *) After the numbers have been put in order, output the numbers in this *exact* output format (spelling and punctuation count): The numbers are, from smallest to largest: 23 156 9823 Of course, the actual numbers will be the numbers entered by the user. *) If the path "out.txt" does not exist, put just the three sorted numbers into the file out.txt, one per line, with the smallest number on the first line and the largest number on the third line in the file. If the path "out.txt" already exists, issue an error message and exit the script with return code 2. (Do not overwrite an existing out.txt file!) BONUS: If out.txt already exists; but, it has a zero size and is writable, you may also safely use it as the output file. *) Count the number of characters in the out.txt file you just created and output this message: The file out.txt contains 12 characters. Of course, the actual number will be the number of characters in your own out.txt file. *) Exit the script with a zero return code if the numbers were already in sorted order. Exit with return code 1 if the numbers were not given in already sorted order. Exit with return code 2 on any errors (e.g. too many arguments, out.txt already exists, etc.). Sorting Hints: The shortest sort solution is based on the "bubble sort" algorithm. Bubble (swap) the largest integer to the top of the list and then put the remaining two numbers in order. You don't need a loop or array here, since there are only 3 numbers. A longer solution is possible using fully nested IF statements to handle all possible combinations of three input numbers; but, less code is better code. ----------- Test Output ----------- Sample test output follows. Do not use the prompt "INPUT" - use a better prompt that tells the user exactly how many things to input. The test cases below are not exhaustive - make sure your program works for *any* three integer numbers, in any order! $ ./integer_sorter.sh INPUT: 1 2 3 You entered these inputs: '1' '2' '3' The numbers are, from smallest to largest: 1 2 3 The file out.txt contains 6 characters. $ ./integer_sorter.sh 1 INPUT: 2 3 You entered these inputs: '1' '2' '3' The numbers are, from smallest to largest: 1 2 3 ./integer_sorter.sh: File out.txt exists. No output saved. $ ./integer_sorter.sh 2 1 INPUT: 3 You entered these inputs: '2' '1' '3' The numbers are, from smallest to largest: 1 2 3 ./integer_sorter.sh: File out.txt exists. No output saved. $ ./integer_sorter.sh 3 2 1 You entered these inputs: '3' '2' '1' The numbers are, from smallest to largest: 1 2 3 ./integer_sorter.sh: File out.txt exists. No output saved. $ ./integer_sorter.sh 3 2 1 0 ./integer_sorter.sh: Expecting 0-3 arguments, found 4 (3 2 1 0) Fix the prompts, above, to ask for the correct number of inputs. Make sure your script sets a correct exit status. Marking scheme: One third of the marks are for the correct output. One third are for useful block comments. One third are for shell code. Failure to follow the script_checklist.txt will lose you many marks. ---------------------------------------------------- Exercise #5 for DAT2330 - Ian Allen - idallen@ncf.ca ---------------------------------------------------- How to submit your script for marking. 1) Make sure your Exterior Assignment Submission Label is near the start of your shell script as comment lines. Use the exact 7-line (plus optional comments) format described online and in previous exercises. See the script_style.txt file for details. Did you follow the script guidelines in script_checklist.txt ? 2) Make sure your script file has read permissions for "group" and "other". 3) Put the output of "pwd" in this directory into a file named "file2.txt". 4) Put the output of running the command "sum *" in the current directory (the directory containing your script) into a file named "file3.txt". 5) Put the output of running the command "wc *" in this directory into a file named "file4.txt". 6) Put the output of running the command "ls -lia" in this directory into a file named "file5.txt". 7) Submit the script and the four other files for marking as Exercise 05, using the following ACADUNIX command line: $ ~alleni/bin/submit 05 integer_sorter.sh file[2345].txt This "submit" program will copy the selected files to my Exercise 05 directory for marking. Your final submission should include only these files: integer_sorter.sh file2.txt file3.txt file4.txt file5.txt You don't need to submit any other files. 8) Verify that you submitted everything, using this command line: $ ~alleni/bin/submit 05 -l You may redo this exercise and re-submit your results as many times as you like. I will mark the most recent submission that is submitted before the final hand-in cutoff date. (Make sure you include all the above files in every submission.) For Exercise 05, always use "05" as the first argument to "submit".