2. Basic Commands of Unix

Embed Size (px)

Citation preview

  • 8/16/2019 2. Basic Commands of Unix

    1/63

    2. Basic commands ofUnix

    1

  • 8/16/2019 2. Basic Commands of Unix

    2/63

    Unix - File Management All data in UNIX is organized into les. All les are

    organized into directories. Tese directories are

    organized into a tree-li!e str"ct"re called teles#stem. $en #o" %or! %it UNIX& one %a# or anoter #o"

    s'end most of #o"r time %or!ing %it les. Tis lesson

    %o"ld teac #o" o% to create and remo(e les& co'#and rename tem& create lin!s to tem etc. In UNIX tere are tree )asic t#'es of les *

    Ordinary Files * An ordinar# le is a le on te s#stem tatcontains data& text& or 'rogram instr"ctions

    Directories * +irectories store )ot s'ecial and ordinar# les.For "sers familiar %it $indo%s or Mac ,& UNIX directories aree"i(alent to folders.

    Special Files * ome s'ecial les 'ro(ide access to ard%ares"c as ard dri(es& /+-0,M dri(es& modems& and ternet

    ada'ters. ,ter s'ecial les are similar to aliases or sortc"tsand ena)le o" to access a sin le le "sin dierent names

    2

  • 8/16/2019 2. Basic Commands of Unix

    3/63

    3isting Files  To list te les and directories stored in te

    c"rrent director#. Use te follo%ing command$ls

    4ere is te sam'le o"t'"t of te a)o(ecommand *

    5

  • 8/16/2019 2. Basic Commands of Unix

    4/63

    3isting Files  Te command ls s"''orts te -l o'tion %ic %o"ld

    el' #o" to get more information a)o"t te listed les

    *

    6

  • 8/16/2019 2. Basic Commands of Unix

    5/63

    col"mns First Column: re'resents le t#'e and

    'ermission gi(en on te le. Belo% is te

    descri'tion of all t#'e of les. Second Column: re'resents te n"m)er of

    memor# )loc!s ta!en )# te le or director#.

    Third Column: re'resents o%ner of te le. Tis is te Unix "ser %o created tis le.

    Fourth Column7 re'resents gro"' of teo%ner. (er# Unix "ser %o"ld a(e an

    associated gro"'. Fifth Column: re'resents le size in )#tes. Sixth Column: re'resents date and time %en

    tis le %as created or modied last time.

    8

  • 8/16/2019 2. Basic Commands of Unix

    6/63

    te t#'e of le tat9s listed In te ls -l listing exam'le& e(er# le line )egan %it a

    d& -& or l. Tese caracters indicate te t#'e of le tat9s

    listed.

    :

    ;rex +escri'tion

    -0eg"lar le& s"c as an A/II text le& )inar#exec"ta)le& or ard lin!.

    bBloc! s'ecial le. Bloc! in'"t

  • 8/16/2019 2. Basic Commands of Unix

    7/63

    Meta /aracters Meta caracters a(e s'ecial meaning in Unix. For

    exam'le = and > are metacaracters. $e "se = to matc

    ? or more caracters& a "estion mar! > matces %itsingle caracter. For xam'le *

    +is'la#s all te les %ose name start %it c and ends%it .doc

    4ere = %or!s as meta caracter %ic matces %itan# caracter. If #o" %ant to dis'la# all te les ending

    %it @"st .doc ten #o" can "se follo%ing command *

  • 8/16/2019 2. Basic Commands of Unix

    8/63

    4idden Files An in(isi)le le is one %ose rst caracter is

    te dot or 'eriod caracter .C. UNIX 'rogramsincl"ding te sellC "se most of tese les tostore cong"ration information.

    ome common exam'les of idden les incl"de

    te les * .'role * te Bo"rne sell sC initialization

    scri't .!src * te Dorn sell !sC initialization

    scri't .csrc * te / sell csC initialization scri't .rosts * te remote sell cong"ration le

    E

  • 8/16/2019 2. Basic Commands of Unix

    9/63

     To list in(isi)le les  To list in(isi)le les& s'ecif# te -a o'tion to ls

    ingle dot . * Tis re'resents c"rrent director#. +o")le dot .. * Tis re'resents 'arent director#.

    F

  • 8/16/2019 2. Basic Commands of Unix

    10/63

    F es  Go" can edit an existing le "sing (i editor. #o" can

    o'en existing le as follo%s

    ,nce le is o'ened& #o" can come in edit mode )#'ressing !e# i and ten #o" can edit le as #o" li!e. If#o" %ant to mo(e ere and tere inside a le ten rst#o" need to come o"t of edit mode )# 'ressing !e# esc 

    and ten #o" can "se follo%ing !e#s to mo(e inside ale * l !e# to mo(e to te rigt side. h !e# to mo(e to te left side.

    k  !e# to mo(e "' side in te le.  j !e# to mo(e do%n side in te le.

    o "sing a)o(e !e#s #o" can 'osition #o"r c"rsor %eree(er #o" %ant to edit. ,nce #o" are 'ositioned ten#o" can "se i !e# to come in edit mode. dit te le&once #o" are done 'ress esc and nall# t%o !e#s Shift1?

  • 8/16/2019 2. Basic Commands of Unix

    11/63

    +is'la# /ontent of a File  Go" can "se cat command to see te content of a le.

    Follo%ing is te sim'le exam'le to see te content of

    a)o(e created le

     Go" can dis'la# line n"m)ers )# "sing -b o'tion along%it cat command as follo%s

    11

  • 8/16/2019 2. Basic Commands of Unix

    12/63

    /o"nting $ords in a File  Go" can "se te wc command to get a co"nt of te total

    n"m)er of lines& %ords& and caracters contained in ale. Follo%ing is te sim'le exam'le to see teinformation a)o"t a)o(e created le *

    4ere is te detail of all te fo"r col"mns * First /ol"mn7 re'resents total n"m)er of lines in te

    le. econd /ol"mn7 re'resents total n"m)er of %ords in

    te le.  Tird /ol"mn7 re'resents total n"m)er of )#tes in

    te le. Tis is act"al size of te le. Fo"rt /ol"mn7 re'resents le name. 12

  • 8/16/2019 2. Basic Commands of Unix

    13/63

    /o'#ing Files  To ma!e a co'# of a le "se te c' command.

     Te )asic s#ntax of te command is *$ cp source_file destination_file

    Follo%ing is te exam'le to create a co'# ofexisting le lename.

    $ cp filename copyfile

    $ No% #o" %o"ld nd one more le co'#le in

    #o"r c"rrent director#. Tis le %o"ld )e exactl#same as original le lename.

    15

  • 8/16/2019 2. Basic Commands of Unix

    14/63

    0enaming Files  To cange te name of a le "se te m(

    command. Its )asic s#ntax is:$ mv old_file new_file

    Follo%ing is te exam'le %ic %o"ld renameexisting le lename to ne%le7

    $ mv filename newfile

    $  Te mv command %o"ld mo(e existing le

    com'letel# into ne% le. o in tis case #o"%o"ld ne onl# ne"#le in #o"r c"rrentdirector#.

    16

  • 8/16/2019 2. Basic Commands of Unix

    15/63

    e e ng es To delete an existing le "se te rm 

    command. Its )asic s#ntax is *

    $ rm filename /a"tion7 It ma# )e dangero"s to delete a

    le )eca"se it ma# contain "sef"l

    information. o )e caref"l %ile "sing tiscommand. It is recommended to "se -io'tion along %it rm command.

    Follo%ing is te exam'le %ic %o"ldcom'letel# remo(e existing le lename7$ rm filename

    $ 18

    t U t

  • 8/16/2019 2. Basic Commands of Unix

    16/63

    tan ar Un x treams Under normal circ"mstances e(er# Unix

    'rogram as tree streams lesC o'ened for it

    %en it starts "' * stdin $ Tis is referred to as standard in'"t and

    associated le descri'tor is ?. Tis is alsore'resented as T+IN. Unix 'rogram %o"ld read

    defa"lt in'"t from T+IN. stdout $ Tis is referred to as standard o"t'"t and

    associated le descri'tor is 1. Tis is alsore'resented as T+,UT. Unix 'rogram %o"ld %rite

    defa"lt o"t'"t at T+,UT stderr $ Tis is referred to as standard error and

    associated le descri'tor is 2. Tis is alsore'resented as T+00. Unix 'rogram %o"ld %rite all

    te error message at T+00 1:

    %dentify the three #les the &nix system automatically opens to

    execute pro'rams and describe their function

    M t

  • 8/16/2019 2. Basic Commands of Unix

    17/63

    Management A director# is a le %ose sole @o) is to

    store le names and related information.All les& %eter ordinar#& s'ecial& ordirector#& are contained in directories.

    UNIX "ses a ierarcical str"ct"re fororganizing les and directories. Tisstr"ct"re is often referred to as adirector# tree . Te tree as a single root

    node& te slas caracter

  • 8/16/2019 2. Basic Commands of Unix

    18/63

    4ome +irector#  Te director# in %ic #o" nd #o"rself %en #o"

    rst login is called #o"r ome director#.  Go" %ill )e doing m"c of #o"r %or! in #o"r ome

    director# and s")directories tat #o"9ll )e creatingto organize #o"r les.

     Go" can go in #o"r ome director# an#time "singte follo%ing command * $cd ~ 4ere ( indicates ome director#. If #o" %ant to go

    in an# oter "ser9s ome director# ten "se te

    follo%ing command * $cd ~username  To go in #o"r last director# #o" can "se follo%ing

    command * $cd -

    1E

  • 8/16/2019 2. Basic Commands of Unix

    19/63

    ;atnames

     Te  pathname of a le incl"des tele name and te name of te

    director# tat olds te le& andte name of te director# tatolds te director# tat olds te

    le& and te name of te H "' tote root

     Te 'atname of e(er# le in a

    gi(en flesystem is "ni"e.

  • 8/16/2019 2. Basic Commands of Unix

    20/63

    ;atnames cont.C

     To create a 'atname #o" start atte root so #o" start %it

  • 8/16/2019 2. Basic Commands of Unix

    21/63

    ;atname xam'les

    /

     bin/ etc/ home/ tmp/ usr/

    Hollid2/ scully/ bin/ local/

    netprog unix/ X ls who

    /usr/bin/lsSyllabus

    /home/hollid2/unix/Syllabus

  • 8/16/2019 2. Basic Commands of Unix

    22/63

    A)sol"te ;atnames +irectories are arranged in a ierarc# %it

    root

  • 8/16/2019 2. Basic Commands of Unix

    23/63

    0elati(e ;atnames

    A 'atname can also )e relati(e to #o"r c"rrent

    %or!ing director#. 0elati(e 'atnames ne(er)egin %it

  • 8/16/2019 2. Basic Commands of Unix

    24/63

    3isting +irectories To list te les in a director# #o" can "se

    te follo%ing s#ntax *$ls dirname Follo%ing is te exam'le to list all te

    les contained in

  • 8/16/2019 2. Basic Commands of Unix

    25/63

    /reating +irectories +irectories are created )# te follo%ing

    command *$mkdir dirname

    4ere& director# is te a)sol"te or relati(e

    'atname of te director# #o" %ant tocreate. For exam'le& te command *$mkdir mydir

    $

    /reates te director# m#dir in te c"rrentdirector#.

    28

    /reating +irectories

  • 8/16/2019 2. Basic Commands of Unix

    26/63

    /reating +irectories 4ere is anoter exam'le *

    $mkdir /tmp/test-dir  Tis command creates te director# test-dir in

    te

  • 8/16/2019 2. Basic Commands of Unix

    27/63

    /reating ;arent +irectories ometimes %en #o" %ant to create a director#& its

    'arent director# or directories migt not exist. Intis case& m!dir iss"es an error message as follo%s*$mkdir /tmp/amrood/test

    mkdir !ailed to make directory"/tmp/amrood/test"#

    o such file or directory

    In s"c cases& #o" can s'ecif# te -p o'tion to tem!dir command. It creates all te necessar#

    directories for #o". For exam'le *$mkdir -p /tmp/amrood/test

    $

    A)o(e command creates all te re"ired 'arentdirectories. 2

  • 8/16/2019 2. Basic Commands of Unix

    28/63

    0emo(ing +irectories +irectories can )e deleted "sing te rmdir

    command as follo%s *$rmdir dirname

    Note * To remo(e a director# make sure it isempty %ic means tere so"ld not )e an#

    le or s")-director# inside tis director#.  Go" can remo(e m"lti'le directories at a time as

    follo%s *

    $rmdir dirname% dirname& dirname3 A)o(e command remo(es te directories

    dirname1& dirname2& and dirname2 if te# areem't#. Te rmdir command 'rod"ces no o"t'"t

    if it is s"ccessf"l. 2E

  • 8/16/2019 2. Basic Commands of Unix

    29/63

    /anging +irectories  Go" can "se te cd command to do more tan

    cange to a ome director#7 Go" can "se it tocange to an# director# )# s'ecif#ing a (alida)sol"te or relati(e 'at. Te s#ntax is asfollo%s *

    )cd dirname 4ere& dirname is te name of te director# tat

    #o" %ant to cange to. For exam'le& te

    command *)cd *usr*local*bin

    /anges to te director#

  • 8/16/2019 2. Basic Commands of Unix

    30/63

    0enaming +irectories Te m( mo(eC command can also )e

    "sed to rename a director#. Te s#ntax isas follo%s *$mv olddir newdir

    $ Go" can rename a director# m#dir to

    #o"rdir as follo%s *

    $mv mydir yourdir$

    5?

    Te directories dotC and dot dotC

  • 8/16/2019 2. Basic Commands of Unix

    31/63

     Te directories . dotC and .. dot dotC  Te lename . dotC re'resents te c"rrent

    %or!ing director#J and te lename .. dot dotC

    re'resent te director# one le(el a)o(e tec"rrent %or!ing director#& often referred to aste 'arent director#.

    If %e enter te command to so% a listing ofte c"rrent %or!ing directories les and "se te-a o'tion to list all te les and te -l o'tion'ro(ides te long listing& tis is te res"lt.

    51

  • 8/16/2019 2. Basic Commands of Unix

    32/63

    Unix - File ;ermission < Access Modes

    File o%nersi' is an im'ortant com'onent of

    UNIX tat 'ro(ides a sec"re metod for storingles. (er# le in UNIX as te follo%ingattri)"tes *

    O"ner permissions * Te o%ner9s'ermissions determine %at actions te o%nerof te le can 'erform on te le.

    ,roup permissions * Te gro"'9s 'ermissions

    determine %at actions a "ser& %o is amem)er of te gro"' tat a le )elongs to& can'erform on te le.

    Other "orld. permissions * Teermissions for oters indicate %at action all

    52

    T ; i i I di t

  • 8/16/2019 2. Basic Commands of Unix

    33/63

     Te ;ermission Indicators $ile "sing ls -l command it dis'la#s (ario"s

    information related to le 'ermission as follo%s*

    4ere rst col"mn re'resents dierent accessmode ie. 'ermission associated %it a le or

    director#.  Te 'ermissions are )ro!en into gro"'s of

    trees& and eac 'osition in te gro"' denotesa s'ecic 'ermission& in tis order7 read rC&

    %rite %C& exec"te xC * 55

    e erm ss on n cators

  • 8/16/2019 2. Basic Commands of Unix

    34/63

    e erm ss on n cators

     Te rst tree caracters 2-6C re'resent te'ermissions for te le9s o%ner. For exam'le

    -r"xr-xr-- re'resents tat on%er as read rC&%rite %C and exec"te xC 'ermission.

     Te second gro"' of tree caracters 8-C consistsof te 'ermissions for te gro"' to %ic te le

    )elongs. For exam'le -r"xr-xr-- re'resents tatgro"' as read rC and exec"te xC 'ermission )"tno %rite 'ermission.

     Te last gro"' of tree caracters E-1?C

    re'resents te 'ermissions for e(er#one else. For56

    File Access Modes

  • 8/16/2019 2. Basic Commands of Unix

    35/63

    File Access Modes  Te 'ermissions of a le are te rst line of

    defense in te sec"rit# of a Unix s#stem. Te

    )asic )"ilding )loc!s of Unix 'ermissions arete read& %rite& and exec"te 'ermissions& %icare descri)ed )elo% *

    /+ 0ead Krants te ca'a)ilit# to read ie. (ie% te

    contents of te le.

    1+ 2rite Krants te ca'a)ilit# to modif#& or remo(e

    te content of te le.

    3+ 4xecute

    User %it exec"te 'ermissions can r"n a le58

    +irector# Access Modes

  • 8/16/2019 2. Basic Commands of Unix

    36/63

    +irector# Access Modes +irector# access modes are listed and organized in

    te same manner as an# oter le. Tere are a fe%

    dierences tat need to )e mentioned71. 0ead

    Access to a director# means tat te "ser can read tecontents. Te "ser can loo! at te lenames inside te

    director#.2. $rite

    Access means tat te "ser can add or delete les to tecontents of te director#.

    5.  xec"te xec"ting a director# doesn9t reall# ma!e a lot of sense so

    tin! of tis as a tra(erse 'ermission. A "ser m"st a(e exec"te access to te )in director# in

    order to exec"te ls or cd command. 5:

    /anging ;ermissions

  • 8/16/2019 2. Basic Commands of Unix

    37/63

    /anging ;ermissions

     To cange le or director# 'ermissions& #o" "sete chmod cange modeC command. Tere are

    t%o %a#s to "se cmod7 s#m)olic mode anda)sol"te mode.

    &sin' chmod in Symbolic 5ode Te easiest %a# for a )eginner to modif# le

    or director# 'ermissions is to "se tes#m)olic mode. $it s#m)olic 'ermissions#o" can add& delete& or s'ecif# te

    'ermission set #o" %ant )# "sing te5

    Using cmod in #m)olic Mode

  • 8/16/2019 2. Basic Commands of Unix

    38/63

    Using cmod in #m)olic Mode

    Chmodoperator

    Description

    Adds te designated 'ermissionsC to ale or director#.

    -0emo(es te designated 'ermissionsC

    from a le or director#.

    6 ets te designated 'ermissionsC.

    5E

    Mode

  • 8/16/2019 2. Basic Commands of Unix

    39/63

    Mode 4ere9s an exam'le "sing testle. 0"nning ls -%

    on testle so%s tat te le9s 'ermissions are as

    follo%s *

     Ten eac exam'le cmod command from te'receding ta)le is r"n on testle& follo%ed )# ls -lso #o" can see te 'ermission canges *

    5

    /anging ,%ners and Kro"'s

  • 8/16/2019 2. Basic Commands of Unix

    40/63

    /anging ,%ners and Kro"'s $ile creating an acco"nt on Unix& it assigns a

    o%ner I+ and a gro"' I+ to eac "ser. All te

    'ermissions mentioned a)o(e are also assigned)ased on ,%ner and Kro"'s.

     T%o commands are a(aila)le to cange te

    o%ner and te gro"' of les * cho"n * Te co%n command stands for

    cange o%ner and is "sed to cange teo%ner of a le

    ch'rp * Te cgr' command stands forcange gro"' and is "sed to cange tegro"' of a le.

    6?

    /anging ,%nersi'

  • 8/16/2019 2. Basic Commands of Unix

    41/63

    /anging ,%nersi'  Te co%n command canges te o%nersi' of

    a le. Te )asic s#ntax is as follo%s *

    $ chown user filelist  Te (al"e of "ser can )e eiter te name of a

    "ser on te s#stem or te "ser id "idC of a "ser

    on te s#stem. Follo%ing exam'le *

    $ chown amrood testfile

    /anges te o%ner of te gi(en le to te "seramrood. 7OT4: Te s"'er "ser& root& as te "nrestricted

    ca'a)ilit# to cange te o%nersi' of a an# le )"t

    normal "sers can cange onl# te o%ner of les te#o%n61

    ,%nersi'

  • 8/16/2019 2. Basic Commands of Unix

    42/63

    ,%nersi'  Te crg' command canges te gro"'

    o%nersi' of a le. Te )asic s#ntax is asfollo%s *

    $ ch'rp 'roup filelist  Te (al"e of gro"' can )e te name of a gro"'

    on te s#stem or te gro"' I+ KI+C of a gro"'on te s#stem.

    Follo%ing exam'le *

    ) ch'rp special test#le

    /anges te gro"' of te gi(en le to s'ecialgro"'.

    62

    a 'rocess in Unix terminolog#

  • 8/16/2019 2. Basic Commands of Unix

    43/63

    a 'rocess in Unix terminolog# $en #o" exec"te a 'rogram on #o"r UNIX

    s#stem& te s#stem creates a s'ecial

    en(ironment for tat 'rogram. Tisen(ironment contains e(er#ting needed forte s#stem to r"n te 'rogram as if no oter'rogram %ere r"nning on te s#stem.

    $ene(er #o" iss"e a command in UNIX& itcreates& or starts& a ne% 'rocess. $en #o"tried o"t te ls command to list director#

    contents& #o" started a 'rocess. 8 process9 insimple terms9 is an instance of a runnin'pro'ram+

    65

    /+; 4xplain a process in &nix terminolo'y and themeanin' of

  • 8/16/2019 2. Basic Commands of Unix

    44/63

    a 'rocess in Unixterminolog#

     Te o'erating s#stem trac!s 'rocessestro"g a (e digit I+ n"m)er !no%n as

    te 'id or 'rocess I+ . ac 'rocess inte s#stem as a "ni"e 'id.

    ;ids e(ent"all# re'eat )eca"se all te

    'ossi)le n"m)ers are "sed "' and tenext 'id rolls or starts o(er. At an# onetime& no t%o 'rocesses %it te same

    'id exist in te s#stem )eca"se it is te66

  • 8/16/2019 2. Basic Commands of Unix

    45/63

    Inter-'rocesscomm"nication

    Inter-'rocess comm"nication I;/C refers to temecanisms

  • 8/16/2019 2. Basic Commands of Unix

    46/63

    File descri'tors

    ac le a 'rocess as access to isidentied )# a small nonnegati(e

    integer& called a le descri'tor. Filedescri'tors are created )# s#stem calls%ic o'en les and are inerited )#

    ne% 'rocesses %ic are for!ed ofrom te c"rrent 'rocess. Tat is& %ena 'rocess starts a ne% 'rogram& teoriginal 'rocess9s o'en les arenormall inerited ) te ne% ro ram.

    6:

    Fil d i t

  • 8/16/2019 2. Basic Commands of Unix

    47/63

    File descri'torsB# con(ention& most 'rograms reser(e

    te rst tree le descri'tors ?& 1& and2C for a s'ecial '"r'oses -- access tote so-called standard input&

    standard output& and standarderror streams. File descri'tor ? is standard in'"t& %ere

    man# 'rograms ex'ect to recei(e in'"tfrom te o"tside %orld.

    File descri'tor 1 is standard o"t'"t. Most'rograms dis'la# normal o"t'"t tere.

    For o"t'"t related to error conditions& le6

    Unix le access 'rimiti(es

  • 8/16/2019 2. Basic Commands of Unix

    48/63

    Unix le access 'rimiti(es

    %ill next disc"ss o% to do I

  • 8/16/2019 2. Basic Commands of Unix

    49/63

    'rimiti(es7

    o'en - o'en for reading or %riting& or

    create an em't# le creat - create an em't# le close - read - get info from le %rite - '"t info in le lsee! - mo(e to s'ecic )#te in le "nlin! - remo(e a le remo(e - remo(e a le

    6

  • 8/16/2019 2. Basic Commands of Unix

    50/63

    File I

  • 8/16/2019 2. Basic Commands of Unix

    51/63

    File I

  • 8/16/2019 2. Basic Commands of Unix

    52/63

    File I

  • 8/16/2019 2. Basic Commands of Unix

    53/63

    Oincl"de P "nistd. Q

    mainC R

      int fdJ  ssizeSt nreadJ

      car )"f1?26J

    fd V o'endata& ,S0+,N3GCJ

  • 8/16/2019 2. Basic Commands of Unix

    54/63

    Oincl"de P stdli). Q

    Oincl"de P fcntl. Q

    car =%or!leV@"n!J

    mainC

    R

      int ledesJ

      if ledes V o'en%or!le& ,S0+$0CC VV -1C

      R

    'rintf/o"ldn9t o'en sYn& %or!leCJ

    exit1CJ

  • 8/16/2019 2. Basic Commands of Unix

    55/63

     Oincl"de P s#s

  • 8/16/2019 2. Basic Commands of Unix

    56/63

    %riteco'# data from 'rogram )"er to external le

    GN,;I  Oincl"de P "nistd. Q

      ssizeSt %riteint ldes& const (oid =)"f& sizeSt n)#teCJ

    +/0I;TI,N

      Te %riteC f"nction attem'ts to %rite n)#te )#tesfrom te

      )"er 'ointed to )# )"f to te le associated %itte

      o'en le descri'tor& ldes. +ata T#'e7 ssizeSt

     Tis data t#'e is "sed to re'resent te sizes of )loc!s tatcan )e read or %ritten in a single o'eration

    8:

    xam'le of "se7

  • 8/16/2019 2. Basic Commands of Unix

    57/63

    xam'le of "se7int fdJ

    ssizeS1t %1& %2J

    car )"f1812& )"f21?26l

    ... ll )"f1 and )"f2

    if fdVo'ensomele& ,S$0,N3G Z ,S/0AT Z ,SX/3&?:66CC VV -1C

    ret"rn -1Cl

    %1 V %ritefd& )"f1& 812CJ

    %2 V %ritefd& )"f2& 1?26CJ

    %ill %rite 185: )#tes to somele& o(er%riting an#ting

    tere alread# 8

    t d d I

  • 8/16/2019 2. Basic Commands of Unix

    58/63

    tandard I

  • 8/16/2019 2. Basic Commands of Unix

    59/63

    / li) f tiint getcar(oidC 

  • 8/16/2019 2. Basic Commands of Unix

    60/63

    / li)rar# f"nction -getcarC

     Te / li)rar# f"nction int getcar(oidC gets acaracter an "nsigned carC from stdin. Tis is

    e"i(alent to getc %it stdin as its arg"ment. Follo%ing is te declaration for

    'etchar() function

    Tis f"nction ret"rns te caracter read as an"nsigned car cast to an int or ,F on end of leor error.

    :?

  • 8/16/2019 2. Basic Commands of Unix

    61/63

    / li)rar# f"nction - 'rintfC

     Te / li)rar#

    function int printf(const char *format+ ,,,)

    sends formatted o"t'"t to stdo"t.

    Follo%ing is te declaration for 'rintfC f"nction.

    int printf(const char *format+ ,,,)

    format * Tis is te string tat contains te text to )e%ritten to stdo"t.

    :1

    xam'le

  • 8/16/2019 2. Basic Commands of Unix

    62/63

    'Oincl"de Pstdio.Q

    int main CR

      car cJ

     

    'rintfnter caracter7 CJ

      c V getcarCJ

     

    'rintf/aracter entered7 CJ

      '"tcarcCJ

      ret"rn?CJ

    W :2

  • 8/16/2019 2. Basic Commands of Unix

    63/63

    xam'le

    3et "s com'ile and r"n te a)o(e'rogram tat %ill 'rod"ce te follo%ing

    res"lt7nter caracter7 a

    /aracter entered7 a