CSC 325 Operating Systems with an Emphasis on UNIX
Assignment 8
This assignment will use skills learned in the past assignments to continue working
with bash scripting. In this assignment, we will learn about using the essential programming structures: assignment statements, conditional statements, and loops.
It will also introduce debugging.
- Open a text document, and then use ssh to logon to your Linux account on cs.berea.edu.
- You have already learned that man is short for manual which is a set of online manual pages for most commands in Unix-like environments. The man pages usually tell you more than you far more than you want to know about its syntax and flags (options).
help is like UNIX's man command except it shows you help files for some of the special commands that were not a standard part of UNIX or for for which man pages were not included. For this reason, when I have a question, I usually check a man page first and then check help. Read man if, noting that this page relates to perl scripts. Then read help if which relates to bash scripts. Write a script using your favorite editor using an if/then/elif/else construct that prints information about the
current month. The script should print the number of days in this month, and give information about leap years if
the current month is February. Be sure to add comments in your script. Also, add information for the users of your script. Change the
permissions on your script and mv it to your own bin directory so that you can run it. Then run the script as you would normally. Copy this script to
your text document.
- Follow the same procedure this time, using a case statement involving an alternative use of the date command. Again, copy the script to your text document
- Read http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html
to learn about the debugging bash scripts. Run the script you wrote in number 3 in debug mode by typing bash -x scriptname. It should run without errors, but you should see the code traced.
- Next, make errors in your script and use the debugger as a tool to help you find the problem. Try each of the following: see what happens if you misspell commands, if you leave out the first line or put something
unintelligible there, or if you misspell shell variable names or write them in lower case characters after they have
been declared in capitals. Write several sentences in your text document
about whether you find the debugger helpful with each of these errors and why or why not.
- There are several ways to create variables. One way to set a variable in the shell is to use
VARNAME="value". Note that bash is not nice about white space and putting any spaces around the equal sign will cause errors. Another way to create a variable is to use set.
At your command-line, use set to create 2 variables, VAR1 and VAR2, initializing them to hold the values "thirteen" and "13" respectively. Then create VAR3 without using set, initializing VAR3 to "Happy
Birthday". Next, use set with no parameter in order to display the values of all three variables. Shell variables can be global or local. Explain when a variable would be local, when it would be global, and precisely desribing the scope of each of the three variables you just created.
- Try to use unset to destroy these varables. Explain what you try and what happens.
- Write a script in which takes one input parameter. Inside the script create a variable called PI which is initialized to 3.141592.
The script should then calculate the area of a circle and be aired with comments and elegant output. Copy your
script to your text document.
- Can you find an alternative for wc -l, using grep? You
may want to look at man wc and man grep for help. When you find an alternative,
copy it to your text document.
- Read help read. Using user input, write a script that asks
for the user's age. If their age is equal to or higher than 16, print a message saying
that this user can legally get a driver's license. If the user's age is below 16,
print a message telling the user how many years he or she has to wait before
legally being allowed to drive. Also, given that life expectancy here in the US is 77 years, compute how many more years they are expected to be able to drive.
- Write a script that takes exactly one argument, a directory name. If the number of arguments is more or less than
one, print a usage message. If the argument is not a directory, print another message.
For the given directory, print the five biggest files and the five files that were most recently modified. Copy your
script to your text document.
- Write a script that does the following
- Display the name of the script being executed.
- Display the first, third and tenth argument given to the script.
- Display the total number of arguments passed to the script.
- If there were more than three positional parameters, use shift to move all the values 3 places to the left.
- Display all the values of the remaining arguments.
- Finally, display the number of arguments remaining.
- Test with zero, one, three and over ten arguments.
Copy your script to your text document.
- Comment in your text document on which of the scripts in this assignment was the most interesting to write and why.
- Save your text document as yourlastnameA8.txt.