To use a Unix machine, you must first log on. This includes identifying your user name and your password to the machine. Once you have been identified and validated, Unix presents you with a shell through which you can execute commands. There are several kinds of Unix shells. Originally, there was the Bourne shell, but some people didn't like its command syntax, and so they invented the C-shell (my personal favorite). Also, there is the Korn shell, which many people are trying to push as the new Unix shell standard (I don't like itþI think it's korny). Each shell has a set of initialization files, much like CONFIG.SYS and AUTOEXEC.BAT for MS-DOS. The Bourne and Korn shells use .profile, and C-shell uses .cshrc and .login. The commands in these files are executed when the shell starts up. When the shell is ready, it gives you a prompt, and you can type commands for the shell to execute. When you are done, you must log off from Unix (or the next person in line will be able to use your account). To log off, you can type "exit" or "logout" or ctrl-D (hold the control key and type "D").
If you log on at the console of one of our HP machines, you enter the Visual User Environment. This is an X-Windows based graphical user interface. In many respects, VUE is itself another kind of shell. Is has a file manager, built-in text editor, and so forth. However, you can also create windows that are running shells, and you can use those windows just like regular terminals.
The main Unix help facility is its on-line reference manual. You can read about a subject by typing "man subject". For example, "man ls" pulls up the reference page for the "ls" command. I use man almost every day. Man uses the "more" command to let you page through the reference pages. At the "more" prompt, you can type space-bar to view the next page, or return to view the next line, or "q" to quit viewing.
See the Unix filesystem section for a description of these commands.
Unix is a multi-tasking environment. There are always a number of different programs running at the same time. Each Unix program is called a "process". Each process has a "process ID" or "pid" associated with it. You can look at processes that are currently executing by using the "ps" command. Examples:
ps list your processes ps -edf list all processes, in a long format
Depending on the particular Unix system you are using, the arguments to "ps" may be very different. The examples above are for an HP-UX system. On a Sun SPARCStation, you would type "ps -aux" to get a full process listing. To learn about "ps" on your machine, type "man ps".
You can send a signal to a process you own with the "kill" command. There are lots of signals, but you only need to worry about two right now. The default is to send an interrupt signal. You can do this by typing "kill pid" where pid is the process ID number of the process you want to terminate. If that doesn't work, you can try "kill -9 pid", which sends an uninterruptible termination signal to the process. If the process is still around after a kill -9, then it is either hung up in the Unix kernel, waiting for an event to complete, or you are not the owner of the process (kill will tell you if you try to kill something you don't own).
You can find out who is logged on to the system with the "who" command. You can find out what each person is doing with the "w" command. (Or you could type "ps -edf | grep userid" to look at a given user's processes.)
Every Unix process has three streams associated with it: stdin, stdout, and stderr. The stdin stream is associated with the input of the process (the keyboard by default). A program can read its stdin stream to get user input. A program writes to its stdout or stderr streams to produce output for the user to see (stdout and stderr go to the screen by default). You can redirect stdout to a file (instead of to the terminal screen) using the ">". For example: ls > ls.out places a directory listing in the file called "ls.out". You can redirect stdin to come from a file (instead of from the keyboard) by using the "<". For example: cat < .cshrc types out on the terminal screen the contents of the file ".cshrc". In the C-shell, you can redirect both stdout and stderr to the same file using ">&". (The Bourne and Korn shells have a different mechanism.)
A pipe is very useful. It sets up the output of one command as the input to another command. You set up a pipe using "|". For example: ps -edf | grep cs290 This command takes the output of "ps -edf", and sends it as the input to "grep cs290". This command will list all processes whose owner is user "cs290". Another example: ls -al | more This command takes the output of "ls -al" and sends it as the input to "more". This command lets you page through the output of a long directory listing. The C-shell also lets you use "|&" to pipe both stdout and stderr into a command. Again, the Bourne and Korn shells use a different mechanism for piping stderr.
The program "more" lets you view a file. It has been described above. To see all the options for "more", type "man more". Another program you can use is "cat". Cat is like the MS-DOS "type" command. It simply prints the contents of the files you list. For example, "cat file1 file2" will print the contents of file1 and file2, but it doesn't pause after every page like "more" does.
You can use "vi" to edit a file. Read the vi section for a tutorial on vi. HP's VUE also has a built-in text editor. Click on the Pencil & Paper icon to start the VUE text editor.
The C++ compiler is called "CC". To compile a C++ source file called prog.C, you would do this: CC -c prog.C This creates an object file called prog.o for your source program. However, you still don't have an executable program. To run your program, you must link it. You do this as follows: CC prog.o -o prog This creates an executable program called "prog" from your "prog.o" object file. If you had several modules, your link command might look like this: CC prog1.o prog2.o prog3.o -o prog. Type "man CC" to see all the options for CC.
Make is a utility that helps you maintain your files that depend on other files in the system. For example, when you make a change to a source code file, you now need to recompile it and relink your executable program file. Rather than keep typing the compile and link commands by hand, you can get "make" to do it for you. You give "make" its instructions in a file called "Makefile". Instructions have the form:
target: dependency list instructions
where target is the file that you want to automatically update, and dependency list is the set of files the target depends on, and instructions are commands that get executed when "make" determines that a target is older than one of its dependencies. For example, consider a program called prog, which gets linked from prog.o, which gets compiled from prog.C and prog.H. Its Makefile would look like this:
prog: prog.o CC prog.o -o prog prog.o: prog.C prog.H CC -c prog.C
There are other ways to do this, but this way does work. You invoke "make" by simply typing "make". It goes to the top of the Makefile and sees if the first target needs to be updated. It does this by recursively checking the files in the dependency list. In our example, it sees whether it needs to update "prog" by checking whether prog.o is up to date. It checks the timestamp of prog.o and sees if it's older than prog.C or prog.H. If so, make first updates prog.o by executing "CC -c prog.C". Next, if prog is older than prog.o, "make" updates prog by executing the command "CC prog.o -o prog".
Most computers in the Computer Science Department are connected to a world-wide network called the Internet. If you have an account on some other machine, you can log on to that machine remotely using telnet or rlogin. First, you must be logged on to a machine (your local machine). Then, you can type "telnet hostname" where hostname is the machine ID you're trying to log in to. For example, suppose you're logged on to breezy in the lab. You could type "telnet warm" and you'll get a log in prompt for the host named "warm". You type your user name and password as usual. You should see a shell prompt where you can type commands. To terminate the connection, exit from the shell as usual ("exit", "logout", or ctrl-D).
You can also use rlogin to connect remotely. For example, type "rlogin warm". The difference is that rlogin assumes your user ID is the same on the remote host, and so it just asks for the password. If you type the wrong password, rlogin asks for user name and password, just like telnet. Rlogin and telnet use different protocols, but they are very similar in their effect.
There are several e-mail programs you can use. The traditional programs are mail and mailx. You'll find those programs on most kinds of Unix machines. To read your mail, type "mail". To send mail, type "mail address", where address is the e-mail address of the person you want to mail something to; then type your message, and when you're done, type a period (.) on a line all by itself.
On the HP machines, I prefer to use elm for my mail. Just type "elm" (or click on the letter icon in the workspace manager), and the elm interface will appear. Elm is menu-driven, so you can play with the menu choices to figure out how to use it. Also, you can type "man elm" to learn more about it.
You can practice by mailing messages to yourself, or if you prefer, you can send a message to webmaster@www.cs.byu.edu.
To connect to a host, type "ftp hostname". For example, "ftp hot". Ftp will ask you for a user name and password. If the host supports anonymous ftp, you can give "anonymous" for the user ID, and your e-mail address for the password. Once you're connected to the ftp host, you can do a directory listing (dir), change directories (cd), and retrieve files (get filename). Type "?" at the ftp prompt for a list of commands. You can type "help command" to get a description of the command. When you're done, type "quit" to exit. Again, remember that "man ftp" will show you the Unix reference manual page for ftp.
In the Unix filesystem, there are files and directories (and some other things I won't talk about).
Directories are organized in a hierarchy. You are assigned a directory as your "home directory". You own this directory and everything subordinate to it. When you first log in, Unix sets your current working directory to your home directory. You can change the current working directory with the "cd" command. Examples:
cd change to home directory cd directory change to directory cd .. change to parent directory cd ~/bin change to the bin directory underneath your home directory
If you set the "cdpath" variable properly, cd will search the cdpath for the directory you've specified. For example, if your home directory is /users/liddl, and you have a /users/liddl/cs/cs290 directory, and you set cdpath to contain ~/cs, then typing "cd cs290" will take you to /users/liddl/cs/cs290 from where ever you are. Without cdpath, you'd have to do either "cd /users/liddl/cs/cs290" or "cd ~/cs/cs290". Note that "~" represents your home directory, "." is the current directory, and ".." is the parent directory.
You can print the current working directory with the "pwd" command. You can make a new directory with the "mkdir" command, and you can remove an empty directory with the "rmdir" command. (There's a way to do it with "rm" if you want to explore it. I'm not going to tell you what it is because it could be dangerous for beginners.)
Unlike MS-DOS, directories under Unix can be renamed and moved around with a single command. For example, I could do the following to put cs290 under my home directory:
mv ~/cs/cs290 ~ OR cd ~/cs; mv cs290 ..
Files can be moved around in a similar way. To rename a file under Unix, you use the "mv" command. For example, to change the name of file1 to 1file, do this:
mv file1 1file
You can also copy files with the "cp" command. Example:
cp file1 1file
To copy everything from the cs290 directory into the tmp directory under your home directory (assuming the tmp directory already exists), do this:
cp ~/cs/cs290/* ~/tmp
Each file/directory has a set of permissions associated with it. Every file/directory also is assigned an owner and a group ID. You can look at the files in your current directory with the "ls" command (e.g., "ls -l"). The first 10 letters of each line from "ls -l" show the file/directory permissions. The first letter is "d" for directories or "-" for regular files. The next 9 letters are grouped into three rwx-triples. The first triple indicates the file/directory owner's permissions. The second triple indicates the group's permissions. And the third triple gives permissions for users who are not the owner and who are not in the same group. "r" stands for read permission, "w" for write permission, and "x" for execute permission. You can't search a directory unless you have execute permission for it. Here are some commands to change file attributes:
chown change owner chgrp change group chmod change permissions
Look up these commands in the on-line manual pages by typing "man chown" or "man chmod".
Files with a period as the first letter of the file name are hidden files (compare the difference between an "ls" and an "ls -a"). Here are some useful options to the "ls" command:
a show hidden files l show long listing C show short listing (in columns) F mark files by type (/ after directories, * after executable files, etc.) t list in order of time (creation) r list in reverse order R list recursively
These options can be combined. Here are some I've found useful:
ls -al ls -l ls -C ls -lrt ls -lt ls -lR
I've aliased "ls" to "ls -F" in the default .cshrc file I've given you.
You can delete a file with the "rm" command. Be careful if you type "rm *", because Unix doesn't ask you if you're sure you want to do that. Also, there is NO UNDELETE MECHANISM for the Unix filesystem. When a file is gone, it's gone! Sayonara, hasta la vista, bye-bye-baby! BE CAREFUL. ONLY DELETE WHAT YOU WANT TO DELETE. If you're unsure about whether a wildcard will pick up only what you want, do an "ls" with that wildcard first, to see what it matches, then run the "rm". THERE ARE NO SECOND CHANCES.
Students are responsible for backing up their files in case their files are lost for whatever reason. The HP systems have a 3 1/2 inch floppy disk drive that can be used for archiving files. Insert a floppy diskette into the system and use the following commands to prepare the floppy for use:
mediainit -v -i2 -f16 /dev/rdsk/floppy newfs -n /dev/rdsk/floppy ibm1440
Now the standard file system commands (descirbed above) can be used to copy files to the floppy disk.
The standard Unix editor is "vi". The name came from the fact that it was a visual version of the underlying editor "ed." In its day, vi was a vast improvement, providing a wonderful front-end to the powerful ed features. You won't learn to love vi until you love Unix and all its two-letter commands. But here's how to use vi in a nutshell.
To edit a file, type "vi filename".
Vi has two modes: insert and command. In insert mode, whatever you type becomes part of the document. To leave insert mode, you press the escape key. There are several ways to enter insert mode, but the most common is to type "i".
Some vi commands are executed at the vi ":" prompt. To get this prompt, just type the colon key (":"). You can save your changes by typing ":w". You can save changes and quit with ":wq". Here are some colon-commands I use often:
:e filename edit another file :e! filename edit another file without saving current changes :e! edit this file again without saving current changes :q quit vi :q! quit vi without saving current changes :w write changes to the current file :w filename save to a new file name :w! filename overwrite the contents of an existing file :wq save changes and quit :!command execute command in a sub-shell (e.g., :!ls) :r filename retrieve a file into the current buffer after the current line
How to get around in vi:
h, j, k, l move left, down, up, right
arrow keys move left, down, up, right
space move right
return move down
w move forward one word
b move backward one word
W move forward one word including punctuation
B move backward one word including punctuation
NNNG go to line NNN
. repeat previous action
cw change word (type ESC to exit insert mode)
cW change word including punctuation
R enter replace mode (type ESC to leave replace mode)
A append to the end of a line (type ESC to leave mode)
I insert before first non-white-space character
i insert before current character
a insert after current character
dd delete line
dw delete word
dW delete word including punctuation
D delete to end-of-line
o open a new line beneath current line
O open a new line above current line
/regexp search forward for a regular expression (pattern)
?regexp search backward
% find matching (), {}, or []
~ convert between upper and lower case
^F scroll forward
^B scroll backward
^D scroll down half-page
That's more than enough to get you started. There are LOTS more features than what I've described. Happy editing! ;-)