Linux Tutorials Network Design/Computer Vision by Helal Saghir the Command Prompt
Total Page:16
File Type:pdf, Size:1020Kb
Linux Tutorials Network Design/Computer vision by Helal Saghir The Command Prompt • Commands are the way to “do things” in Unix • A command consists of a command name and options called “flags” • Commands are typed at the command prompt • In Unix, everything (including commands) is case- sensitive [prompt]$ <command> <flags> <args> helal:~$ ls –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 rm and mv. Getting help with man • man (short for “manual”) documents commands – man <cmd> retrieves detailed information about <cmd> – man –k <keyword> searches the man page summaries (faster, and will probably give better results) – man –K <keyword> searches the full text of the man pages helal:~$ man –k password passwd (5) - password file 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 • 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 • cd – 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 <directory> - shows the list in that directory cp • cp – copy files • cp [source_file1] [des_file1] • -p : preserve=mode,ownership,timestamp • -R –r : copy directories recursively Move / mkdir • mv : move files /directory • mv [source] [destination] • mkdir : make directory – Syntax: mkdir <directories> • rmdir : remove directory which must be empty. – Syntax: rmdir <directories> rm • Rm –remove non-empty directory or file • -r: recursive • -f : force • -v: verbose Shell Shortcuts • Tab completion – Type part of a file/directory name, hit <tab>, 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 more 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/vi – A lighter editor, also used in programming – Your opinion is wrong. Commands Contd.. • Cat : 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 who is on the system • finger get more info about another user. • jobs one of a number of job control commands. Gives the job No. of your jobs. – kill kill a specified job number Find command • find: find the file • find [place to find] –name “filename” –print • . – current directory • ~ home directory • / whole root directory chmod • chmod : change mode • ugo : user, group, other • rwx: read, write, execute • 777 :read, write, execute for all • chmod 755 test.c printing • lpr <filename> : send print to default printer • a2ps –p <printername> <filename> : format text file in postscript and prints Compilation in c • gcc –o <executable_file> <file1.c> • 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++ [<options>] <input files> • Some useful options: -g produce debugging info (for gdb) -Wall “Warnings all” -ansi force ANSI compliant compilation -D<sym> define <sym> in all source files -o <name> 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 time, 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 <file>] [<target>] • argument descriptions: -f <file> use <file> as the makefile default: Makefile or makefile <target> 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 <TAB> peel boiled_eggs <TAB> put toast on a plate <TAB> 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 <TAB> pour water into pot <TAB> put eggs in the water <TAB> put pot on the stove <TAB> 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: <target>: <requirements> <command> • The <requirements> may be files and/or other targets • It must be a tab before <command>, or it won’t work • The first rule is the default <target> for make • Variable Syntax: <variable>=<string value> • 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 – touch 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