±A few commands (such as diff, gcc, awk) are doccumented using info.
®info is GNU-specific
±--help , eg: gcc --help
±Most commands have HTML references on the WWW.
±QNX 632.chm
which
±Displays a path name of a command.
±Searches a path environmental variable for the command and displays the absolute path.
±To find which tcsh and bash are actually in use, type: % which tcsh % which bash
±% man which for more details
whereis
±Display all locations of a command (or some other binary, man page, or a source file).
±Searchers all directories to find commands that match whereis’ argument
±% whereis tcsh
chsh
±Change Login Shell
±Login shell is the shell that interprets commands after you logged in by default.
±You can change it with chsh (provided that your system admin allowed you to do so).
±To list all possible shells, depending on implementation: % chsh -l % cat /etc/shells
±% chsh with no arguments will prompt you for the shell.
passwd
±Change your login password.
±A very good idea after you got a new one.
±It’s usually a paranoid program asking your password to have at least 6 chars in the password, at least two alphabetical and one numerical characters. Some other restrictions (e.g. dictionary words or previous password similarity) may apply.
±Depending on a privilege, one can change user’s and group passwords as well as real name, login shell, etc.
±% man passwd
date
±Guess what :-)
±Displays dates in various formats
±% date
±% date -u
®in GMT
±% man date
cal
±Calendar
®for month
®entire year
±Years range: 1 - 9999
±No year 0
±Calendar was corrected in 1752 - removed 11 days
clear
±Clears the screen
±There’s an alias for it: Ctrl+L
±Example sequence:
®% cal
®% clear
®% cal
®Ctrl+L
sleep
±“Sleeping” is doing nothing for some time.
±Usually used for delays in shell scripts.
±% sleep 22 seconds pause
Command Grouping
±Semicolon: “;”
±Often grouping acts as if it were a single command, so an output of different commands can be redirected to a file:
±% (date; cal; date) > out.txt
alias
±Defined a new name for a command
±% alias
®with no arguments lists currently active aliases
±% alias newcommand oldcommand
®defines a newcommand
±% alias cl cal 2003
±% cl
unalias
±Removes alias
±Requires an argument.
±% unalias cl
history
±Display a history of recently used commands
±% history
®all commands in the history
±% history 10
®last 10
±% history -r 10
®reverse order
±% !!
®repeat last command
shutdown
±Causes system to shutdown or reboot cleanly.
±May require superuser privileges
±% shutdown -h now- stop
±% shutdown -r now- reboot
ls
±List directory contents
±Has whole bunch of options, see man ls for details.
±% ls
®all files except those starting with a “.”
±% ls -a
®all
±% ls -A
®all without “.” and “..”
cat
±Display and concatenate files.
±% cat
®Will read from STDIN and print to STDOT every line you enter.
±% cat file1 [file2] ...
®Will concatenate all files in one and print them to STDOUT
±% cat > filename
®Will take whatever you type from STDIN and will put it into the file filename
±To exit cat or cat > filename type Ctrl+D to indicate EOF (End of File).
more / less
±Pagers to display contents of large files page by page or scroll line by line up and down.
±Have a lot of viewing options and search capability.
±Interactive. To exit: ‘q’
less
±less ("less is more") a bit more smart than the more command
±to display contents of a file:
®% less filename
±To display line numbers:
®% less -N filename
±To display a prompt:
®% less -P"Press 'q' to quit" filename
±Combine the two:
®% less -NP"Blah-blah-blah" filename
±For more information:
®% man less
touch
±By touching a file you either create it if it did not exists (with 0 length).
±Or you update it’s last modification and access times.
±There are options to override the default behavior.
±% touch file
±% man touch
cp
±Copies files / directories.
±% cp [options] <source> <destination>
±% cp file1 file2
±% cp file1 [file2] … /directory
±Useful option: -i to prevent overwriting existing files and prompt the user to confirm.
mv
±Moves or renames files/directories.
±% mv <source> <destination>
®The <source> gets removed
±% mv file1 dir/
±% mv file1 file2
®rename
±% mv file1 file2 dir/
±% mv dir1 dir2
rm
±Removes file(s) and/or directories.
±% rm file1 [file2] ...
±% rm -r dir1 [dir2] ...
±% rm -r file1 dir1 dir2 file4 ...
script
±Writes a log (a typescript) of whatever happened in the terminal to a file.
±% script [file]
±% script
®all log is saved into a file named typescript
±% script file
®all log is saved into a file named file
±To exit logging, type:
®% exit
find
±Looks up a file in a directory tree.
±% find . -name name
±% find . /(-name ‘w*’ -or -name ‘W*’ /)
mkdir
±Creates a directory.
±% mkdir newdir
±Often people make an alias of md for it.
cd
±Changes your current directory to a new one.
±% cd /some/other/dir
®Absolute path
±% cd subdir
®Assuming subdir is in the current directory.
±% cd
®Returns you to your home directory.
pwd
±Displays personal working directory, i.e. your current directory.
±% pwd
rmdir
±Removes a directory.
±% rmdir dirname
±Equivalent:
®% rm -r dirname
ln
±Symbolic link or a “shortcut” in M$ terminology.
±% ln –s <real-name> <fake-name>
chmod
±Changes file permissions
±Possible invocations
®% chmod 600 filename
®-rw------- 1 user group 2785 Feb 8 14:18 filename (a bit not intuitive where 600 comes from)
®% chmod u+rw filename (the same thing, more readable)
®For the assignment:
®% chmod u+x myshellscript (mysshellscript is now executable)
®-rwx------ 1 user group 2785 Feb 8 14:18 myshellscript
Pipes
±What's a pipe?
®is a method of interprocess communication (IPC)
®in shells a '|' symbol used
®it means that the output of one program (on one side of a pipe) serves as an input for the program on another end.
®a set of "piped" commands is often called a pipeline
±Why it's useful?
®Because by combining simple OS utilities one can easily solve more complex tasks
Pipes and Redirections
±|
±>
±<
±>>
±<<
Commands
±chmod
±pwd
±ls
ls file; ls directory ;; ls –a ; ls –l ; ls -R
±cd
±mkdir
±rmdir
±mv
mv oldfilename newfilename
mv file1 file2 file3 newtargetdirectory
±cp-- syntax like mv
±rm
±find
±echo
±cat
±Whereis
±printenv,env,set,export
±grep
Linux commands for programmers
®man –kSearch man pages by topic
®timeHow long your program took to run
®dateprint out current date/time
®testCompare values, existence of files, etc
®teeReplicate output to one or more files
®diffReport differences between two files
®sdiffReport differences side-by-side
®wcShow number of lines, words in a file
®sortSort a file line by line
®gzipCompress a file
®gunzipUncompress it
®stringsPrint out ASCII strings from a (binary)
®lddShow DLLs/SOs program is linked to
®nmShow detailed info about a binary obj
Linux Shell Scripting: Conditional Execution
±program1 && program2
®Program 2 will execute if and only if program1 exited with a 0 status
®Example:
®project1 && echo “Project1 Finished correctly!”
±program1 || program2
®Program 2 will execute if and only if program1 exited with a non-0 status
®Example:
®project1 || echo “Project1 FAILED to complete!”
±Exit a script with an error:
®exit 1
Linux job control
±Start a background process:
®program1 &
®program1
Hit CTRL-Z
bg
±Where did it go?
®jobs
®ps
±Terminate the job: kill it
®kill %jobid
®kill pid
±Bring it back into the foreground
®fg %1
±Start a job in the future
®at
Regular Expressions
±Powerful language for specifying strings of text to be searched and/or manipulated.
±Used by
®grep“Get Regular Expression and Print” – search files line by line
®sedSimple Editing tool, right from the command line
®awkScripting language, executes “program” on matching lines
®perlPathological Rubbish Lister. Powerful programming language
±Note: These are not “file-globs”. The syntax is similar, but the semantics are slightly different!
±Cannot be used to match nested structures
Regular Expressions: Summary
±Fundamentals:
Match the specified character unless it is a ...
.Match any character (except EOL)
[character class]Match the characters in character class.
[start-end]start to end
[^character class]Match anything except the character class.
$Match the end of the line
^Match the beginning of the line
*Match the preceeding expression zero or
more times
?Match the preceeding zero or one time
|Match the lef hand side OR the right side
(regexp)Group the regular expression
/Treat next character literally (not specially)
±Examples:
Match a line beginning with a space-padded line number and colon.
±The ":set all" command gives all the possible options.
±:set tabstop=8
±:set number
±:set nonumber
Linux Programming gcc
Contents
±Intro
±Options
±Examples
What is gcc?
±gcc
®stands for GNU C/C++ Compiler
®a popular console-based compiler for *NIX platforms and others; can cross-compile code for various architectures
®gcc to compile C programs; g++ for C++
®can actually work with also ADA, Java, and a couple other languages
®gcc performs all of these:
®preprocessing,
®compilation,
®assembly, and
®linking
±man gcc
±QNX632.chm
Options
±To compile: -c
±Specify output filename: -o <filename>
±Include debugging symbols: -g
±GDB friendly output: -ggdb
±Show all (most) warnings: -Wall
±Be stubborn about standards: -ansi and -pedantic
±Optimizations: -O, -O*
Options: -c
±gcc performs compilation and assembly of the source file without linking.
±The output are usually object code files, .o; they can later be linked and form the desired executables.
±Generates one object file per source file keeping the same prefix (before .) of the filename.
Options: -o <filename>
±Places resulting file into the filename specified instead of the default one.
±Can be used with any generated files (object, executables, assembly, etc.)
±If you have the file called source.c; the defaults are:
®source.o if -c was specified
®a.out if executable
±These can be overridden with the -o option.
Options: -g
±Includes debugging info in the generated object code. This info can later be used in gdb.
±gcc allows to use -g with the optimization turned on (-O) in case there is a need to debug or trace the optimized code.
Options: -ggdb
±In addition to -g produces the most GDB-friendly output if enabled.
Options: -Wall
±Shows most of the warnings related to possibly incorrect code.
±-Wall is a combination of a large common set of the -W options together. These typically include:
®unused variables
®possibly uninitialized variables when in use for the first time
®defaulting return types
®missing braces and parentheses in certain context that make it ambiguous
®etc.
±Always a recommended option to save your bacon from some “hidden” bugs.
±Try always using it and avoid having those warnings.
Options: -ansi and -pedantic
±For those who are picky about standard compliance.
±-ansi ensures the code compiled complies with the ANSI C standard; -pedantic makes it even more strict.
±These options can be quite annoying for those who don’t know C well since gcc will refuse to compile unkosher C code, which otherwise it has no problems with.
Options: -O, -O1, -O2, -O3, -O0, -Os
±Various levels of optimization of the code
±-O1 to -O3 are various degrees of optimization targeted for speed
±If -O is added, then the code size is considered
±-O0 means “no optimization”
±-Os targets generated code size (forces not to use optimizations resulting in bigger code).
Options: -I
±Tells gcc where to look for include files (.h).
±Can be any number of these.
±Usually needed when including headers from various-depth directories in non-standard places without necessity specifying these directories with the .c files themselves, e.g.: #include “myheader.h” vs. #include “../foo/bar/myheader.h”
For Your Assignments
±For your assignments, I’d strongly suggest to always include -Wall and -g.
±Optionally, you can try to use -ansi and –pedantic, which is a bonus thing towards your grade.
±Do not use any optimization options.
±You won’t need probably the rest as well.
Example
±For example, if you have the following source files in some project of yours:
®ccountln.h
®ccountln.c
®fileops.h
®fileops.c
®process.h
®process.c
®parser.h
®parser.c
±You could compile every C file and then link the objet files generated, or use a single command for the entire thing.
®This becomes unfriendly when the number of files increases; hence, use Makefiles!
±NOTE: you don’t NEED to compile .h files explicitly.
Example (2)
±One by one:
®gcc -g -Wall -ansi -pedantic -c ccountln.c
®gcc -g -Wall -ansi -pedantic -c parser.c
®gcc -g -Wall -ansi -pedantic -c fileops.c
®gcc -g -Wall -ansi -pedantic -c process.c
±This will give you four object files that you need to link and produce an executable:
±Instead of typing this all on a command line, again: use a Makefile.
Example (4)
Linux Programming makefile
Intro
±Makefiles in conjunction with the make utility (man make) provide a very convenient facility to build, install, and uninstall large (and small too) projects in the *nix systems.
±Makefiles are text files and similar in a way to the shell scripts, which are interpreted by the make utility.
Intro (2)
±A makefile may have some variables declared for convenience then followed by rules on how to build a given target program.
±The variable part usually declares variables like which compiler to use across all the targets:
®which compiler options to use,
®where to look for libraries and include files, etc.
±The rules specify what's needed to build a specific part and how to do it, using shell commands.
Intro (3)
±The make utility avoids re-bulding targets which are up-to-date, thus, saving typing and compiling time a lot.
±Makefiles largely similar to the Project and Workspace files you might be used to from Visual C++, JBuilder, Eclipse, etc.
Make command line (simplified)
make [options] [targets]
±options include
®“-f filename”use filename as the Makefile
®“-n”don’t do anything – just print what needs to be done.
±targets are what should be built (if needed).
Specifying Targets
±When making all the stuff in your program typing make is enough.
±In certain cases, the make utility assumes target “all”, or most often the first target in the list.
±If you want to build a specific target just supply it as a parameter to make: make mytarget
Filenames
±When you key in make, the make looks for the default filenames in the current directory. For GNU make these are:
®GNUMakefile
®makefile
®Makefile
±If there more than one of the above in the current directory, the first one according to the above chosen.
±It is possible to name the makefile anyway you want, then for make to interpret it: make -f <your-filename>
Basic Makefile Format
±Basic Format Summary:
Basic Makefile Format (2)
±Comments
®Just like for most shell scripts, they start with ‘#’
±Variables (make calls them macros)
®Are assigned the same way as in bash: var=value
®Used as: $(var)
®Variables can be scalar as well as lists (‘arrays’)
Rules
±Rule or several of then are needed to direct compilation and building, installation, and clean up actions that depend on certain prerequisites.
±Basic components of a Rule are
®Ultimate target of a rule, the goal
®List of dependencies needed to be satisfied before this rule can be executed, the prerequisites
®List of actions, or command, that are executed once all prerequisites satisfied to arrive to the target goal.
Rules
target : prerequisites
command1
command2
…
target depends on the files listed after the colon.
commands are unix commands that build a new target file.
Simple Example
±This rule would tell make that the file linecount depends on the file foo.c.
±To build linecount the command “wc –l foo.c > linecount” should be run.
linecount : foo
wc –l foo.c > linecount
Simple Makefile for building a program.
subs.o: subs.c
gcc –c subs.c
main.o: main.c
gcc –c main.c
main: main.o subs.o
gcc –o main main.o subs.o
Macros
±You can create macros (fancy variables)
OBJS = subs.o main.o
main: $(OBJS)
gcc –o main $(OBJS)
Fancy Stuff
±Macro substitution – evaluates to the value of a macro after some substitutions:
SOURCE = main.c subs.c
OBJS = ${SOURCE:.c=.o}
now OBJS is main.o subs.o
Suffix Rules
±General rules for building files that end with some suffix from files with the same name but a different suffix.
±For example, how to build a .o file from a .c file.
±Special predefined macros help with suffix rules:
$< means current prereq.
$@ is the current target.
Example Suffix Rule
.c.o:
gcc –c $<
This rule tells Make how to create a .o file any time is has a .c file (and needs the .o file to build a target).
A Complete Example
.c.o:
gcc –c $<
SOURCE = main.c subs.c
OBJS = ${SOURCE:.c=.o}
main: $(OBJS)
gcc –o main $(OBJS)
Targets
±Target name can be almost anything:
®just a name
®a filename
®a variable
±There can be several targets on the same line if they depend on the same things.
±A target is followed by
®a colon “:”
®and then by a list of dependencies, separated by spaced
±The default target make is looking for is either all or first one in the file.
±Another common target is clean
®Developers supply it to clean up their source tree from temporary files, object modules, etc.
®Typical invocation is: make clean
±You are required to supply another target, called run, so that it makes sure your application is compiled and run just by typing: make run.
Dependencies
±The list of dependencies can be:
®Filenames
®Other target names
®Variables
±Separated by a space.
±May be empty; means “build always”.
±Before the target is built:
®it’s checked whether it is up-to-date (in case of files) by comparing time stamp of the target of each dependency; if the target file does not exist, it’s automatically considered “old”.
®If there are dependencies that are “newer” then the target, then the target is rebuild; else untouched.
®If you want to “renew” the target without actually editing the dependencies, “touch” them with the touch command.
®If the dependency is a name of another rule, make descends recursively (may be in parallel) to that rule.
Actions
±A list of actions represents the needed operations to be carried out to arrive to the rule’s target.
®May be empty.
±Every action in a rule is usually a typical shell command you would normally type to do the same thing.
±Every command MUST be preceded with a tab!
®This is how make identifies actions as opposed to variable assignments and targets. Do not indent actions with spaces!
Line-Oriented
±Makefile is a line-oriented file.
±That means by the end of line a new line starts, it is either a new action or target or whatever.
±If your list of dependencies or a command are too long and you would like for them to span across several lines for clarity and convenience, you may do so, but you will have to escape the end of line by “/” at the end of each such a line.
±Make sure not to use tabs for such lines.
More Advanced Constructs
±List/array variables/macros can be assignment via the “:=” operator.
±It is possible to use control-flow constructs within the actions part of a rule, such as for, if-then-else, test, etc. The syntax is that of bash.
±Implicit targets for files of specific extensions.
±“Macros” and “directives” for conditional variable definitions as well as to include other makefiles.
±Others...
±Not part of this “study” yet...
Examples
sum: main.o sum.o
gcc –o sum main.o sum.o
main.o: main.c sum.h
gcc –c main.c
sum.o: sum.c sum.h
gcc –c sum.c
Rule syntax
main.o: main.c sum.h
gcc –c main.c
tab
dependencyaction
Equivalent makefiles
±.o depends (by default) on corresponding .c file. Therefore, equivalent makefile is:
sum: main.o sum.o
gcc –o sum main.o sum.o
main.o: sum.h
gcc –c main.c
sum.o: sum.h
gcc –c sum.c
Equivalent makefiles - continued
±We can compress identical dependencies and use built-in macros to get another (shorter) equivalent makefile :
sum: main.o sum.o
gcc –o $@ main.o sum.o
main.o sum.o: sum.h
gcc –c $*.c
Another makefile example
# Makefile to compare sorting routines
BASE = /home/blufox/base
CC=gcc
CFLAGS=-O –Wall
EFILE=$(BASE)/bin/compare_sorts
INCLS=-I$(LOC)/include
LIBS=$(LOC)/lib/g_lib.a /
$(LOC)/lib/h_lib.a
LOC=/usr/local
OBJS = main.oanother_qsort.ochk_order.o /
compare.oquicksort.o
$(EFILE): $(OBJS)
@echo “linking …”
@$(CC) $(CFLAGS) –o $@ $(OBJS) $(LIBS)
$(OBJS): compare_sorts.h
$(CC) $(CFLAGS) $(INCLS) –c $*.c
# Clean intermediate files
clean:
rm *~ $(OBJS)
Example - continued
±We can define multiple targets in a makefile
±Target clean – has an empty set of dependencies. Used to clean intermediate files.
±make
®Will create the compare_sorts executable
±make clean
®Will remove intermediate files
Passing parameters to makefile
±We can pass parameters to a makefile by specifying them along with their values in the command line.
±For example:
make PAR1=1 PAR2=soft1
will call the makefile with 2 parameters: PAR1 is assigned the value “1” and PAR2 is assigned the value “soft1”. The same names should be used within the makefile to access these variables (using the usual “$(VAR_NAME)” syntax)
Passing parameters - continued
±Note that assigning a value to a variable within the makefile overrides any value passed from the command line.
±For example:
command line : make PAR=1
in the makefile:
PAR = 2
±PAR value within the makefile will be 2, overriding the value sent from the command line
Conditional statements
±Simple conditional statements can be included in a makefile.
±Usual syntax is:
ifeq (value1, value2)
body of if
else
body of else
endif
Conditional statements - example
sum: main.o sum.o
gcc –o sum main.o sum.o
main.o: main.c sum.h
gcc –c main.c
#deciding which file to compile to create sum.o
ifeq ($(USE_SUM), 1)
sum.o: sum1.c sum.h
gcc –c sum1.c –o $@
else
sum.o: sum2.c sum.h
gcc –c sum2.c –o $@
endif
Linux Programming libgen makefile
R_IL1_0.1/libgen/build/makefile
LIBGEN_DIRS = ${VOBTAG}/libgen/lib/build /
${VOBTAG}/libgen/src/dmxmsg/core-mod
${VOBTAG}/libgen/src/dmxmsg/octeon-mod /
${VOBTAG}/libgen/tst/ste
all:
for d in $(LIBGEN_DIRS); do cd $$d && make;done
touch build.tag
clean:
for d in $(LIBGEN_DIRS); do cd $$d && make clean;done
±Use GDB when your program doesn’t behave the way you expected - catching the bugs
±Use GDB when your make files stops on specified condition
±When you want to examine your program logic to see howit works
Invoking
±Preparing An Executable For Debugging
$ gcc -o filename filename.c-g {1,2,3}
or
$gcc -ggdb -o filename filename.c
±Invoking gdb
$ gdb filename
±Run program to be debugged
(gdb) r[un]
±Note: Pressing enter with no command executes the previous command
Run
Step
Next
Continue
Finish
Break
Break
Conditional Breakpoints
±break [position] if expression
(gdb) s
27 for ( i = 1; i <= limit ; ++i )
(gdb) break 28 if i == limit - 1
Breakpoint 1 at 0x4010e7: file gdb_test.c, line 28.
±Once bp reached, expression is evaluated and program stops if true
Watch
Watchpoints
±watch expression
®The debugger stops the program when the value of expression changes.
±rwatch expression
®The debugger stops the program whenever the program reads the value of any object involved in the evaluation of expression.
±awatch expression
®The debugger stops the program whenever the program reads or modifies the value of any object involved in the evaluation of expression.
Info
Clear
Delete
Breakpoints
Deleting, Disabling, and Ignoring BP
±delete [ bp_number | range]
±d [ bp_number | range]
®Deletes the specified breakpoint or range of breakpoints. A delete command with no argument deletes all the breakpoints that have been defined. GDB prompts you for confirmation before carrying out such a sweeping command:
®(gdb) d Delete all breakpoints? (y or n)
±disable [ bp_number | range]
®Temporarily deactivates a breakpoint or a range of breakpoints. If you don't specify any argument, this command affects all breakpoints. It is often more practical to disable breakpoints temporarily than to delete them. GDB retains the information about the positions and conditions of disabled breakpoints so that you can easily reactivate them.
±enable [ bp_number | range]
®Restores disabled breakpoints. If you don't specify any argument, this command affects all disabled breakpoints.
±ignore bp_number iterations
®Instructs GDB to pass over a breakpoint without stopping a certain number of times. The ignore command takes two arguments: the number of a breakpoint, and the number of times you want it to be passed over.
Print
Display
List
Displaying Source Code
±list filename: line_number
®Displays source code centered around the line with the specified line number.
±list line_number
®If you do not specify a source filename, then the lines displayed are those in the current source file.
±list from,[ to]
®Displays the specified range of the source code. The from and to arguments can be either line numbers or function names. If you do not specify a to argument, list displays the default number of lines beginning at from.
±list function_name
®Displays source code centered around the line in which the specified function begins.
±list, l
®The list command with no arguments displays more lines of source code following those presented by the last list command. If another command executed since the last list command also displayed a line of source code, then the new list command displays lines centered around the line displayed by that command.
Backtrace
Analyzing the Stack
±bt
®Shows the call trace
±info frame
®Displays information about the current stack frame, including its return address and saved register values.
±info locals
®Lists the local variables of the function corresponding to the stack frame, with their current values.
±info args
®List the argument values of the corresponding function call.
±info breakpoints or watchpoints
®List all breakpoints or watchpoints
Displaying Data
±p [/format] [expression]
±Output Formats
®d: Decimal notation. This is the default format for integer expressions.
®u: Decimal notation. The value is interpreted as an unsigned integer type.
®x: Hexadecimal notation.
®o: Octal notation.
®t: Binary notation. Do not confuse this with the x command's option b for "byte," described in the next subsection.
®c: Character, displayed together with the character code in decimal notation.