<<

Tutorials Network Design/Computer vision by Helal Saghir The Prompt

• Commands are the way to “do things” in • A command consists of a command name and options called “flags” • Commands are typed the command prompt • In Unix, everything (including commands) is case- sensitive [prompt]$ helal:~$ –l -a unix-tutorial

Command (Optional) arguments Command Prompt (Optional) flags

Note: Many Unix commands will print a message only if something went wrong. Be careful with and . Getting help with man • man (short for “manual”) documents commands – man retrieves detailed information about – man –k searches the summaries (faster, and will probably give better results) – man –K searches the full text of the man pages helal:~$ man –k password passwd (5) - password xlock (1) - Locks the local X display until a password is entered helal:~$ passwd ssh (secure shell)

• Open secure shell from windows. • Enter server name , username and password . You can login remotely. • In linux use this command • ssh [email protected] • Will ask your password and let you enter. yppasswd

• To change the password on linux use • passwd • In lab you have to use yppasswd as we are running NIS. Quota

• Quota limit is 100 MB. • Check quota by using • du – disk usage. • du –cms • -c : total • -m: megabytes • -s: summary • There is quota command also but it won’t work because of NIS running on clients. It should work on your home computer, if you have Linux installed on it and quota rpm on. Linux Commands

– change directory • cd .. – changes to one up-level • cd / - goes to the root • cd $HOME – takes to you home directory • cd $home – does the same thing • Handy directories to know ~ Your home directory ls command

• ls – shows the list of files/directory • ls –al – shows all files including hidden with detail. • ls –l - shows all files not including hidden. • ls - shows the list in that directory

• cp – copy files • cp [source_file1] [des_file1] • -p : preserve=mode,ownership,timestamp • -R –r : copy directories recursively Move /

• mv : move files /directory • mv [source] [destination]

• mkdir : directory – Syntax: mkdir : remove directory which must be empty. – Syntax: rmdir rm

• Rm –remove non-empty directory or file • -r: recursive • -f : force • -v: verbose Shell Shortcuts • Tab completion – part of a file/directory name, hit , and the shell will finish as much of the name as it can – Works if you’re running tcsh or bash • Command history – Don’t re-type previous commands – use the up-arrow to access them • Wildcards – Special character(s) which can be expanded to match other file/directory names * Zero or characters ? Zero or one character – Examples: • ls *.txt • rm may-?-notes.txt Editing Text • Which text editor is “the best” is a holy war. Pick one and get comfortable with it. • Three text editors you should be aware of: – pico – Easy! Comes with pine (Dante’s email program) – emacs/xemacs – A heavily-featured editor commonly used in programming – vim/ – A lighter editor, also used in programming – Your opinion is wrong. Commands Contd..

: concatenation • -cat [file1] [file2] • Attach both file and gives the output

• more : views a file, pausing every screenful • more [file1] Redirection

• Standard Input is from keyboard • Standard Output is the monitor

• Able to change this! command < infile > outfile command < infile command > outfile command >> outfile –attach at end Pipes

• Output of one command “piped into” another command1 | command2 • Do the first command command1 • Feed its output as input for command2 Wildcards

Group of files accessed using wildcards

* matches everything ls g* lists all files beginning with g ls go.* lists files named go with any extension ls * lists all files Commands cond.

• history gives a history of your commands • date displays date >date • who is on the system • finger get more info about another user. • jobs one of a number of commands. Gives the job No. of your jobs. – kill a specified job number command

• find: find the file • find [place to find] –name “filename” –print • . – current directory • ~ home directory • / whole root directory

• chmod : change mode • ugo : user, group, other • rwx: read, , execute • 777 :read, write, execute for all • chmod 755 .c printing

• lpr : send print to default printer • a2ps –p : format text file in postscript and prints Compilation in c

• gcc –o • Make different executable file name for different files compiled. • Default executable is a.out • Run using ./a.out • Or a.out in 410 lab Detail on Compilation in c • g++ syntax: g++ [] • Some useful options: -g produce debugging info (for gdb) -Wall “Warnings all” -ansi force ANSI compliant compilation -D define in all source files -o output filename • Example: g++ -g –Wall –ansi –o hello hello.cpp Editor

• Choice is on you : • emacs • vi • gedit • pico • other Make

• The make utility is a software engineering tool for managing and maintaining computer programs. • make provides most help when the program consists of many component files. As the number of files in the program increases so to does the compile , complexity of compilation command and the likelihood of human error when entering command lines, i.e. typos and missing file names. make cont.

• By creating a descriptor file containing • dependency rules, • macros and • suffix rules • you can instruct make to automatically rebuild your program whenever one of the program's component files is modified. • make is smart enough to only recompile the files that were affected by changes thus saving compile time. make cont..

