linux record - theory

Upload: satish-kumar-j

Post on 07-Aug-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/21/2019 Linux Record - Theory

    1/84

    THE Vi EDITOR:-vi(short for visual editor).This is the popular editor in Unix since last 30 years. Which isused to create a file and to modify an existing file and to append text to an existing file.It has three modes.Command mode:-In this mode user can enter commands and all the keys pressed y the

    user are interpreted to editor commands. !or example" if you hit a h the cursor is movedone position to the left. In command mode the keys that are hit are not displayed on thescreen. This command mode is also called as #$% modeInsert mode:-This mode permits insertion of ne& text" editing of existing text or replacement of existing text. #ach of these operations can e performed y only after changing over from the command mode to insert mode using appropriate commands. Theinsertion mode is also kno&n as input'text mode. To move from command mode to inputmode" &e have to press i. To move from insert mode to command mode &e have to press #$% key.The ex command mode: -This mode permits us to give commands at the command line.The ottom line of the vi editor is called the command line. *i uses the command line to

    display messages and commands.

    Example:-

    +et us no& create a file "add text to it"save this file on the disk and then ,uit out of vi. Toinvoke vi type vi and the name of the file you &ant to create as sho&n elo&.

    -vi filename&hen &e type the aove command"vi clears the screen and displays a &indo& in &hichyou can enter and edit text.The (underscore) on the top line sho&s the cursor &aiting for you to enter a command here.#very other line is marked &ith a /(tilde)" the symol for anempty line.The screen as it looks like is sho&n elo&.

      ////////1letter24e& file5

  • 8/21/2019 Linux Record - Theory

    2/84

    If &e have successfully entered vi" y default &e are in command mode and vi is &aitingfor your command.6o& do &e create text7+et us no& add text to the file letter.

    8ress the i key to enter the insert mode of vi.(9o not press the enter key).We can no& add

    text to the file.(the I is not printed on the screen) 4o& type text8ress the #$% :ey and press ;& to save the contents of a fileThe ;, command is used to exit from the vi editor.The ;&, command is used to save the file and ,uit from the vi editor.

  • 8/21/2019 Linux Record - Theory

    3/84

    FILE HANDLIN !TILITIE":

    #$%to&ch command:-This command is used to create empty file. It doesnt allo& you to

    store anything in a file.

    #x;' -touch sampleThis creates a file called sample

    #xne is to create ne& file and todisplay the contents of an existing file.?nd it is also used to append text to an existingfile and also to concatenate the contents of t&o files and store them in the third file.

    #x;' -cat @test Unix is a multiuser operating system Ad

    The aove command is used to create a file called test

    #x

  • 8/21/2019 Linux Record - Theory

    4/84

    #x

  • 8/21/2019 Linux Record - Theory

    5/84

    #x

  • 8/21/2019 Linux Record - Theory

    6/84

    #xP;' -ls El'r&'r&'r&' JK#%JK#%< !e 3 ther possile file typesare given in the follo&ing figure!ile TypeJeaning

    r'>rdinary file

    d'9irectory file

    c'%haracter special file

     'Hlock special file

    l'$ymolic link 

    s'$emaphore

     p'4amed pipe

    m'$hared memory fileThe aove command displays names of files and directories of current directory in longfashion.That is" file permissions"o&ner name"group"links"time"siQe and names.

    L

  • 8/21/2019 Linux Record - Theory

    7/84

    DIRECTOR RELATED CO..AND":-

    #$p/d command:-It displays current &orking directory.

    #x; -p&dGhomeGJK#%

    '$m0dir command:- This command is used to create a ne& directory#x;' -mkdir ook 

    The aove command creates a directory named ook 

    #x;' -mkdir Ep &orksGpGook The Ep option is used to create multiple generations of directories at a time.The Ep optiontells unix to first create &orks then &ithin it p"next its child directory.

    #x;' -mkdir 'm MC= ne&dir 

    The aove command is used to create a directory &hich should have permissions MC=irrespective of the umask value.

    ($ rmdir command:- It is used to remove directories.With the E option "rmdir can ale toremove the specified firectory ut also its parent directories.6o&ever rmdir only removesthe empty directories.

    #x;' -rmdir &orksGpGook The aove command removes the directory ook.

    #x;' -rmdir Ep &orksGp

    The aove command removes oth directories at a time ut these directories must eempty.

    )$cd command:-It is used to change the directory#x;' -cd directoryname

    M

  • 8/21/2019 Linux Record - Theory

    8/84

    "EC!RIT 1 FILE 2ER.I""ION":

    "ec&rit3:-$ecurity is one of the feature of Unix operating system.Unix has 3 inherent

     provisions for protecting data.The first is provided y assigning pass&ords and login names to individual users ensuringthat not anyody can come and have access to your &ork.?t the file level"there are read"&rite and execute permissions to each file &hich decide&ho can access a particular file"&ho can modify it and &ho can execute it.Rou mayreserve read and &rite permissions for yourself and leave others on the net&ork free toexecute it"or any such comination.+astly"there is file encryption.This utility encodes your file into an unreadale format"sothat even if someone succeeds in opening it"your secrets are safe.>f course should you&ant to see the contentsyou can al&ays decrypt the file.

    The existing file permissions can e changed y the o&ner of the file or y the superuser.There are three types of permissions to a file.

      8ermission WeightKead(r) =Write(&) <#xecute(e)

    Example:-

     -ls El sample

    'r&xr&'r'' JK#% JK#%

  • 8/21/2019 Linux Record - Theory

    9/84

    #xecute(x)

    Kead and &rite(r&) =S

  • 8/21/2019 Linux Record - Theory

    10/84

    The S refers to add permissionThe E refers to remove permissionThe refers to add specified permossion and removes the existing permissions

    #x;' -chmod S& sampleThis command is used to add &rite permissions to all(o&ner"group V others)

    #x

  • 8/21/2019 Linux Record - Theory

    11/84

    2ROCE"" !TILITIE":-

    #$ps command:- This command is used to display the status of the running processes at

    any instant.#x;' -ps8I9 TTR TIJ# %>JJ?49

  • 8/21/2019 Linux Record - Theory

    12/84

    DI"5 !TILITIE":-

    #$d6 command7Dis0 Free command$:- This command reports the free as &ell as theused disk space for all the file system installed on your machine.

    #x;' -df  

    G (GdevGroot);

  • 8/21/2019 Linux Record - Theory

    13/84

    CO..!NICATION CO..AND":-

    Unix has excellent provision for communicating &ith other users. The communicationmay e &ithin the net&ork of a single main computer or et&een t&o or more suchcomputer net&orks. The users can easily exchange mail"data"programs through such

    net&orks. We have different net&ork commands(communication commands).

    #$/rite command:- The &rite command can e used y any user to &rite something onsomeone elses terminal.

    #x;' -&rite unix psG6ello $tudent I am >$#86%trl d

    The aove command displays the specified message to the user &hose login name isuser

  • 8/21/2019 Linux Record - Theory

    14/84

    6appy ne& year %trl d

    The aove command &ill send message to use<#x

  • 8/21/2019 Linux Record - Theory

    15/84

    LIN5 FILE":- Unix supports t&o types of links for files and directories naminglyhard links and symolic links

    Hard lin0s:-

    #x;' -ln file file<

    !ile< contains the same information of file. This command is used to estalish a link  et&een file and file

  • 8/21/2019 Linux Record - Theory

    16/84

    FILTER":

    Text processin8 &tilities:-

    #$/c command:- It counts the numer of lines"&ords and characters in the specified fileor files.It comes &ith the options El"'& and Ec &hich allo& the user to otain the numer of lines"&ords or characters individually or in any desired comination.

    #x ;' -&c lc file file<!ile

  • 8/21/2019 Linux Record - Theory

    17/84

    #xL;' -sort 'm fileThe aove command is used to comine the contents of a file &ith the input from thekeyoard and then carry out the sorting. Where ' stands for the standard input i.e thekeyoard.

    #xM;' -cat @employJadhukar 0000 Doseph

  • 8/21/2019 Linux Record - Theory

    18/84

  • 8/21/2019 Linux Record - Theory

    19/84

    This command &ould display only those lines &hich end &ith any character et&een sto Q.

    *$68rep76ixed 8rep$ command:-fgrep is used to search a group of strings.6ere eachstring has to e separated from other y a ne&line.

    #x;' -fgrep rao@ram@raDu

    This command displays those lines having either rao or ram or raDu.

    ,$e8rep7Extended 8rep$:-It is also used to search for a group of strings.6ere each stringhas to e separated from other y pipe( X ).

    #x;' -egrep raoXramXraDu filename

    9$head command:-This command is used to display the lines from the eginning of afile.

    #x;' -head filenameThis command displays first 0 lines of the given file.#x

  • 8/21/2019 Linux Record - Theory

    20/84

    JK#%#x;' -nl file

    India< ?p3 4iQamaad

    = JK#%#'$&niG command:-This command displays uni, lines of the given files.That is if successive lines of a file are same then they &ill e removed.Hy default output &ill e onto the screen.

    #x;' -cat @file<JK#%JK#%JK#%6R9#K?H?96R9#K?H?9

    Ad#x; -uni, file<JK#%6R9#K?H?9

    #x

  • 8/21/2019 Linux Record - Theory

    21/84

    C3 $athish#x;' -cat @file=

    0 %$#MO %$IT=< #I#

    a-?< ;>A-M?< x3

    This command replaces all lo&er case characters of the file xyQ to uppercase.

  • 8/21/2019 Linux Record - Theory

    22/84

    Ex*:- tr -d ;< x3

    This command removes all occurrences of F in the given file xyQ.

    Ex,:- tr =s ;< x3

    This command replaces multiple consecutive Fs &ith single O in the given file.

    #,$di66 command:-The diff command compares text files. It gives an index of all thelines that differ in the t&o files along &ith the line numers. It also displays &hat needs to e changed.diff filename filename<

    #x;' -diff sample sample<cB ass''''

    @ add3d<Bass

    If the content of sample and sample< are exactly same it displays nothing. >ther&ise itdisplays the difference information in a special format such as aove information.

    #9$ cmp command:- The cmp command compares t&o files (text or inary) yte'y'yteand displays the first occurrence &here the files differ.

    %mp filename5 filename

  • 8/21/2019 Linux Record - Theory

    23/84

    #$tee command:- It used to save intermediate results in a piping se,uence. It accepts aset of filenames as arguments and sends its standard input to all these files &hile givingthe same as standard output

    #x;' - ls El X grep 1Ad2 X tee xyQ

    The aove command saves the details of the directories of current &orking directory intoxyQ file.

  • 8/21/2019 Linux Record - Theory

    24/84

    1AC5!2 !TILITIE":-

    #$tar command:- This command is used to create and extract archives" one file thatcontains one or (usually) many other files. The name \tar\ itself comes from the phrase\tape archive\" ut that]s Dust an old name. I mostly Dust create archives and then sendthem over the &ire these days.

    To comine multiple files andGor directories into a single file" use the follo&ingcommand;

    Ex: tar -c+6 6ilename%tar inp&t6ile7s$

    Creatin8 archi+es

    To create an archive of all files in your current directory" and all sudirectories" use thiscommand;

    tar c+6 m36iles%tar

    his command creates a ne& file named myfiles.tar that contains all the files anddirectories from your current directory (specified y the F filename metacharacter). Itdoesn]t remove your existing files" it Dust creates an archive that also contains your files.

    In this command the c option means \create\ and the f option lets you specify the

    filename for the archive.

    Listin8 6iles in an archi+e

    To list files in an archive use the t option" like this;

    -tar tvf myfiles.tar 

    Extractin8 archi+es

    To extract all the files from an archive use the x option;

    -tar xvf myfiles.tar 

    This command extracts all files from archive.

    #x=;' - tar ExvZf a.tQ

  • 8/21/2019 Linux Record - Theory

    25/84

    This command exctracts all files from the compressed archive.

    '$cpio command:  cpio stands for \copy input output.\ cpio simply copies its input to itsoutput. cpio is not capale of finding files on its o&n" so it is usually used in comination&ith the  find  command.

    !nderstandin8 cpio command options:

    • i ; (copy in) Keads an archive from the standard input and conditionally extracts

    the files contained in it and places them into the current directory tree.

      t ; 8rints a tale of contents of the input.  o ; (copy out) Keads a list of file path names from the standard input and copiesthose files to the standard output in the form of a cpio archive.  + ; *erose

    !or example" the cpio command to ackup for the files &hich egins &ith p to a tapeunit named GdevGst0 &ould e;

    Create 4ac0&p 6ile &sin8 cpio:

    $ls p*|cpio -ov /dev/st0

    Restore 6ile &sin8 cpio

    To copy from tape ack to a directory" use the command as follo&s;$cpio -icvD < /dev/rmt/

    Vie/ the contents o6 cpio

    Use the command as follo&s;$cpio -it < /dev/st0

    ($rlo8in command:- With the help of this command it is possile to login to remotemachine if &e happened to have legal username and pass&ord on that machine. When &edo so the current machine ecomes terminal to that remote machines. ?fter that &hatever file &e create it &ill e stored in that remote machine.Usually"rlogin is used for UI ased remote login service unlike telnet service.

    #x;' - rlogin Ipaddress or machine name 'l username

    ($telnet command:-

    $yntax;' telnet Ipaddress or machinename

    The follo&ing output appear on the screenTrying

  • 8/21/2019 Linux Record - Theory

    26/84

    #scape character is A5.Ked 6at linux release P (shrike):ernel nce &e have logged in" &e can do&nload files &ithcommand get filename. We can put files of the local directory using put filename.,$arp command:- This command is used to manipulate arp cache. That is" &e can see thearp cache" remove a hosts entry from the arp cache"etc

  • 8/21/2019 Linux Record - Theory

    27/84

    IPO REDIRECTION O2ERATOR":-

    O&tp&t redirection operator7$:-  The symol @ implies redirection of output. Thesymol @ sends the output of a command to a file or a device such as a printer.

    #x;' -cat Bfile @ file<

    This makes cat command to take input from the file file and &rite its output to the filefile<

    #x

  • 8/21/2019 Linux Record - Theory

    28/84

    "HELL":

    The follo&ing example sho&s ho& to move to other shells- ash ^Jove to Hash shell

    - ksh ^Jove to :orn shell- csh ^Jove to % shell

    #%Lo8in "hell Veri6ication

    U4IY contains a system variale" $6#++" that identifies path to our login shell echo "HELL

    GinGksh'%C&rrent "hell Veri6ication

    >ur current shell may or may not e our login shell. We use the follo&ing command todetermine our current shell. 6o&ever this command &orks only &ith :orn and Hashshells_ it does not &ork &ith % shell.

    echo ksh(%Redirection

    9efinition; Kedirection is the process y &hich &e specify that a file is to e used in place of one of the standard files. There are three types. They areInp&t redirection

    #g; cat B filenameIn the aove command contents of given input file &ill ecome input to the 1catcommand2O&tp&t redirection

    - &ho @sampleIn the aove command the output of the 1&ho command2 &ill e redirected to 1sample2fileError redirection:

    -&homHash; command not found

    2I2E"

    De6inition: 8ipe is an operator that temporarily saves the output of one command in

     uffer that is eing used at the same time as the input of the next command. The symol

    for pipe is (X).

  • 8/21/2019 Linux Record - Theory

    29/84

    #xample;

    - &ho X more

    In the aove command the output of &ho is the input to more command.

    Tee command

    De6inition: The tee command copies standard input to standard output and at the same

    time copies it to one or more files

    #g;

    tee sample

    This is a sample o6 the tee command

    This is a sample of the tee command

    Each line is repeated on the standard o&tp&t as /e press 0e3 enter

    #ach line is repeated on the standard output as &e press key enter 

    cat sample

    This is a sample of the tee command

    #ach line is repeated on the standard output as &e press key enter 

    #g

  • 8/21/2019 Linux Record - Theory

    30/84

    CO..AND EEC!TION:

    "eG&enced Commands; ? se,uence of commands can e entered on one line. #ach

    command must e separated y a semicolon. There is not direct relationship et&een the

    commands.

    #xample;

    date S /ho

    Wed $ept M M; CL; C< I$T

  • 8/21/2019 Linux Record - Theory

    31/84

    Chained commands: This method of comining commands is to pipe them. 6ere there

    is a direct relationship et&een the commands. The output of the first ecomes the input

    of the second .

    #xample;

    /ho Q tee 6#

    %se tty0 sept C L.3=

    %se tty0< sept C L.3<

    %se C tty0= sept C L.3M

    6ere the output of &ho command ecomes input to tee command and tee command

    copies the input to f as &ell as displays on the monitor.

    Conditional commands; We can comine t&o or more commands using conditional

    relationships. There are t&o shell logical operators" and (VV) and or (XX).

    In general " &hen t&o commands are comined &ith a logical and" the second executes

    only if the first command is successful.

    %onversely "if t&o commands are comined using the logical or" the second command

    executes only if the first fails

    #g;

    cp 6# 6' echo U"!CCE""

    $U%%#$$

    cp 6( 6) echo UFAIL

    f3 ; 4o such file or directory

    !?I+

    3

  • 8/21/2019 Linux Record - Theory

    32/84

    W!OTE":

    Jetacharacters are characters that have a special interpretation. The shell use a selected

    set of Jetacharacters in commands.

    %ollectively as ,uotes they are

    Hackslash< 9oule ̀ uotes3 $ingle ̀ uotes

    1ac0slash: The ackslash metacharacter () changes the interpretation of the character

    that follo&s E it converts literal characters into special characters and special characters

    into literal characters.

    Examples:

    #%!sin8 4ac0slash to print do&4le G&otation mar0s echo UHELLO XORLD

    6#++> W>K+9

    echo KUHELLO XORLDK

    16#++> W>K+92

    '%Displa3in8 special characters as literal text

    echo U ; K

     ash; syntax error ; @ unexpected

    echo K K KU K; K K K

    B @ 1 -

    Do&4le W&otes: doule ,uotes remove the special interpretation of most metacharacters.

    #xamples;

    xYhello

    echo U x ;3< @

    B @ hello y 7 V

    In the aove command &e use echo command to display several metacharacters .

    "in8le W&otes; $ingle ,uotes operate like doule ,uotes" ut there effect is stronger.

    They preserve only the meaning of single ,uotes. ?ny enclosed metacharacters are

    treated as literal characters

    #xamples

    3

  • 8/21/2019 Linux Record - Theory

    33/84

    #% !sin8 sin8le G&otes to chan8e meanin8 o6 special characters

    xYhello

    echo ; x U3 @ <

    B @ -x y 7 V

    '% .atchin8 sin8le G&otes

    xYhello

    echo ; ;x< U3 @ <

    B @ hello 1y2 7 V

    In the aove command the value of -x is displayed

     4>T#; $ingle ,uotes and doule ,uotes must al&ays e used in pairs.

    33

  • 8/21/2019 Linux Record - Theory

    34/84

    JO1 CONTROL

    >ne of the important features of a shell is Do control.

    ? Do is a user task run on the computer. #diting " sorting" and reading mails are all

    examples of Dos.

    There are namely t&o Dos

    !oreground Do

    < Hackground Do

    Fore8ro&nd o4: ? foreground Do is any Do run under the active supervision of the

    user. It is started y the user and may interact &ith user. While it is running no other Dos

    may e started.

    • $tart a foreground Do" &e simply enter a command and key enter%

    • $uspending a foreground Do" key ctrl S Q

    • Kestoring a foreground Do - fg

    Terminating a foreground Do - ctrl S c- ls GAZ ''''''' $uspending a foreground Do" key ctrl S Q-fg ''''''' Kestoring a foreground Do-fgAc '''''''Terminating a foreground Do

    1ac08ro&nd o4: if &e kno& a Do &ill take long time" &e may run it as ackground Do.

    os run in the ackground free the keyoard and monitor so &e may use them for other

     purposes.

    #xamples

    ls Q more  ^ starting a ackground Do

    5 MPCO=

    stop Z#  ^suspending a ackground Do

    5 S MPCO= $topped

    48 Z#  ^restarting a ackground Do

    3=

  • 8/21/2019 Linux Record - Theory

    35/84

    5 ls X more

    0ill Z#  ^terminating a ackground Do

    5 S Terminated

    3C

  • 8/21/2019 Linux Record - Theory

    36/84

    ALIA"E": ?n alias provides a mean of creating customiQed commands y assigning

    name to a command.

    In the :orn and Hash shells" an alias is created y using the alias command. Its format is

      alias name command'definition

    &here alias is the command key&ord" name is the name of the alias eing created" and

    command'definition is the code.

    Examples

    #% Renamin8 Commands: instead o6 &sin8 ls 6or list /e can &se dir or /hiche+er

    con+enient to the &ser

    alias dirYls

    dir!

    !<

    '% Alias o6 Command /ith options: here /e de6ine a director3 command /ith lon8

    list options

    alias dir Y< ls =l<

    dir

    Total T#; doule ,uotes are also valid

    ( Alias o6 m&ltiple commands: O6ten a process reG&ires more than cone command%

    As lon8 as the commands are considered as one line it can 4e assi8ned as alias

    alias dir Y Uls -l Q more

    dir

     Total

  • 8/21/2019 Linux Record - Theory

    37/84

    alias lndirY

  • 8/21/2019 Linux Record - Theory

    38/84

    VARIA1LE"

    ? variale is a location in memory &here values can e stored. #ach shell allo&s us to

    create" store" and access values in variales. #ach shell variale must have a name. The

    name of a variale must start &ith an alphaetic or underscore () character. There are

    t&o road classifications of variales ; user'defined and predefined.

    $toring 9ata in *ariales

    ?ll three shells provide a means to store value in variales.

    ?ssignment in :orn and Hash $yntax; varialevalue

     ?ssignment in % shell $yntax; set variale value

    Assi8nment in 5orn and 1ash "hells

    xY'(

    echo x

  • 8/21/2019 Linux Record - Theory

    39/84

    9irectory argument is a relative pathname

    #9IT>K #9IT>K 8athname of the command'line editor  

    #4* 8athname of the environment file

    6>J# home(6>J#) 8athname for the home directory

    8?T6 path(8?T6) $earch path for commands

    8$ prompt 8rimary prompt" such as - and b$6#++ shell($6#++) 8athname of the login shell

    T#KJ term(T#KJ) Terminal type

    TJ>UT autologout 9efines idle time in seconds" efore shell

    automatically logs you off 

    *I$U?+ *I$U?+ 8athname of the editor for command'line

    editing.

    O2TION"

    $hell options are listed elo&

    lo4al 7no8lo4$ The gloal option controls the expansion of &ildcard tokens in the

    command tokens in a command. !or example" &hen the gloal option is off" the list

    file(ls) command use &ildcards to match the files in a directory. Thus " the follo&ing

    command list all files that start &ith file follo&ed y one character 

    - ls file7

    >n the onther hand" &hen the gloal option is on" &ildcards ecome text charcters andare not expanded. In this case " only the file name file7 &ould e listed

    2rint Commands 7+er4ose and xtrace$ There are t&o print options" verose and xtrace "

    that are used to print commands efore they are executed. The verose option prints the

    command efore it is executed. The xtrace option expands the command arguments

     efore it prints the command.

    Command-Line Editor 7emacs and +i$ To specify that the emacs editor is to e used in

    :orn shell" &e turn emacs option. To specify vi editor &e turn on vi option.

     4>T#; These options are valid only in :orn shell.

    I8nore End o6 File 7i8noreeo6$  4ormally if end of file (ctrl Sd) is entered at the

    command line " the shell terminates. To disale this action" &e can turn on the ignore end

    of file option" ignoreeof. With this option end of file generates an error message rather

    than terminating the shell.

    3P

  • 8/21/2019 Linux Record - Theory

    40/84

    No Clo44er Redirection 7noclo44er$ When output or errors are directed to a file that

    already exists" the current file is deleted and replaced y a ne& file. To prevent this

    action" &e set the nocloer option

    FILTER":

    ? filter is any command that gets its input from the standard input stream" manipulates

    the input " and then sends the result to the standard output stream.

    Filters and 2ipes

    !ilters &ork naturally &ith pipes. Hecause a filter can send its output to the monitor" it

    can e used on the left of a pipe_ ecause a filter can receive its input from the keyoard"

    it can e used on the right of a pipe. In other &ords " a filter can e used on the left of a

     pipe" et&een t&o pipes" and on the right of a pipe.

    Concatenatin8 Files

    !NI  provides a po&erful utility to concatenate commands. It is kno&n as the catenate

    command " or cat for short. It comines one or more files y appending them in the order

    they are listed in the command. The input can come from the keyoard_ the output goes to

    the monitor.

    Cat has , options

    'e print - at end of line

    'n numer lines

    's silent(no error message)

    't print tas and form feeds

    'u unuffered output

    'v print control characters

    Displa3in8 1e8innin8 and End o6 Files

    U4IY provides t&o commands" head and tail" to display portion of files.

    head command

    &hile the cat command copies entire files" the head command copies a specified numer

    of lines from the eginning of one or more files to the standard output stream. If no files

    are specified" it gets the lines from standard input.

    =0

  • 8/21/2019 Linux Record - Theory

    41/84

    >ption;

    '4 numer of lines

    head -' 6#

    Unix is multitasking os

    Unix is multiuser os

    The aove command displays first < lines from the eginning of the file

    When multiple files are include in one head command" head displays the name of the file

     efore output.

    head -' 6# 6'

    @ f B

    Unix is multitasking os

    Unix is multiuser os

    @ f< B

    Unix is &idely used

    It provides est security

    tail command:

    The tail command also outputs data" only this time from end of the file. ?lthough only

    one file can e referenced it has several options

    S4 $kips 4' lines_ copies rest to end of file

    '4 %opies last 4 lines

     'l %ounts y line(default)

    'c %ounts y character  

    ' %ounts y disk lock  

    'r >utputs in reverse order(from ottom to top)

     

    tail -* sample

    The aove command displays last C lines from the end of the file

    TRAN"LATIN CHARACTER":

    tr command; it replaces each character in a user'specified set of characters &ith a

    corresponding characters in a second specified set. #ach set is specified as a string.

    "imple Translate

    =

  • 8/21/2019 Linux Record - Theory

    42/84

    Translate receives its input from standard input and &rites its output to standard output. If 

    no options are specified the text is matched against the string set and any matching

    characters are replaced &ith the corresponding characters in the string< set. Unmatched

    characters are unchanged.

    #xample;

    tr Uaeio& UAEIO!

    It is +er3 eas3 to &se TRAN"LATE% [inp&t

    It Is v#ry #?sR t> Us# TK?4$+?T#. [o&tp&t 

     4ote; uppercase vo&els in input are not changed.

    Non matchin8 Translate "trin8

    When the translate string are of different lengths" the result depends on &hich string is

    shorter. If string< is shorter the unmatched characters &ill all e changed to the last

    character in stringn the other hand " if string is shorter the extra characters in string<

    are ignored.

      tr Uaeio& UAE@ [ case # : srtin8' is shorter than strin8#

    It is +er3 eas3 to &se translate%

    It 7s v#ry #?sy t7 7s# tr?nsl?te.

    tr Uaei U AEIO! [ case ': strin8# is shorter than strin8'

    It is +er3 eas3 to &se translate%

    It Is v#rR #?sR to use translate.

    Delete Characters

    To delete matching characters in the translation &e use the delete option ('d).

    tr =d Uaeio&AEIO!

     It is +er3 eas3 to &se TRAN"LATE%

    t  s vry sy t s TK4$+T

    Complement

    The complement option reverses the meaning of the first string. Kather than specifying

    &hat characters are to e changed it says &hat characters are not to e changed

    #g;

    =

  • 8/21/2019 Linux Record - Theory

    43/84

    tr =c Uaeio& U

    It is +er3 eas3 to &se TRAN"LATE%

    FFFiFFFeFFFeaFFFFoFuFeFFFFFFFFF

    Files /ith D&plicate Lines

    The uni, command deletes duplicate lines" keeping the first and deleting the others. To e

    deleted the lines must e adDacent. 9uplicate lines that are not adDacent are not deleted. To

    delete nonadDacent lines the file must e sorted.

    $uppose let file sample contain

    Unix

    Unix

    Jefaf 

    Jefa

    Unix

    Jefa

    9ld

     4o& &niG sample

    Unix

    Jefa

    Unix

    Jefa

    9ld

    ?ove it &rites all of the nonduplicated lines and the first of a series of duplicated lines.

    This is the default result.

    Nond&plicated Lines

    The nonduplicated lines option is Eu. it suppresses the output of the duplicated lines and

    list only the uni,ue lines in the file.

    &niG =& sample

    Unix

    Jefa

    9ld

    =3

  • 8/21/2019 Linux Record - Theory

    44/84

    Onl3 D&plicated Lines7-d$

    The opposite of nonduplicated lines is to &rite only the duplicated lines. This option is E 

    d.

    &niG =d sample

    Unix

    Jefa

    Co&nt D&plicate lines 7-c$

    The count duplicate option ('c) &rites all of lines" suppressing the duplicates" &ith a

    count of the numer of duplicates at the eginning of the line

    &niG =c sample

    = Unix

    < Jefa

    Unix

    Jefa

    9ld

    ==

  • 8/21/2019 Linux Record - Theory

    45/84

    RE2:

    8rep stands for gloal regular expression print.

    It is a family of programmes that is used to search the input file for all lines that match aspecified regular expression and &rite them to the standard output file.

    The syntax for using grep command is; - grep options regularexpression filelist

    O2ERATION:

    !or each line in the standard input file(input file or keyoard)" grep performs thefollo&ing operations;

    . %opies the next input line

  • 8/21/2019 Linux Record - Theory

    46/84

    In the aove example the grep command searches for lines that contain the expressionday and prints the line. 6ere in this case there is only one line that matches &ith thegiven regular expression.

    68rep:

    It your search criteria re,uires only se,uence expressions" fgrep is the est utility.

    #xample;

    - fgrep En " f;This is the day"

  • 8/21/2019 Linux Record - Theory

    47/84

    "EARCHIN FOR FILE CONTENT

    $ome modern operating system allo&s us to search for a file ased on a phrase containedin it. ?lthough U4IY doesnt have this capaility" &e can use the grep family to

    accomplish the same thing.

    When &e kno& the directory that contains the file" &e can simply use grep y itself.

    - grep El student$tudent$tudent<$tudent

    In the aove expression" the grep command searches for all the file names that match&ith student. The option l prints out the filename of any file that has at least one line

    that matches the grep expression.

    =M

  • 8/21/2019 Linux Record - Theory

    48/84

    "ED:-

    $#9 is an acronym for stream editor. It is not a true editor" it does not change anything inthe original file.

    $#9 scans the input file line y lineand applies a list of instructions (sed script) to eachline in the input file.the script &hich is usually a separate file" can e included in the sedcommand line if it is a one'line command.

    The syntax for using the sed command is;

    - sed options script filelist

    "CRI2T"

    In addition to input data" sed also re,uires one or more instructions that provide editingcriteria. Jost of the time" ho&ever" instructions are placed in a file kno&n as sed script.#ach instruction in a sed script contains an address and a command.

    "CRI2T FOR.AT"

    When the script fits in a fe& lines" its instructions can e included in the command line.In this case" the script must e enclosed in ,uotes.

    - sed Ee adress command inputfile ( inline script )- sed Ef script.sed inputfile (script file)

    IN"TR!CTION FOR.AT"

    #ach instruction consists of an address and a command.address N command ( instruction format )

    The address selects the line to e processed ( or not processed ) y the command. Theexclamation point is an optional address compliment. When the compliment operator is present" any line that does not match the address is selected.

    CO..ENT"

    ? comment is a script line that documents or explains one or more instructions in a script.%omment lines egin &ith a comment token" &hich is the pound sign ( ^ ). If thecomment re,uires more than one line" each line must start &ith the comment token .

    =O

  • 8/21/2019 Linux Record - Theory

    49/84

    O2ERATION

    #ach line in the input file is given a line numer y sed. !or each line" sed performs thefollo&ing operations;

    . %opies an input line to the pattern space. The pattern space is a special uffer capale of holding one or more text lines for processing.

  • 8/21/2019 Linux Record - Theory

    50/84

    When a line that is in the pattern space matches a start range" it is selected for processig.#ach input line is processed y the instructions command untill the stop address matchesa line. The line that matches the stop address is also processed y the command" ut atthat point" the range is no longer active. If at some future line the start range again

    matches" the range is again active untill a stop address is formed.

    NE"TED ADDRE""E"

    ? nested address is an address that is contained &ithin another address. While the outer address range must e either a set of lines or an address range" the nested addresses may e either a single line" a set of lines" or another range.

    #xample;

  • 8/21/2019 Linux Record - Theory

    51/84

    - sed Ef insertTitle.sed student

    ^insert.sedi

    $tudent records%se

    In the aove example &e insert text at the eginning of the file. The t&o lines specifiedare inserted at the eginning of the file in the output.

    '%Append command7a$:

      ?ppend is similar to the insert command except that it &rites the text directly to theoutput after the specified line. Inserted and appended text never appear in seds patternspace.

    #xample;

    - sed Ef append+ine$ep.sed student

    ^append+ine$ep.seda''''''''''''''''''''''''''-a

    ? cse P''''''''''''''''''''''''''H cse 0M''''''''''''''''''''''''''% cse 0''''''''''''''''''''''''''

    In the aove example &e are appending the line separator to every line.

    (%Chan8e command7c$:

      %hange replaces a matched lin &ith the ne& text. It accepts all four address types.

    #xample;

    - sed Ef change.sed student

    ^change.sed

  • 8/21/2019 Linux Record - Theory

    52/84

    ? cse P9 cse 0M% cse 0

    In the aove example &e are changing the second line in the input file &ith the text givenin the script file.

    )%Delete pattern space7d$:

    When a lo&er case script command is used" it deletes the entire pattern space.

    #xample;

    - sed GA>Gd student

    The aove command deletes the lines in the input file starting &ith the capital letter >.

    *%DELETE ONL FIR"T LINE CO..AND7D$

    When an upper case delete command is used" only the first line of the pattern space isdeleted.

    "!1"TIT!TE CO..AND7s$:-

    $ustitute replaces text that is selected y a regular expression &ith a replacementstring.the format of the sustitute command is as

    ?ddress s GpatternG replacement$tring G flags

    When a text line is selected" its text is matched to the pattern. If matching text is found" itis replaced y the replacement string. The pattern and replacement string are separated ya triplet of identical delimiters.

    2attern matches address;In this case &e dont need to repeat the regular expression in the sustitute command.#xample;- sed G?GsGGaG studenta cse PH cse 0M% cse 0

    In the aove example &e are replacing the student name ? &ith a.

    Replace strin8

    The replacement text is a string. >nly one atom and t&o metacharacters can e used inthe relpacement string.

    C

  • 8/21/2019 Linux Record - Theory

    53/84

    #xample;- sed sGunixGFFF V FFFG file

    $ustitute >peration

    We can use the sustitute command to add" delete"or replace part of a line.When the pattern matches the text" sed first deletes the text and then inserts thereplacement text.

    9elete 8art >f ? +ine;To delete part of a line" &e leave the replacement text empty.

    #xample;- sed sG0'P5GGgInput;

  • 8/21/2019 Linux Record - Theory

    54/84

    ^add8art.sed-GAG G-G-G''G

    Input; 4o& is the time!or all good studentsTo come to the aid>f their college.

    >utput; 4o& is the time''!or all the students''To come to the aid''>f their college.''

    TRAN"FOR. CO..AND

    Transform command re,uires t&o parallel set of characters. #ach character in the firststring represents a value to e changed to its corresponding character in the second string.

    ?ddress y G source characters G replacement characters G

    %haracters that do not match a source character are left unchanged.

    #xample;- sed yGaeiouG?#I>UGInput;? good time &as had y allUnder the 6arest Joon last $eptemer.

    >utput;? g>>d tIme &?s h?d y ?llUnd#r th# 6arv#st J>>n l?st $ept#m#r.

    This session demonstrates the transform command y translating lo&ercase vo&els totheir upper case forms.

    IN2!T AND O!T2!T CO..AND"

    The sed utility automatically reads text from the input file and &rites data to the outputfile" usually standard output.There are five input output commands.

    C=

  • 8/21/2019 Linux Record - Theory

    55/84

    #%Next Command7n$

    The next command (n) forces sed to read the next line from the input file. Hefore readingthe next line" ho&ever" it copies the current contents of the pattern space to the putput"deletes the current text in the pattern space" and then refills it &ith the next input line.

    ?fter reading the input line it cintinues processing through the script.

    #xample;

    - sed Ef deleteHlank+ines.sed file

    ^deleteHlank+ines.sedGA0'P5G

      n  GA-Gd 

    Input;

    $econd +ine; +ine V line 3 lank 

    =th line' follo&ed y non lank lineThis is line CLth line follo&ed y lank line

    +ast line (^O)

    >utput;

    $econd +ine; +ine M line 3 lank 

    =th line'follo&ed y non lank lineThis is line CLth line follo&ed y lank line+ast line (^O)

    Whenever a line that starts &ith a digit is immediately follo&ed y a lank line" &e deletethe lank line. We first locate a line that egins &ith a digit. To see if the next line is lank" &e first read it using the next command. This command forces the current line to e &ritten. We then check the ne& line. If it is lank" &e delete it.

    '%Append Next Command7N$:

    The append next command doesnt clear the pattern space efore inputting the next line.It adds the next input line to the current contents of the pattern space.

    CC

  • 8/21/2019 Linux Record - Theory

    56/84

    #xample;

    - sed Ef append+ines.sed file

    ^append+ines.sed

     4sGnG G

    Input;first

  • 8/21/2019 Linux Record - Theory

    57/84

    ======================CCCClastCCCCCCCClastCCCC

    The real use of this command is to let the script control the printing.

    )%2rint First Line Command72$:

    The print first line command prints only the first line. It prints the contents of the patternspace up to and including a ne&line character. ?ny text follo&ing the first ne&line is not printed.

    #xample;

    - sed Enf print!irst+Ine.sed file

    ^print!irst+ine.sed-N4Gn G89

    Input;This is line .This is line utput;This is line

  • 8/21/2019 Linux Record - Theory

    58/84

    Input;>ctal 0; X X>ctal P; X X>ctal utput;>ctal 0; X X>ctal P; XtX>ctal

  • 8/21/2019 Linux Record - Theory

    59/84

    1RANCH CO..AND"

    The ranch commands change the regular flo& of the commands in the script file. The ranch commands allo& us to skip one or more commands in the script file. There aret&o ranch commands.#ach ranch command must have a target" &hich is either a lael or the last instruction inthe script (a lannk lael). ? lael consists of a line that egins &ith a colon (;) and isfollo&ed upto seven characters that constitute the lael name.

    #xample; ;com6ere

    Hranch %ommand();The ranch command follo&s the normal instruction format consisting of an address" the

    command" and an attriute that can e used to ranch to the end of the script or to aspecific location &ithin the script. If no lael is provided" the ranch is to the end of thescript" at &hich point the current contents of the pattern space are copied to the output fileand the script is repeated for the next input file.

    #xample;

    - sed Ef ranch.sed inputfile

    ^ranch.sedG()G G(

  • 8/21/2019 Linux Record - Theory

    60/84

    (

  • 8/21/2019 Linux Record - Theory

    61/84

     print thrice. print thrice.9efault; print once.

    If a sustitution &as made the ranch is taken. It the sustitution pattern &as not found"

    the ranch is ignored. The print controls have een removed except for the last one"&hich is a user input error.

    HOLD "2ACE CO..AND

    It is used tio save the pattern space. There are five commands that are used to move text ack and forth et&een the pattern space and the hold space..6old ?nd 9estroy %ommand(h);The hold and destroy command copies the current contrents of the pattern space to thehold space and destroys any text currently in the hold space.

  • 8/21/2019 Linux Record - Theory

    62/84

    +ine <+ine 3+ine =+ine C+ine L

    >utput;+ine <+ine +ine =+ine 3+ine L+ine C

    It does not &ork &ith an odd numer of lines. In that case" the last line is printed t&ice.The easiest solution is to run a sed script to delete the last line.

    RE2 AND "ED

    To demonstrate ho& &e can use sed in place of grep" &e look at t&o general situations.

    .+ines that match a regular expressionIf &e need to use sed instead of grep to find a line that matches a regular expression" &euse the print(p) command in sed and turn off the automatic output option ('n).

    The t&o commands are as follo&s.

    grep regular expression filesed 'n Gregular expressionGp file

  • 8/21/2019 Linux Record - Theory

    63/84

    AX5 command:

    The a/0  utility" &hich takes its name from the initials of its authors (?lfred *.?ho"8eter

    . Weinerger" and Hrian W. :ernighan)" is a po&erful language. Its ehavior is to someextent like sed. It reads the input file line y line and performs an action on part of or onthe entire line.a&k utility has t&o options'!; specifies the input field separator'f; names the script filea/0  re,uires one or more instructions that provide editing instructions. When they areonly fe& instructions they can e entered at the command line from the keyoard.6o&ever if there are many lines it is placed in a file kno&n as an a&k script. #achinstruction in an a&k script contains a pattern and action.The format for command'line script is;

    a/0 ;pattern\action]< inp&t-6ileFIELD" AND RECORD":

    The a&k utility vie&s a file as a collection of fields and records. #ach line in a&k is arecord.Field : ? field is a unit of data that has informational content.Record : ? record is a collection of fields treated as unit.$uppose consider a file student - cat student ohn

  • 8/21/2019 Linux Record - Theory

    64/84

    "3stem Varia4les

    *ariale !unction

    !$ Input field separator  

    K$ Input record separator  

    >!$ >utput field separator  

    >K$ >utput record separator   4! 4umer of nonempty fields in current record

     4K 4umer of records read from all files

    !4K 4umer of records read Erecord numer in current!ile

    !I+#4?J# 4ame of the current file

    ?K% 4umer of command'line arguments

    ?K* %ommand'line argument array

    K+#4T6 +ength of string matched y a uilt'in string function

    K$T?KT $tart of string matched y a uilt'in string function

    These are the t&elve system variales used y a&k.!ser-De6ined +aria4les

    Within an a&k script &e can define variales. They can e numers" strings " or arrays.*ariale names start &ith a letter and can e follo&ed y se,uence of letters" digits" andunderscores etc

    L=

  • 8/21/2019 Linux Record - Theory

    65/84

    "CRI2T":

    a&k scripts are divided into three parts; egin" ody and endH#I4 Hegins ?ctions H#I4 part

      8attern action  8attern action H>9R  8attern action#49 #nds ?ctions #49 part"imple a/0 example

    - cat student ohn

  • 8/21/2019 Linux Record - Theory

    66/84

    "imple 2atterns :? simple pattern matches one record. When a pattern matches a recordthe result is true and actions statement is executed.H#I4 ?49 #49 ; H#I4 is true at the eginning of the file efore the first record isread. It is used to initialiQe the script efore passing any data.#xample;

    It sets the field separator here &e set output field separator (>!$) to ta space.H#I4

      !$ 1t2  >!$ 1t2  #49 #49 is used at the conclusion of the script.

    Expressions:The a&k utility supports four expressions; regular" arithmetic" relational" and logical.Re8&lar expressions; The a&k regular expressions are those defined in egrep. ?lso a&k

    re,uires t&o operators; match (/) and not match (N/). 4>T#; When using a regular expression it must e enclosed in GslashesG.#xample; cat sample

    UnixJ#!?9+98$Unix is a multiuser operating system..Write an a&k command that prints the lines &hich egins &ith U a/0 ; ^PB!P \print]< sample

    UnixUnix is a multiuser operating system.In the aove command the regular expression is -0 /GAUG . it displays the records &hich egin &ith U.

  • 8/21/2019 Linux Record - Theory

    67/84

    ohn

  • 8/21/2019 Linux Record - Theory

    68/84

    Nexpr 

    expr VV expr<

    expr XX expr<

     4ot expression

    #xpression and expression <

    #xpression or expression <

    Kesult of logical ?49 is true if and only if oth expressions are true_ it is false if either of the expression is false.Kesult of logical >K is true if either of the expression is true_ it is false if and only if othexpressions are false.The 4>T operator complements!or the same student file*% Xrite an a/0 command that prints details o6 the st&dents /ho ha+e less than #*mar0s in 4oth the s&4ects

    - a&k -3 B C VV -= BC print student3 aco O P

    C 9avid L

  • 8/21/2019 Linux Record - Theory

    69/84

    ACTION":

    ?ctions are instructions or statements. They are called actions in a&k ecause they act&hen the pattern is true.In a&k an action is one or more statement associated &ith a pattern. There is a one'to'onerelationship et&een n action and pattern. >ne action is associated &ith only one pattern.

    The action statements must e enclosed in a set of races. ? set of races containing patternGaction pairs or statements is kno&n as lock. When an action consists of severalstatements they must e separated y a statement separator such as semicolon" a ne&line"or a set of races.

    Hasic a/0 statements

    +et us consider fe& a&k examples &hich illustrate the aove.2rint; print &rites specified data to the standard output file. #ach print action &rites aseparate line. When multiple fields or variales are eing &ritten they must e separated&ith commas. If no data is specified the entire record is printed.#xample;%Xrite an a/0 command that prints st&dent n&m4er and st&dent name onl3

    a/0 ;\print # ' ]< st&dent

    ohn< ames3 aco= $amuelC 9avidThe aove command prints only first t&o fields of the input file student.Decision statements: The asic decision statement is if else.%onsider an employee file- cat emp

    LP

    $tatements

    #xpression >utput

    8rint

    8rintf 

    $printf 

    9ecision +oop

    &hile

    for 

    do'&hile

    %ontrol

    next

    getline

    exit

  • 8/21/2019 Linux Record - Theory

    70/84

    ? %$# 0000< H %$# L0003 % #%# C000= 9 #%#

  • 8/21/2019 Linux Record - Theory

    71/84

    O&tp&t:

    a/0 =6 total%a/0 st&dent

    $no $name su su< Total ohn

  • 8/21/2019 Linux Record - Theory

    72/84

    3 aco O P M= $amuel P 0

  • 8/21/2019 Linux Record - Theory

    73/84

    "TRIN F!NCTION"

    The a&k utility contains several string functions. These are the uilt'in functions.#%Len8th

    The length function has the formatlen8th 7strin8$+ength returns the numer of characters in the string parameters.#xample;%onsider sample file cat sample

    UnixJefa9ld8sUnix is a multiuser operating system.

    #%Xrite an a/0 script to ill&strate len8th 6&nction@   print len length (-0) " 1t2 -0>utput; a/0 =6 len%a/0 sample

    = Unix= Jefa3 9ld< 8s3L Unix is a multiuser operating system.'%Index

    The index function returns the first position of a sustring &ithin a string. Its format is  Index 7strin8 s&4strin8$

    The sustring can e string constant or variale . if the sustring is not found it returnsQero.#xample;'%Xrite an a/0 script to ill&strate index 6&nction@

    loc index(-0"2cse2) print 1cse is found at 1 loc 1position2 a/0 =6 index%a/0 

    Jrec csecse is found at L positionctrl SdIn the aove command input file is not given it &aits for user to give input

    (%"&4strin8

    The sustr function extracts a sustring from a string. It has t&o formats;

    M3

  • 8/21/2019 Linux Record - Theory

    74/84

      sustr (string" position)  sustr (string" position" length)The only difference et&een the t&o functions is the length of the data to e retuned.Hoth return the sustring from string starting at position. If a length is specified it returnsup to length characters. If no length is specified it returns everything to the end of the

    string.#xample;

    (%Xrite an a/0 command to ill&strate s&4strin8 6&nction@

    - a&k sustr (-0 " L)ood morningJorning%trl Sd

    .ATHE.ATICAL F!NCTION":

    These are the fe& a&k mathematical functions!U4%TI>4 %>JJ#4T$

    Intrand()srand(seed)cos (x)exp (x)log (x)sin (x)s,rt (x)atang< (y" x)

    Truncates floating'point value to integer Keturns next random numer in series_ range 0$eeds random numer series. $eed should e a prime numer.Keturns cosineKeturns eAxKeturns natural logarithm of xKeturns sine of xKeturns s,uare of xKeturns arc tangent of yGx in range Epi to pi

    M=

  • 8/21/2019 Linux Record - Theory

    75/84

    "HELL 2RORA.":

    Aim:/rite a shell pro8ram to per6orm arithmetic operations.

    8rogram;'echo enter t&o valuesread aread

    c[expr -a S -[echo aS-c

    c[expr -a ' -[echo a'-c

    c[expr -a F -[echo aF-c

    c[expr -a G -[echo aG-c

    c[expr -a b -[echo ab-c

    O!T2!T:-

    -sh filenameenter t&o numers

  • 8/21/2019 Linux Record - Theory

    76/84

    Aim:-Find /hether the 8i+en n&m4er is e+en or odd n&m4er%

    8rogram;'

    echo enter any numerread nr[expr -n b

  • 8/21/2019 Linux Record - Theory

    77/84

    Aim:-Find smallest n&m4er amon8 three n&m4ers@

    Aim:-/rite a pro8ram to read mar0s o6 a st&dent in ( s&4ects and print totala+era8e and di+ision

    Aim:-Xrite a pro8ram to chec0 /hether the character is lo/er or &pper or di8it or

    special character%

    2ro8ram:'echo enter any character read chcase -ch in

    ?'Z5) echo It is in upper case__

    a'Q5) echo it is in lo&er case__

    0'P5) echo it is a digit__

    7) echo it is a special symol__

    F) echo you entered more than one character__esac

    O!T2!T:-

    -sh filenameenter any character 6It is in upper case

    MM

  • 8/21/2019 Linux Record - Theory

    78/84

    Aim:-Find 6actorial o6 a 8i+en n&m4er 43 &sin8 XHILE loop

    2ro8ram:'

    echo enter any numer

    read nfacti&hile -i Ele -n 5dofact[expr -fact F -i[i[expr -i S [doneecho the factorial of given numer-fact

    O!T2!T:-

    -sh filenameenter any numer Cthe factorial of given numer

  • 8/21/2019 Linux Record - Theory

    79/84

    Aim:To displa3 prime n&m4ers 4et/een and

    2ro8ram:'echo #nter Y and R valuesread Y

    read Recho prime numers et&een -Y and -R&hile -Y 'le -R 5doac0&hile -a 'le -Y 5dor[expr -Y b -a[if -r 'e, 0 5then

    c[expr -c S [fia[expr -a S [doneif -c 'e, < 5thenecho -YfiY[expr -Y S [done

    O!T2!T:-

    -sh filename#nter Y and R values3C prime numers et&een 3 and C3CM

    MP

  • 8/21/2019 Linux Record - Theory

    80/84

    Aim:-Xrite a men& dri+en pro8ram /hich has 6ollo/in8 options ;#%List o6 6ile and directories in a c&rrent director3

    '%List o6 &sers /ho ha+e c&rrentl3 lo88ed in%

    (%2resent /or0in8 director3%)%Exit%%etc

    2ro8ram:'echo 1.+ist of file and directories in a current directory2echo 1

  • 8/21/2019 Linux Record - Theory

    81/84

    Aim: Xrite a shell script /hich deletes all lines containin8 the /ord ;!NI< in the6iles s&pplied as ar8&ments to this shell script%

    8rogram;'

    if -^ 'e, 0 5thenecho enter only one file nameexitfi

    if 'f - 5then

    cat - X grep 1U4IY2 @ fileif 's file 5thencat - X grep 1U4IY2 'v @ filemv file -elseecho 1The file does not contain &ord U4IY2fi

    elseecho 1Invalid !ilname2fi

    O!T2!T:

    -cat sampleThis is U4IY recardThis is sample file

    sh prograname sample

    -cat sampleThis is sample file

    O

  • 8/21/2019 Linux Record - Theory

    82/84

    FILE .ANAE.ENT:

    ""TE. CALL" FOR FILE .ANAE.ENT:

    FILE IPO:- Jost Unix file IG> can e performed using only five functions; open" read"&rite" lseek" and close.!ile 9iscritors;' To the kernel all open files are referred to y file descriptors .? filedescriptor is a non'negative integer. When &e open an existing file or create a ne& file"the kernel returns a file descriptors to the process.

    Open 6&nction:- This function is used to open existing file or to create a ne& file^include BsysGtypes.h@^includeBsysGstat.h@^includeBfcntl.h@

    int open(const char pathname" int oflag" GF " modet mode FG )_ 

    Keturns; file descriptor if >:" ' on error 

    In the open function the pathname is the name of the file to open or create. There are amultitude of options for this function"&hich are specified y the oflag argument.>flag arguments;'ORDONL Open 6or readin8 onl3

    OXRONL Open 6or /rite onl3

    ORDXR Open 6or readin8 and /ritin8

    >ne and only one of these three constants must e specified. The follo&ing constants areoptional;OA22END:- Xhich is &sed to append text to the end o6 the 6ile%

    OCREAT:- Xhich is &sed to create the 6ile i6 it doesn

  • 8/21/2019 Linux Record - Theory

    83/84

    $IW>T6 >ther&rite

    $IY>T6 >therexecute

    The nine file access permission its"from BsysGstat.h@

    Create 6&nction;'&hich is used to create a ne& file.^include BsysGtypes.h@^includeBsysGstat.h@^includeBfcntl.h@int creat (const char Fpathname" modet mode)_ 

    Keturns; !ile descriptor opened for &riteonly if >:" ' on error 

     4ote that this function is e,uivalent to>pen(pathname" >WK>4+R X >%K#?T" mode)_

    Close F&nction:- It is used to close file &hich is already opened.^include Bunistd..h@int close(int filedes)_  Keturns ; 0 if >:" ' on error 

    Read 6&nction:- It is used to read data from an open file^include Bunistd.h@ssiQet read(int filedes" void Fuff" siQet nytes)_

    If the read is successful" the numer of ytes read is returned. If the end of file isencountered" 0 is returned.It returns E on error.Write !unction;'9ata is &ritten to an open file &ith the &rite function.^includeBunistd.h@ssiQet &rite(int filede" const void Fuff" siQet nytes)_

    If the &rite is successful" it returns numer of ytes &ritten other&ise E on error.

    Lsee0 6&nction:- It is used to set the cursor at a particular position in a file #very openfile has an associated 1current file offset2. This is a nonnegative integer that measures the

    numer of ytes from the eginning of the file.^includeBsysGtypes.h@^includeBunistd.h@

    offt lseek (int filedes" offt offset" int &hence)_  Keturns; 4e& file offset if >:" ' on error 

    O3

  • 8/21/2019 Linux Record - Theory

    84/84

    The interpretation of the offset depends on the value of the &hence argumentIf &hence $##:$#T" the files offset is set to offset ytes from the eginning of the fileI! &hence is $##:%UK" then the files offset its current value S the offset. The offsetcan e positive or negative.If &hence is $##:#49" the files offset The siQe of the file S the offset

    Aim: /rite a pro8ram to 8i+e demo on lsee0 and creat 6&nctions

    ^include BsysGtypes.h@^includeBsysGstat.h@^includeBfcntl.h@^includeBunistd.h@char uf5 1>$#862

    char uf