Notes on using Solaris


Table of contents


Logging in

Your login id and password are given to you by your instructor. Type the login id, then a return key, then your password. Passwords are case-sensitive. Be sure that the caps-lock key is not turned on. If you use the number pad, be sure that the num lock is on.

When you first log in, you will be given a choice of Common Desktop Environment (CDE) or Gnome. Choose Gnome.


Logging out

Please log out when you leave. To log out, select Actions->Log out from the menu at the top of the Gnome screen.


Storage of your files

When you log in, your current directory will be set to your home directory. (A directory is the same thing as a folder.) Files that you store there will remain there until you delete them.

The lab uses a network file server. Whichever computer you are on, you will see the same view of your files. They are not stored on the computer on which you are working.

Please do not be extravagent with disk usage. You can use the du command to find out how much space you are using. Command du -s -k will print a single usage number, the number of kilobytes that you are using in the current directory.


Command line interface

You are probably familiar with point-and-click computing using an end-user operating system such as Microsoft Windows or MacOS. Graphical user interfaces can be very convenient, but there is another form of interface to the operating system that also has some advantages. Unix is an operating system that was developed by programmers for programmers, and it provides a command-line interface that can be a versatile tool. Commands can easily be combined with one another to perform tasks that have not been built into any single piece of software.

To use the command line interface, open a terminal window by right-clicking on the background and selecting new terminal. You type commands in the terminal window, one per line. Type the return key at the end of the line to perform the command.

You can use some abbreviations. Type !! to repeat the previous command. Type !c to repeat the most recent command that begins with c.

A Unix command typically starts with the name of the command, then has options (normally starting with a dash) and then a list of file names. For example, command

  ls -l -a horse.cc
runs the ls command with options -l and -a, on file horse.cc. (The ls command will show the file name, with its attributes.)

In a Unix command, you can use * to stand for any file name or part of a file name. For example, to show the names of only files whose names begins with a q, you type

  ls q*


Redirecting the standard input and standard output

Commands typically read from the standard input and write to the standard output. Both are normally the terminal. You can redirect them, though. Use

  cmd <infile >outfile
to read standard input from infile and to write standard output to outfile. You can redirect just the standard input or just the standard output if desired.

Put & after a command to run it in background. This cause the terminal not to wait for the command to finish.


Search path

A command is really just the name of a file that contains the executable code for the command. For example, the ls command runs the file /bin/ls. The command interpreter searches for files to run by looking at a variable called PATH. It is a sequence of directories separated by colons. You can see the value of your PATH variable by typing command

  cmd <infile >outfile

Editing a program

Edit a program using either the system text editor or another text editor such as vi or emacs. Notes on using emacs are available below.

Be sure to save your program before you try to compile and run it. For a C++ program, give the program a name that ends on extension .cc or .cpp. For example, you might call your program prog1.cc.


Compiling and running a C++ program: basic method

To compile a C++ program, go to a terminal window. If the program is called prog1.cc, then type

   g++ -Wall prog1.cc
If there are errors, you will be notified. If not, then you will just see another prompt. No news is good news. The compiler creates a file called a.out. To run your program, just type command
  a.out
If your program has more than one file, you can just compile all of the files. For example,
   gcc -Wall prog1.cc helper1.cc
compiles a two-part program.


Stopping a rogue program

You can forcibly stop your program by typing ctl-C (control and c) in the terminal window where you started the program. Use this when your program is apparently in an infinite loop.

If you need to stop a process that is running in background, use command /usr/ucb/ps u to get the process number of the process, and kill -9 n to kill it, where n is the process number. Be sure that you are killing the right process.


Running the debugger

There is a debugger called gdb. To run the debugger for a C++ program, compile the program using option -g. That is, use command line

  g++ -Wall -g prog1.cc
Then use command
  gdb a.out
to run your program. At the prompt, type run to run your program. If you stop the program using ctl-C, you can do debugger command backtrace to get a trace of the run-time stack, most recently entered frame first. Use commands up and down to move up and down in the stack. (Unfortunately, up moves down and down moves up because the stack is shown upside-down.) You can print the value of a variable x using command print x.

Command break f causes the program to stop each time it starts function f. There are other commands as well. Type help to see them.

To exit the debugger, use command quit.


Killing print jobs

Please never print a binary file. It will consume a lot of paper. To kill a print job, use command lpq to show the print queue and lprm n to remove print job number n. You can only remove your own print jobs. To remove somebody else's, find a system administrator.

If necessary, you can stop the printer by pulling out the paper trays.


Putting programs in separate directories

