Changing the Unix Prompt
It is easy to get confused in Unix about your current location in the
directory structure. When this happens, commands involving files give
strange error messages that can convince you that your terminal
session has turned evil. There is a simple way to remedy this problem,
change your Unix prompt so it tells you where you are.
Your Unix prompt is an environment variable that you can change. You
do this, on our Unix machines, in the .cshrc file, which runs after
you log in to Unix. Using emacs, look for the line:
set prompt="%m(%h)%
and change it to:
set prompt="[%h] %m %/ $ "
The next time you log in, your new prompt will tell you where you are.
More on the Unix History Mechanism
The history mechanism in Unix can save you a great deal of typing and
aggravation. Simply hitting the up arrow key will cause the last
command to appear on the command line. Pressing this key multiple
times can bring any command you have recently entered back to the
command line.
But if the command you want is really far back in the history list,
using the arrow key can be very tedious. Fortunately, the history
mechanism has another feature that can come into play in these
situations. If you type "history" at the command line, you will see a
list of your last several command line entries. If you then type !n,
where n is the number in the listing, the appropriate command will
appear at the command line. Like so:
[8] blade71 /home/ghoffman $ history
1 15:39 ls
2 15:40 cd lab_1/
3 15:40 pwd
4 15:40 cd ..
5 15:40 cd ..
6 15:40 ls
7 15:40 cd
8 15:40 history
[9] blade71 /home/ghoffman $ !3
pwd
/home/ghoffman
[10] blade71 /home/ghoffman $
Typing "history" can be annoying, so you might consider creating an
alias in your .cshrc file. An alias is a way of associating a Unix
command with another, usually shorter, sequence of characters. For
example, I find typing "ls -l" annoying, so the following alias
appears in my .cshrc:
alias ll 'ls -l'
Similarly, you might create the following alias for the history command:
alias h 'history'
But be careful that you don't use a real Unix command for your alias
unless that is really what you want. What I usually do is type my
proposed alias alone at the command line and if Unix responds "Command
not found." you are good to go:
[10] blade71 /home/ghoffman $ xx
xx: Command not found.
If you completely screw up your .cshrc file, you can always get
another at /usr/local/lib/.cshrc
Moving files from your home computer to the UMB Unix
machines
If you work on your assignments from home, you'll need to use scp
(secure copy) to transfer files back and forth. Here's what you need
to know.
The general format for using scp is:
scp [file] [destination]
Both the file and the destination can be either on your machine or the
Unix box.
A file on the Unix box must be specified by your Unix
username, "@", "users.cs.umb.edu" (the machine you are connecting to)
, a colon (":"), and the file path,
usually relative to your home directory. In specifying this path, the
tilde ("~") username abbreviation comes in handy. For example, you can
specify my home directory with ~ghoffman.
So, if I wanted to copy the file umb.txt in my current directory to
the cs-110 directory in my home directory, I would do the following
[511] glennhoffman - ~/consulting/umb
$ scp umb.txt ghoffman@users.cs.umb.edu:~ghoffman/cs-110
ghoffman@users.cs.umb.edu's password:
umb.txt 100% 1897 1.9KB/s 00:00
[512] glennhoffman - ~/consulting/umb
Notice that command feedback, which lets you know things have gone
according to plan. If you want to copy something FROM the Unix machine
To your home machine, simply reverse the two arguments. You can also
use wildcard characters, so I could transfer all the java files in my
current directory using *.java. You can also use the -r option to copy
whole directories.
One further point. If you are trying to connect from the UMB campus,
where they block the port normally used for ssh, you must include
"-p80" after ssh, to redirect the ssh connection to an open port.
Indenting your code
Proper indentation of code makes a program more readable, and readable
code is easier to debug and maintain than unreadable code. For this
reason, we will be deducting points for improperly indented code.
You should indent whenever the text that follows an open parenthesis
("(") or an open curly brace ("{") extends beyond a single line. You
should also indent the cases in a switch statement and the code blocks
that follow.
Emacs makes this easier to do. If you move your text insertion point
to the far left of the page and then hit the Tab key, emacs will
adjust the indentation for you automatically, except, perhaps, for the
switch statement.
Emacs also has another nice feature that can help with syntax
errors. Whenever you type a close parenthesis (")") or close curly
brace ("}"), emacs briefly highlights the open parenthesis or open
curly brace character that you just closed. To see which open
parenthesis or open curly brace corresponds to a given character,
simply remove the character and type it again, watching for emacs to
highlight its mate.
Using curly braces with for and if statements
Although Java does not require curly braces ("{" and "}" with for and if statements, when what follows is a single line of code, thisis not good practice.Here's why. Let's say the following code snippet appears in your program:
for (int i; i < max; i++)
j += 2;
Fine. Now say a few months later you find a bug and you want to insert a println statement in your for loop to make sure j is really incrementing:
for (int i; i < max; i++)
j += 2;
System.out.println("j = " + j);
The print statement will only execute once even though the for loop may iterate several times. Bugs like this can be VERY hard to find. Save yourself some aggravation, and always use braces. Better still, whenever you close the parenthesis on a for or if statement, enter open and close curly braces like this:
for (int i; i < max; i++){
}
That way you won't forget to close the brace, which is another common compile error.