William Barrett, San Jose State Computer Engineering Dept.
UnixTips.htm vs. 4
login: your assigned login name, e.g. cmpe152t17
passwd: your assigned password, e.g. 86#amed*
man command ;
find command, C function or system file
man –s 2 command ;
find command, etc. in section 2
o
section 1 – Unix shell commands; user commands
o
section 2 – Unix shell commands; system commands
o
section 3 – C library functions
o
section 4 – File formats
o
section 5 – Standards, environments & macros
o
etc. –
special Sun/Solaris manuals
o
This depends on the operating system and window
configuration.
o
The default installation under Linux is usually with no
command window appearing along the bottom row.
You should then open the system menus, find the command window icon, and
send it to the bottom row.
o
Find a configuration that pleases you. It should stick to your account and come up
each time you log in.
o
You might also want to identify an editor that you like
and get its icon along the bottom window.
However, these can also be called as a command through the command
window.
Most of what follows assumes that you will be working through a command window rather than the desktop window.
Modern Linux installations provide windows interfaces to most of these functions, which you can discover for yourself.
|
create a new directory |
mkdir directory-name |
|
go to some other directory |
cd directory-name |
|
go to home directory $HOME |
cd |
|
go to home directory (~ is shorthand for $HOME) |
cd ~ |
|
removing a directory (directory must be empty) |
rmdir directory-name |
|
removing a directory or file |
rm file-name rm –r directory-name |
|
removing all files in the current directory |
rm * |
|
removing all directory trees in the current directory |
rm –rf * |
|
what’s in directory dir? |
ls directory-name |
|
what’s in the current directory? |
ls |
|
show permissions, owners, mod times & file name |
ls –l |
|
change a file name, or move it somewhere else; also works with directories |
mv oldname newname |
|
copy a file |
cp file filecopy |
|
copy a batch of files to some directory |
cp files directory |
· The vi editor is provided in essentially the same way on all Unix platforms.
· You can install a shareware editor, vim, on Windows through the internet.
· Though the command structure seems ugly, it is a surprisingly powerful editor.
· We like it because you will be editing in the same directory that you are in, reducing the confusion with other files scattered around among different directories.
· We also like it because all operations are done through the keyboard. Mouse support, while appearing to be friendly, in fact means moving your hand back and forth between it and the keyboard, and that costs you in performance.
· vi behaves exactly the same way through a secure shell network connection.
· The vi editor uses the typewriter keys on the keyboard and the arrow keys ONLY
o NOT the mouse
o NOT the F1, F2, ... control keys
o NOT the Insert/Delete/Home/End/Pageup/PageDown keypad
o The numeric keypad is OK if in NUMLOCK mode
· There are NO scroll bars, but you can use the mouse to increase the window size
· open an existing file like this:
vi filename
· vi is always in one of two modes –
o control mode, in which keys cause some operation, or
o insertion mode, in which keys insert text
· switch to control mode with ESC – the escape key
· switch to insertion mode with either of a, i, o, p keys
Operation |
Keys – in control
mode |
|
move the cursor |
arrow keys h move left j move down k move right l move up ctl-f move one screen down ctl-b move one screen up $ last position of current line 0 first position of current line |
|
insertion |
a append after cursor i insert ahead of cursor o open a line below cursor O open a line above
cursor |
|
text changes |
dd delete the current line 15dd delete this and 14 more lines x delete one character 15x delete this and 14 more characters yy copy this line into buffer 15yy copy this and 14 more lines to buffer p insert buffer lines after cursor u undo last change |
|
show line numbers |
:set numbers show numbers :set nonumbers do not show numbers |
|
show matching (..), {..}, [..] |
position the cursor on one of these, then type "%" in control mode |
|
block operations (type CR after
each one) |
:5,50d delete lines 5 through 50 :5,50y copy lines 5..50 to buffer :5,50w file write
lines 5..50 to file :1,$w file write
all lines to file :r file insert file after cursor |
|
searching (CR needed for all
but n) |
/string search
forward for string ?string search
backward for string n repeat previous search (no CR) / repeat previous search forward ? repeat
previous search backward |
|
editing a new file |
:e filename |
|
writing file |
:w overwrites current file :w filename write
to filename :w! filename if filename exists |
|
reading file |
:r filename |
|
exit from the editor |
:q |
o The emacs editor has only one mode – text insertion.
o All commands are with some combination of the control key or ALT key.
o The console version uses the mouse, has mouse-active menu, and scroll bars.
o
Choose the tutorial to get started:
control-h t
o emacs can be used through a network secure shell, but you need to use the key commands, not the mouse and menu, etc.
o Some versions provide a menu and mouse support, with scroll bars, etc. similar to the Windows environment.
The Gnu tools are used in the Unix lab. These are also compatible with other Unix versions, including Linux.
· Start in a console window. Don’t try to run these tools from the interactive menus.
· Use vi or emacs to prepare your text files, saving it with the appropriate suffix, i.e.
file.c
file.cpp
file.h
· Compile like this:
g++ -g –c file1.c
file2.cpp etc.
o You can list as many files as you like
o DO NOT compile .h files (they are picked up from your #include directives)
o The file type is inferred from the SUFFIX -- .c for an ANSI C file, .cpp for a C++ file.
o These options prepare for debugging (-g) and generate object files (-c)
o You can also use gcc for ANSI C files (but not C++ files)
· Link like this:
g++ file1.o file2.o file3.o –o exefile
o These are object files (the .o suffix)
o You can also list some source files, but remember to include –g –c
o The executable file exefile has no suffix, and follows the option –o
o Sometimes you need a library designator, for example, this for the “math” library. Read the man pages to decide what additional libraries you will need to support special functions.
g++ file1.o file2.o file3.o –o exefile –lm
· If you need to link to a library of your own, you need to designate your library like this:
libxxx.a
where xxx is your library name. Then use
-lxxx
in the g++ line to access your library.
· If your library is in some other directory, use -L to designate that directory, like this:
-L /mypath/mylibdir -lxxx
The Gnu debugger gdb is command-driven. Don’t expect menus to pop up.
o You’ve prepared for gdb by using the –g option in g++ and gcc.
o gdb needs an executable file.
o The –g option in g++/gcc has asked the compiler to install symbol table and line number information
o gdb works from line numbers in your C and C++ source files.
o You should open a separate editor window that shows the line numbers of the source file. See “:set numbers” in the vi editor table above, for example.
o gdb cannot figure out what your programming problems are. It can only help you trace the program flow and view your variables. You have to decide whether they make sense.
o You may step into a system function and get lost (no source information) – use finish to get out and continue.
o Compilation is fast. Don’t hesitate to drop in some print statements to help you figure out what your program is doing.
Here’s how to get started with a debugging session. The Italic lines are what you type
gdb exefile
...several lines of stuff
(gdb) b main set
a breakpoint on your ‘main’ function
(gdb) r run
program with no parameters
(gdb) r parameters
run program with parameters;
can be file inputs, options, etc.
“r”
also restarts with given parameters
(gdb) c continue
to the next breakpoint or program end
(gdb) n step
once, through function calls
(gdb) s step
once, into function calls
(gdb) finish finish
the current function, return, then break
(gdb) l list
some source code (see where you are)
(gdb) b 22 set
a breakpoint at line 22
(gdb) b filename.cpp:45 set
a breakpoint at line 45 in file ‘filename.cpp’
(gdb) p variable view contents of a simple variable
(gdb) p ary view pointer value (ary is a
pointer or array)
(gdb) p ary[3] view
array element at index 3
(gdb) p *ary@50 view contents of an array; show
50 elements
(gdb) p ary[5]@20 view
array contents starting at ary[5], 20 elements
(gdb) where show stack of function calls
(gdb) h get
help
(gdb) h breakpoints get
help on “breakpoints”
(gdb) q quit gdb
Did your program crash, with a message about generating a core file? Try this:
gdb exefile core
You will be shown the line number on which the accident occurred.
o Use where to view the sequence of function calls leading up to the crash
o You can examine variables
o A common crash problem is an uninitialized pointer dereference. Use p to see if any pointers have value 0 or some weird value.
o Another common crash problem is an unterminated for or while loop or unterminated recursive function call loop.
o Sometimes gdb can’t help you. It depends on function call information in the runtime stack, which can get corrupted by a bad program.
o You will just have to write in enough printf or cout statements to try to figure out what happened.
o Start it with gdb as explained above.
o Note that you need to “run” it with r
o Hit control-C to interrupt it.
o gdb should show you where you interrupted it.
o You can continue with the stepping and finish commands; look for a loop that isn’t terminated.
Make files are a very convenient way to organize a project consisting of many C, C++, header files, and other materials.
The Unix utility make looks for a file named makefile or Makefile in your current directory.
o YOU write the makefile as a text file. Follow these guidelines:
Pattern |
Significance |
|
# something or other |
This is a COMMENT. It extends to the end of the current line. |
|
$(CFLAGS) |
This is a MACRO EXPANSION. · The name CFLAGS may be declared as a macro (see next box), or · The name CFLAGS may be an environment variable, or · The name CFLAGS may be passed to the makefile like this: make CFLAGS="-O2" |
|
CFLAGS=-g |
This is a MACRO DEFINITION. The macro name CFLAGS will be expanded into the string "-g" later in the makefile, wherever $(CFLAGS) appears. A definition needs to appear before any expansion call. |
|
myfile.o: myfile.cpp tab g++ $(CFLAGS) -c myfile.cpp |
This is a CONCRETE RULE, consisting of a PATTERN (first line) and COMMANDS (second and subsequent lines) · if myfile.o is absent, or is older than myfile.cpp, then the RULE is triggered. · When a RULE is triggered, the COMMANDS are executed · Each COMMAND line must have a TAB character in the leftmost position. In this case, if myfile.o is absent, the compiler g++
is invoked to compile myfile.cpp to produce the object file mfile.o |
|
%.o: %.cpp tab g++ $(CFLAGS) -c %<.cpp |
This is an ABSTRACT RULE. It's similar to the concrete rule given above, except that it can fit any number of C++: object file pairs, as suggested by the % forms. If no concrete rule exists for some operation, make will see if an abstract rule will match the operation. Then % is replaced by some specific file name. (This form is ONLY available through the Gnu make utility). |
· In your Unix account, look for the makefile in the examples directory. It has plenty of comments and serves to build the example hello.cpp program.
· When you are ready to rebuild something after editing, just type make into your command window and watch the action flow automatically! But also watch for make errors, which will be reported, and check that everything that should be rebuilt in fact is rebuilt.
See the web page http://www.engr.sjsu.edu/wbarrett/UnixLabs0.htm.