It is a good idea to put each program in a separate directory (or folder). To create a directory called assn1, you can use the file manager (Applications->home folder), or you can just type command

  mkdir assn1
To make assn1 your current directory, use command
  cd assn1
You can find out your current directory using command
  pwd
or command dirs.


File names and paths

To refer to a file called foo in directory dir, write dir/foo. Notice that you use a forward slash (/), not a backslash (\). You can refer to your home directory as ~. For example, ~/prog1/prog1.cc is the file called prog1.cc in the prog1 directory in your home directory.

Each directory has two special directories in it called . (another name for the current directory) and .. (the directory above the current directory). So command cd .. moves up one directory, and cd ../.. moves up two directories.


Using makefiles to build programs automatically

If you have a multipart program, you can compile it by listing all of the parts on the g++ command line. Suppose that you have a program that is written in two modules, prog1.cc and utils.cc. Then you can compile it using command

  g++ -Wall prog1.cc utils.cc
or, if you are compiling all of the .cc files in the current directory,
  g++ -Wall *.cc
Do not list any .h files on the command line.

You will find it more convenient to use the make utility. Using a text editor, create a file called Makefile in the directory where your program is written. Here is a sample Makefile.

FLAGS = -c -g -Wall
go:
	g++ -o go prog1.o utils.o
prog1.o: prog1.cc utils.h
	g++ $(FLAGS) prog1.cc
utils.o: utils.cc
	g++ $(FLAGS) utils.cc
The lines of this file have the following meanings.
  1. The first line defines variable FLAGS. Wherever you see $(FLAGS) below, the definition of variable FLAGS is substituted. The flags are used for the g++ command, and are as follows.

    -c Do not link, just create a .o file. When file utils.cc is compiled with this option, the compile creates file utils.o that needs to be linked to other files before it can be run.
    -g Create debugging information so that you can run the debugger on this program.
    -Wall Give all common warnings.

  2. The lines

      go:
    	g++ -o go prog1.o utils.o
     
    tell how to build the target called go. This will build an executable program called go by linking together the compiled versions of prog1.cc and utils.cc. IMPORTANT NOTE: The line after go: begins with a tab. It MUST begin with a tab, not with a space.

  3. The lines

     prog1.o: prog1.cc utils.h
    	g++ $(FLAGS) prog1.cc
     
    tell how to build target prog1.o, the compiled version of prog1.cc. The first line says that prog1.o must be rebuilt whenever either prog1.cc or utils.h are changed. The second line, which must begin with at tab, is a command that will rebuild prog1.o.

To build your program, use command

  make
This build the first target (go) in file Makefile. To build target prog1.o instead, type
  make prog1.o

To run your program, just type

  go
since you said to put the executable in a file called go.


Basic Unix commands

Here are a few useful commands. Use the man command to find out more about them.

cancel

cancel n cancels print job n. Use command lpq to see what is in the print queue. The printer in room 320 is called peetie. The lprm command does the same job.

cd

cd dir makes dir be your current directory.

chmod

chmod u+r f gives you read permission to file f. You can use permissions r (read) w (write) and x (execute). You can also take away permission using chmod u-r. You can give or take away permission to/from yourself (u), or everybody else (o), or everybody (a).

cp

cp A B creates a copy of file A, and calls the copy B

dirs

Show the current working directory.

dos2unix

Unix uses a convention that a line is ended by a line feed character (Ascii 10). Windows/DOS uses a convention that a line is ended by a two character sequence, carriage-return line-feed (Ascii 13 then ascii 10). The dos2unix command converts for Windows/DOS format to Unix format. It writes the result to standard output. To write to a file, just redirect the standard output to the file. For example, use

  dos2unix myfile >mynewfile

du

Show disk usage. du -s -k will only show a summary of total usage in the current directory, in kilobytes.

ftp

Type ftp mach to open an ftp connection to computer mach. You can use commands similar to Unix commands to set the current directory (cd) on the remote machine and list (ls) the contents of the current directory. Use the put command to copy a file to the server and the get command to get a file from the server.

gcc

gcc is the C compiler. It is similar to the C++ compiler, g++.

g++

g++ is the C++ compiler. Use g++ options mprog.cc to compile program myprog.cc. Common options are as follows.

-c
Only create myprog.o, an object-language file that can be linked to other object-language files.
-g
Produce information for a debugger to use.
-o file
Put the compiled version into file file.
-O
Run the code optimizer. This both produces better quality machine code and gives some additional warnings that are only discovered during optimization. It can, however, make using the debugger more difficult.
-Wall
Give common warnings. Without this option, warnings are suppressed.
grep

grep s f searches file f for occurrences of string s

kill

kill -9 n will destroy process number n.

