By the time you have completed this lab, you should be able to
Let the person who was not the first pilot last week be the pilot first this time. Switch roles after awhile, before the pilot gets tired, and before the navigator gets bored or distracted.
If your regular partner is more than 5 minutes late, ask the TA to pair you with someone else for this week.
This lab's pilot should log in. You will (both) work in this account for the rest of the lab.
Create a ~/cs32/lab02 directory and make it your current directory:
mkdir ~/cs32/lab02 cd ~/cs32/lab02
Recall (from lectures/readings) that a script is a collection of shell commands and control structures. In fact a script is like a program written in the language of a particular shell. The shell interprets the script, and executes the commands in the order prescribed by the program. Any sequence of shell commands that are stored in a file can (basically) qualify as a script.
Use an editor (e.g., emacs) to create a file named rootcontents.sh - then type just the following two lines in it, and save the file:
cd / lsNow tell an appropriate shell to execute it, like
sh
for instance
(user input is bold):
bash-4.2$ sh rootcontents.sh bin cgroup dev fs lib lost+found mnt proc sbin srv tmp var boot cs etc home local media opt root selinux sys usr
To repeat: "you must tell an appropriate shell to execute it." The point is that the shell is the executing program, and the script is just the specific instructions that program is asked to execute. The default shell will run it if you don't specify:
bash-4.2$ ./rootcontents.sh bash: ./rootcontents.sh: Permission denied
Oops, first we must change the permissions of the script file
to make it executable. Then bash
, our default shell, will execute it:
bash-4.2$ chmod 755 rootcontents.sh bash-4.2$ ./rootcontents.sh bin cgroup dev fs lib lost+found mnt proc sbin srv tmp var boot cs etc home local media opt root selinux sys usr bash-4.2$ pwd /cs/class/cs32/labs/lab02
NOTICE TWO IMPORTANT THINGS:
pwd
and found
out our current directory was still local to our account, and not the root
(/
) as specified by `cd /
` in the script.
Do you know why? Think about it, then click here for the answer. |
Fix the script now (as per important thing #2) by typing the following at the very start of rootcontents.sh:
#!/bin/sh
Then run the script again to verify it still works. The shebang must be the very first line of the script, and it must not even have any spaces before it.
Normally you would follow the shebang with a comment. In keeping with this convention, add the following comment (begins with '#') right after the shebang as shown - substituting your actual name(s), of course:
#!/bin/sh # Very simple script to display the root's contents # YOUR NAME(S), 10/11/2012
Sufficient sh scripting techniques were presented in lecture to handle the required in-lab work of Step 4. In case you missed it, here is a link to the demo scripts presented in lecture. Additional techniques might be required for the after-lab tasks though, so here is a short sh script primer, and here is a link to a nice bash scripting tutorial (bash scripts are basically identical to sh scripts).
Peruse/review these materials now, and then refer back to them if necessary to complete the rest of the lab work.
Also remember that shell scripts just contain shell commands, and that these commands can
be tested interactively at the shell prompt. In other words, don't be afraid to try stuff
before including it in your script. Use the same continuation character you use for C programs,
'\
', to continue long lines (or control structures).
SWITCH ROLES WITH YOUR PARTNER now if you have not already done that. Please remember to do so frequently in every lab, and we promise to stop pestering you about it. Thanks!
Write a script named listwritable
(most scripts don't have extensions like .sh)
to list the writable items in the directory specified by the user at the command line.
For example, assuming that listwritable has its permissions set to execute, then the
following command would print the names of just the writable items in the directory
named myfiles
:
./listwritable myfiles
Here are two sample runs, one for the current directory ('.'), and one for the root:
bash-4.2$ ./listwritable . listwritable rootcontents.sh bash-4.2$ ./listwritable / tmp
This in-lab part does not have to do any error checking. For example, if the user does not specify a command line argument, or if the argument is not a readable directory name, then the script's behavior may be unpredictable. That's okay for now.
Be sure to include the shebang line at the beginning of the script.
Also include a comment with your name(s) and the current date. Please do this for every file you create in CS 32 labs in the future too. Thanks.
Make the file executable - type 'chmod 755 listwritable
' - then test it a few times
until you are sure it is working, and leave the terminal window open for the TA to see.
Get your TA's attention to inspect your work, and to record completion of your in-lab work. |
Don't leave early though ... begin the after-lab work below.
Step 5a. ONLY IF YOU RAN OUT OF TIME TO HAVE THE TA INSPECT YOUR WORK
If you must complete this assignment at CSIL, then submit it with the turnin program - but do NOT turn it in if the TA already checked you off. You MUST have both your name and your partner's name in the file in order to receive credit. Remember that the original pilot needs to do this step, since that is whose account you have been using in Cooper Lab.
Bring up a terminal window on CSIL, and cd into the original pilot's cs32 directory, and cd again into the lab02 directory. Then type the following to turn in your version of listwritable:
turnin lab02@cs32 listwritableRespond "yes" when the program asks if you want to turn in (be sure to read the list of files you are turning in), and then wait for the message indicating success.
Each pair of students must accomplish the following to earn full credit for this lab:
This week the homework is (mostly) to add features to the listwritable script, as described in these hw2 instructions. You will use the turnin program to submit your enhanced script, and that must be done before your next lab - the TA will check for it then. A short written part is also required, to be turned in at your lab next week. Please do NOT print the homework in Cooper Lab.
Each student must complete the paper part of the homework, even if you do the script part in pairs.
Write a script named sumargs to sum the arguments entered on the command line (assume the user enters
nothing but numbers). Use the expr
command to do simple calculations in a script,
as demonstrated in the multi demo script.
Example sumargs runs should look like this:
bash-4.2$ ./sumargs 75 6 -9 sum is 72 bash-4.2$ ./sumargs sum is 0
The user did not enter any arguments in the second run, so the sum is 0.
Want/need more to do? Why not write a script named calcargs that will perform different calculations
on the arguments. Perhaps provide a menu to give the user a choice to add, subtract, multiply or
divide the arguments. See the menu demo script for an example
menu and use of the read
command to get user responses. Add error checking if you
want to be proud of your script!
Prepared by Michael Costanzo.