Linux Tutorials

Embed Size (px)

Citation preview

  • 7/27/2019 Linux Tutorials

    1/43

    Linux TutorialsNetwork Design/Computer

    visionby Helal Saghir

  • 7/27/2019 Linux Tutorials

    2/43

    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]$ helal:~$ ls l -a unix-tutorial

    Command Prompt Command

    (Optional) flags

    (Optional) arguments

    Note: Many Unix commands will print a message only if something

    went wrong. Be careful with rm and mv.

  • 7/27/2019 Linux Tutorials

    3/43

    Getting help with man man (short for manual) documentscommands

    man retrieves detailed informationabout

    man k searches the man pagesummaries (faster, and will probably givebetter results)

    man K searches the full text ofthe man pages

    helal:~$ man k password

    passwd (5) - password file

    xlock (1) - Locks the local X display

    until a password is entered

    helal:~$ passwd

  • 7/27/2019 Linux Tutorials

    4/43

    ssh (secure shell)

    Open secure shell from windows.

    Enter server name , username andpassword . You can login remotely.

    In linux use this command ssh [email protected]

    Will ask your password and let you enter.

  • 7/27/2019 Linux Tutorials

    5/43

    yppasswd

    To change the password on linux use

    passwd

    In lab you have to use yppasswd as we

    are running NIS.

  • 7/27/2019 Linux Tutorials

    6/43

    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 wont work

    because of NIS running on clients. It shouldwork on your home computer, if you have Linuxinstalled on it and quota rpm on.

  • 7/27/2019 Linux Tutorials

    7/43

    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

  • 7/27/2019 Linux Tutorials

    8/43

    ls command ls shows the list of files/directory

    ls al shows all files including hiddenwith detail.

    ls l - shows all files not including hidden. ls - shows the list in that

    directory

  • 7/27/2019 Linux Tutorials

    9/43

    cp cp copy files

    cp [source_file1] [des_file1]

    -p : preserve=mode,ownership,timestamp

    -R r : copy directories recursively

  • 7/27/2019 Linux Tutorials

    10/43

    Move / mkdir mv : move files /directory

    mv [source] [destination]

    mkdir : make directory Syntax: mkdir

    rmdir : remove directory which must beempty.

    Syntax: rmdir

  • 7/27/2019 Linux Tutorials

    11/43

    rm Rm remove non-empty directory or file

    -r: recursive

    -f : force

    -v: verbose

  • 7/27/2019 Linux Tutorials

    12/43

    Shell Shortcuts Tab completion

    Type part of a file/directory name, hit , and the shell will

    finish as much of the name as it can

    Works if youre running tcsh or bash

    Command history

    Dont re-type previous commands use the up-arrow to access

    them Wildcards

    Special character(s) which can be expanded to match otherfile/directory names

    * Zero or more characters? Zero or one character

    Examples: ls *.txt

    rm may-?-notes.txt

  • 7/27/2019 Linux Tutorials

    13/43

    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 (Dantes email

    program)

    emacs/xemacs A heavily-featured editorcommonly used in programming

    vim/vi A lighter editor, also used in

    programming Your opinion is wrong.

  • 7/27/2019 Linux Tutorials

    14/43

    Commands Contd.. Cat : concatenation

    -cat [file1] [file2]

    Attach both file and gives the output

    more : views a file, pausing everyscreenful

    more [file1]

  • 7/27/2019 Linux Tutorials

    15/43

    Redirection Standard Input is from keyboard

    Standard Output is the monitor

    Able to change this!

    command < infile > outfile

    command < infile

    command > outfilecommand >> outfile attach at end

  • 7/27/2019 Linux Tutorials

    16/43

    Pipes

    Output of one command piped intoanother

    command1 | command2

    Do the first command command1

    Feed its output as input for command2

  • 7/27/2019 Linux Tutorials

    17/43

    WildcardsGroup of files accessed using wildcards

    * matches everything

    ls g* lists all files beginning with g

    ls go.* lists files named go with any extensionls * lists all files

  • 7/27/2019 Linux Tutorials

    18/43

    Commands cond.

    history gives a history of yourcommands

    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

  • 7/27/2019 Linux Tutorials

    19/43

    Find command find: find the file

    find [place to find] name filename print

    . current directory

    ~ home directory / whole root directory

  • 7/27/2019 Linux Tutorials

    20/43

    chmod chmod : change mode

    ugo : user, group, other

    rwx: read, write, execute

    777 :read, write, execute for all chmod 755 test.c

  • 7/27/2019 Linux Tutorials

    21/43

    printing lpr : send print to default

    printer a2ps p :

    format text file in postscript and prints

  • 7/27/2019 Linux Tutorials

    22/43

    Compilation in c gcc o

    Make different executable file name fordifferent files compiled.

    Default executable is a.out Run using ./a.out

    Or a.out in 410 lab

  • 7/27/2019 Linux Tutorials

    23/43

    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

  • 7/27/2019 Linux Tutorials

    24/43

    Editor Choice is on you :

    emacs

    vi

    gedit pico

    other

  • 7/27/2019 Linux Tutorials

    25/43

    Make The make utility is a software engineering tool

    for managing and maintaining computerprograms.

    make provides most help when the program

    consists of many component files. As thenumber of files in the program increases so todoes the compile time, complexity of compilation

    command and the likelihood of human errorwhen entering command lines, i.e. typos andmissing file names.

  • 7/27/2019 Linux Tutorials

    26/43

    make cont. By creating a descriptor file containing

    dependency rules, macros and

    suffix rules

    you can instruct make to automatically rebuildyour program whenever one of the program'scomponent files is modified.

    make is smart enough to only recompile the filesthat were affected by changes thus savingcompile time.

  • 7/27/2019 Linux Tutorials

    27/43

    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 astargets.

    It follows the chain of dependencies until itreaches the end of the chain and then beginsbacking out executing the commands found ineach target's rule.

    Make builds object files from the source files andthen links the object files to create theexecutable.

  • 7/27/2019 Linux Tutorials

    28/43

    The Make Command make syntax:

    make [-f ] []

    argument descriptions:

    -f use as the makefile

    default: Makefile or makefile

    compile configuration targetdefault: first target in the makefile

  • 7/27/2019 Linux Tutorials

    29/43

    Lets Make to make breakfast So, let's say we need (boiled) eggs, toast, and coffee forour 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 onlyhave 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

  • 7/27/2019 Linux Tutorials

    30/43

    Make cont. Now we need a rule to make a toast, and

    another to make coffee.I won't go through the excercise of writingthese rules out: I think you get the idea.

  • 7/27/2019 Linux Tutorials

    31/43

    Makefile Syntax

    Rule Syntax:

    :

    The may be files and/or other targets It mustbe a tab before , or it wont work

    The first rule is the default for make

    Variable Syntax:

    =

    All variable values default to the shell variable values

  • 7/27/2019 Linux Tutorials

    32/43

    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, andfile3.cc.

    An include file, mydefs.h, is required by files file1.ccand 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 prog1or if prog1 is the first target defined in the descriptor file

    % make

  • 7/27/2019 Linux Tutorials

    33/43

    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

  • 7/27/2019 Linux Tutorials

    34/43

    Making Descriptor file smallerOBJS= 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}

  • 7/27/2019 Linux Tutorials

    35/43

    Making Descriptor file even smaller

    #this is macro

    OBJS= file1.o file2.o file3.o

    CC=gccCFLAGS = -g wall

    prog1:${OBJS}

    ${CC} -o $@ ${OBJS}

    file1.o file2.0: mydefs.h

    clean :

    rm ${OBJS}

  • 7/27/2019 Linux Tutorials

    36/43

    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

  • 7/27/2019 Linux Tutorials

    37/43

    Debugging The ideal:

    do it right the first time

    The reality:

    bugs happen

    The goal:

    exterminate, quickly and efficiently

  • 7/27/2019 Linux Tutorials

    38/43

    Debugging Methods The best way: read the code

    If you dont understand it, itll 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

  • 7/27/2019 Linux Tutorials

    39/43

    Efficiency in Debugging

    The goal is to isolate the problem and fix it

    Dont try random things, looking for a solution

    if you dont understand ititll be back

    half the time, this takes an insanely long time you dont learn anything from it

    Look for the problem, not the solution

    figure out two points in code that the problem isbetween, and close the gap from there

    once you know the line the bug is on, its easy

  • 7/27/2019 Linux Tutorials

    40/43

    Breakpoints What are they?

    A break point: a point in the code whereexecution 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

  • 7/27/2019 Linux Tutorials

    41/43

    Debuggers

    Advantages over the old fashionedway:

    you can step through code as it runs you dont have to recompile to set a

    breakpoint

    you can examine the entire state of theprogram

    call stack, variable values, scope, etc.

    you can modify values in the runningprogram

    you can view the state of a crash using

    core files

  • 7/27/2019 Linux Tutorials

    42/43

    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 gfor debug info

  • 7/27/2019 Linux Tutorials

    43/43

    Basic GDB Commands General Commands:

    file [] selects as the program to debugrun [] runs selected program with arguments

    attach attach gdb to a running process

    kill kills the process being debuggedquit 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 returnvalue