lpr

lpr f prints file f.

ls

Command ls shows the names of the file and directories in the current directory. Use ls -l to get a detailed listing, showing permissions and modification times. The ls command normally does not show files whose name begins with a dot. To see all files, use ls -a.

make

make runs the make utility to build a program.

man

man c shows a manual page for command or function c.

mkdir

mkdir A creates a new directory called A.

more

more f shows file f on the terminal.

mv

mv is used to rename or move a file. Use mv A B to move file A to file or directory B.

popd

popd removes the top directory from the directory stack. See pushd.

passwd

Change your password.

ps

List processes that are running. There are two common versions of this command, /bin/ps and /usr/ucb/ps. They work a little differently. Command /usr/ucb/ps u gives a fairly readable output.

pushd

The shell maintains a stack of directories. Command pushd dir pushes directory dir on the top of the stack. The top is always the current directory. Command pushd swaps the two topmost directories. See popd.

pwd

Show the current working directory (full path).

rm

rm f destroys file f.

rmdir

rmdir dir destroys directory dir. It can only be used to remove an empty directory.


Remote access

To access the Unix system over the internet, use ssh, with host login.cs.ecu.edu. You can get ssh for Windows from ftp://ftp.ssh.com/pub/ssh. Get SSHWinClient....exe, where the ... indicates the current version and build.

Ssh can also be used for file transfer. Just push the yellow folder symbol with blue dots in the tool bar. You can drag programs from the local machine to the remote machine and back.


Using the emacs text editor

Emacs is a versatile and extensible text editor. It is available under Unix or for personal computers running Microsoft Windows. See getting emacs for windows to get emacs for a windows machine.

Emacs offers some advantages for developing programs. One is that it will automatically indent programs. (Type a tab on a line to indent that line.) Another is that, when you type a right brace, emacs shows you the matching left brace, which helps find mismatched brace problems very quickly.

Emacs also allows you to have several files open at once, which is convenient for working on a program that consists of several files.

Start emacs by giving command

  emacs&
in a terminal window.

There is a one line minibuffer at the bottom of the screen. It is used to communicate with the user.

ctl-X means hold down the ctrl key while typing X. esc is the escape key. Emacs is case-sensitive, so s is not the same thing as S. For the ctl keys, just hold down ctl, not ctl and shift.

You can use menus at the top of the emacs buffer to open and save files. Some useful ones are File->open file (to open a file), File->save (to save your work), File->Postscript Print Buffer (to print your file) and Options->Mule->Set Font/Fontset (to change the font). You can get a C++ program to be shown with colors to indicate reserved words, types, etc. by selecting Options->Syntax Highlighting. Here are a few of the keyboard commands.

ctl-space

Put the mark at the current cursor location.

ctl-A

Go to the start of the line.

ctl-D

Delete the current character.

ctl-E

Go to the end of the line.

ctl-G

Abort the current command.

ctl-H c

Describe the command that is performed by the keystroke that you give after ctl-H c.

ctl-K

Delete the part of the line after the cursor. If the line has no nonblank characters, remove the line.

ctl-O

Open a new line.

ctl-R

Search the file backwards. Type in the word to search for. Use ctl-R ctl-R (typing it twice) to search again for the previous string. Each time you hit ctl-R, you will find the next occurrence of a match.

ctl-S

Like ctl-R, but seaches forward. Use ctl-S ctl-S to repeat the last search.

ctl-W

Delete all text between the cursor and the mark.

ctl-X b

Switch to another buffer

ctl-X ctl-C

Quit.

ctl-X ctl-F

Start editing a file.

ctl-X o

Switch to the other buffer that is being displayed.

ctl-X ctl-S

Save the current buffer.

ctl-X s

Save all modified buffers.

ctl-X 4 f

Open a file in another window.

ctl-Y

Paste back the result of the last kill (from the clipboard). (Emacs keeps a stack of clipboards. If you type esc y after ctl-Y, you get the next thing down on the stack. Keep typing esc y to see more on the stack.)

esc w

Copy the text between the cursor and the mark to the clipboard.

esc x

Give a long command. You type the command in the minibuffer.

esc %

Do a replacement. You will be asked at each occurrence whether to do the replacement. Type y to replace, n to skip, q to quit.


Getting emacs for windows

You can get emacs here. Select the link below the heading Obtaining GNU Emacs (a bit down the page), then select the Windows link at the bottom of the page. Web pages often change. If this does not work, go to www.gnu.org. There are many tools there. Under the Software heading, select Free Software Directory, then Text creation and manipulation, then Editors, then Emacs.

After you get emacs, you will need to run progman.exe to install a link for it.