We downloaded class as an ordinary text file. To run it as a shell script, you need to give yourself, the user, execute permissions (x) using the change mode command.
chmod u+x class the user is added execute permissions
ls -lrt
-rwxr--r-- 1 rhills 438738691 405 Jun 16 2015 class
ls -lrtFG
-rwxr--r-- 1 rhills 405 Jun 16 2015 class*
If you don't see the x in the 4th character (execute privileges), try:
sudo chmod u+x class sudo executes a command as superuser (Administrator)
Let's execute our program and test different integers as input to the program. The program expects us to pass an integer variable (-5 etc.) on the command line at the time of execution. A single dot (.) is needed in front of the class filename so that Unix will look in the current directory to run this program (this is a safety feature to prevent you from running non-system code).
./class
./class -1
./class 99
./class 199
./class 200000
./class 20000000000000000000
./class 200000000000000000000
./class 2.5
Q4: Looking at the source code in class.csh and rationalizing the above, explain why the last three outputs are incorrect for the integer variable. What's the largest positive or negative number the program can accept?
Some programs can accept (or require) two or more command arguments separated by spaces:
ls ../day1 . (space before single dot)
For unix programs, the list of arguments specified on the command line is stored as a vector array (argv in class.csh, argument vector). For example in C shell:
set array = ("arg1" arg2 3)
echo $array
echo $array[2] # the brackets are calling (referencing) the second element of the array
arg2
echo $#array #the pound sign returns the length (number of elements) in the array. This is useful for error checking, as in when class.csh complains if it doesn't see an input number (line 2 of code: if-then statement).
3
Arrays are useful when you have a large number of data and don't want to create extra variables.
Q5a. Looking at the nested if-then statements in the CLASS program, which lines of code are executed before the program terminates if you don't specify an argument when calling the program? What happens if you give CLASS two arguments?
The else statements allow us to treat each possible input case specified to prevent the program from terminating due to an error. Copy the program to a separate program:
cp class class2
vim class2 Edit class2 to make a new program...
Q5b. Modify the function of some statements in class2 to create a new result obtained by the program. Don't just change the category cutoffs. For example, you could add in a fifth category or enable the program to test two numbers on the same command line. The final program should not crash (terminate without completely executing) or give a Unix shell error, but may give a misleading result. Explain in which situations your modified program arrives at a different result than the original program. Paste your source code into your lab report.
./class2 <input numbers> show how your output is different
diff class class2
The diff command will show lines that you've changed. With syntax highlighting on, you could even try:
vim -d class class2
You've now opened two Vi instances (switch between them using Control-W-W). Try scrolling the cursor in one using the hjkl keys. When you're done, type :q enter followed by :q enter to quit both windows.
If-then statements can employ logical operators. For example AND: '&&' means only if both conditions are true, and OR: '||' which evaluates successfully if either or both conditions are true. There is also negation (!), identity (==), etc. (>=, <=).