• Make goes through a descriptor file starting with the target it is going to create. • Make looks at each of the target's dependencies to see if they are also listed as targets. • It follows the chain of dependencies until it reaches the end of the chain and then begins backing out executing the commands found in each target's rule. • Make builds object files from the source files and then links the object files to create the executable. The Make Command

• make syntax:

make [-f ] []

• argument descriptions: -f use as the makefile default: Makefile or makefile compile configuration “target” default: first target in the makefile Lets Make to make breakfast • So, let's say we need (boiled) eggs, toast, and coffee for our breakfast. Then our breakfast rule will be simply: • breakfast : boiled_eggs toast coffee butter peel boiled_eggs put toast on a plate pour coffee into cup • So far so good, but we don't have boiled_eggs, we only have raw eggs. So we need another rule: • boiled_eggs : raw_eggs water pot stove pour water into pot put eggs in the water put pot on the stove boil until eggs ready Make cont.

• Now we need a rule to make a toast, and another to make coffee. I won't go through the excercise of writing these rules out: I think you get the idea. Makefile Syntax

• Rule Syntax: :

• The may be files and/or other targets • It must be a tab before , or it won’t work • The first rule is the default for make • Variable Syntax: =

• All variable values default to the shell variable values Simple example for Makefile • This is an example descriptor file to build an executable file called prog1. • It requires the source files file1.cc, file2.cc, and file3.cc. • An include file, mydefs.h, is required by files file1.cc and file2.cc. • You can run it in a normal way by : % CC -o prog1 file1.cc file2.cc file3.cc Descriptor file will run it like this : % make prog1 or if prog1 is the first target defined in the descriptor file % make Simple example of Descriptor file

#top level rule to compile the whole process all:prog1 #program is made of several source files prog1 : file1.o file2.o file3.o gcc -o prog1 file1.o file2.o file3.o #rule for the file “file1.o” file1.o : file1.cc mydefs.h gcc -c file1.cc #rule for the file “file2.o” file2.o : file2.cc mydefs.h gcc -c file2.cc #rule for the file “file3.o” file3.o : file3.cc gcc -c file3.cc #rule for cleaning files generator during compilation clean : rm file1.o file2.o file3.o Making Descriptor file smaller OBJS= file1.o file2.o file3.o Prog1:${OBJS} gcc -o prog1 ${OBJS} file1.o : file1.cc mydefs.h gcc -c file1.cc file2.o : file2.cc mydefs.h gcc -c file2.cc file3.o : file3.cc gcc -c file3.cc clean : rm ${OBJS} Making Descriptor file even smaller #this is macro OBJS= file1.o file2.o file3.o CC=gcc CFLAGS = -g –wall prog1:${OBJS} ${CC} -o $@ ${OBJS} file1.o file2.0: mydefs.h clean : rm ${OBJS} Make Power Features

• “Fake” targets – targets that are not actually files – can do just about anything, not just compile – like the “clean” target • Forcing re-compiles – the required files – touch the Makefile to rebuild everything Debugging

• The ideal: do it right the first time • The reality: bugs happen • The goal: exterminate, quickly and efficiently Debugging Methods

• The best way: read the code – If you don’t understand it, it’ll happen again • Consistent use of error checking & handling – most uncaught errors are not where they appear to be • Examine the state at key points in the code – wrap debug output with #ifdef DEBUG – use a debugger, like gdb, to view the state Efficiency in Debugging

• The goal is to isolate the problem and fix it • Don’t try random things, looking for a solution – if you don’t understand it…it’ll be back – half the time, this takes an insanely long time – you don’t learn anything from it • Look for the problem, not the solution – figure out two points in code that the problem is between, and close the gap from there – once you know the line the bug is on, it’s easy Breakpoints

• What are they? – A “break point”: a point in the code where execution pauses, or “breaks”, then continues • What are they for? – To examine the state of the running program • How do you make them? – modifying code to print information and pause – use a debugger to interactively step through code Debuggers • Advantages over the “old fashioned” way: – you can step through code as it runs – you don’t have to recompile to set a breakpoint – you can examine the entire state of the program • call stack, variable values, scope, etc. – you can modify values in the running program – you can view the state of a crash using core files GDB, the GNU DeBugger

• Text-based, invoked with: gdb [ [|]]

• Argument descriptions: executable program file core dump of program process id of already running program • Example: gdb ./hello • Compile with –g for debug info Basic GDB Commands

• General Commands: file [] selects as the program to debug run [] runs selected program with arguments attach attach gdb to a running process kill kills the process being debugged quit quits the gdb program help [] accesses the internal help documentation • Stepping and Continuing: c[ontinue] continue execution (after a stop) s[tep] step one line, entering called functions n[ext] step one line, without entering functions finish finish the function and print the return value