ݺߣ

ݺߣShare a Scribd company logo
LinuxFEST 7.3.2009




               GNU/Linux
                      Presmerovanie I/O



Autor: Michal Sedlk
Email: michal6103 [at] gmail [dot] com
Prezentcia je zalo?en na


                           The LBook:
             An Introductory Guide to Linux Basics
            http://linuxbasics.org/course/book/index

           Vizulna tma prezentcie je odvoden
                      tmy vytvorenej:
               Sakari Koivunen and Henrik Omma
                Released under the LGPL license.



7.3.2009                                               2/40
Obsah

   
       Jednoduch presmerovania
       C   ?tandardn? vstup a v?stup
       C   Opertory
   
       Pokro?il presmerovania
            C   File deskriptor
   
       Filtre



7.3.2009                               3/40
Presmerovanie

   
       Zachytenie v?stupu
           C   Sboru
           C   Prkazu
           C   Programu
           C   Skriptu
           C   Bloku kdu v skripte
   
       Poslanie na vstup
           C   Sboru, prkazu, programu, alebo skriptu

7.3.2009                                              4/40
Input, Output, Error



    Program
           C   Standard input
                  
                      Klvesnica
           C   Standrad output
                  
                      Obrazovka
                  
                      Tla?iare
                  
                      Sbor
           C   Standard error

7.3.2009                           5/40
Opertory > a |

   
        prikaz > subor
           C   Presmeruje stdout do sboru
           C   Ak sbor neexistuje, tak bude vytvoren?
           C   Ak sbor existuje, bude prepsan?

    
        prikaz1 | prikaz2
           C   Presmeruje stdout prkazu jedna do stdin
                prkazu dva


7.3.2009                                                  6/40
Prklad >

   $?cat?>?test1
   Toto?sa?zapise?do?suboru

   $?cat?>?test2
   Toto?sa?zapise?do?ineho?suboru

   $?cat?test1
   Toto?sa?zapise?do?suboru

   $?cat?test2
   Toto?sa?zapise?do?ineho?suboru


7.3.2009                            7/40
Prklad >

#zl? enie?sborov?test1?a?test2?
$?cat?test1?test2?>?test3

$?cat?test3
Toto?sa?zapise?do?suboru
Toto?sa?zapise?do?suboru?test2




7.3.2009                            8/40
Prepsanie sborov >|
   
       Noclobber C bash ochrana (.bashrc)
$?set??o?noclobber

?cat?>?test3
bash:?test3:?ned?sa?prepsa? ?existujci?sbor

$?cat?>|?test3
prepisal?som?subor?test2????????

$?cat?test3
prepisal?som?subor?test2

$?set?+o?noclobber
7.3.2009                                      9/40
Vytvorenie sboru

$?ls??la
celkom?12
drwxr?xr?x???2?michal?michal?4096?2009?03?06?18:17?.
drwxr?xr?x?161?michal?michal?8192?2009?03?06?18:17?..

$?>?prazdny?subor

$?ls??la
celkom?12
drwxr?xr?x???2?michal?michal?4096?2009?03?06?18:17?.
drwxr?xr?x?161?michal?michal?8192?2009?03?06?18:17?..
?rw?r??r?????1?michal?michal????0?2009?03?06?18:17?prazdny?subor




7.3.2009                                                   10/40
Sie?

   
       /dev/tcp/host/port
   
       /dev/udp/host/port
   
       Vypnut v Debiane:
           C   nepredvdate?n d?sledky
           C   Netcat
           C   /usr/share/doc/bash/README.Debian.gz




7.3.2009                                              11/40
Rra |

    
        Presmerovanie v?stup jednho programu
        do druhho
    
        grep retazec1 subor | grep -v retazec2
    
        ls -la | less
    
        cat script | mysql




7.3.2009                                         12/40
Rra |



  $?ls?/dev?|grep?sda
  sda
  sda1
  sda2
  sda3
  sda5




7.3.2009                13/40
Prklad <
   
       Presmerovanie obsahu sboru do
       ?tandardnho vstupu programu
$?ls?Obrzky/
Avatar??Experimenty??Webcam

$?ls?Obrzky/?>?dir.txt

$?cat?dir.txt?|?sort??r
Webcam
Experimenty
Avatar

$?sort??r?<?dir.txt
Webcam
Experimenty
Avatar
7.3.2009                                14/40
Kombinovanie opertorov
    
        Opertory sa daj kombinova?
$?sort??r?<?dir.txt?>?reversed.txt
$?cat?reversed.txt?
Webcam
Experimenty
Avatar

$?less???help?|?grep??i?examine?>?examine.txt
$?cat?examine.txt?
??:e?[file]?????????Examine?a?new?file.
??:n?????????????*??Examine?the?(N?th)?next?file
??:p?????????????*??Examine?the?(N?th)?previous?file
??:x?????????????*??Examine?the?first?(or?N?th)?file
??+cmd??????????????Execute?the?less?cmd?each?time?a?new?file

7.3.2009                                                  15/40
Kombinovanie opertorov

   
       ?ubovo?n? po?et |
   
       Niektor programy nepou?vaj stdin a
       stdout (?tajte man strnky)




7.3.2009                                       16/40
Opertor >>

   
       Pripojenie stdout na koniec sboru
   
       Ak sbor neexistuje, tak vytvor sbor
   
       Ak sbor existuje, tak ho roz?ri o v?stup
       spustenho programu




7.3.2009                                        17/40
Prklad >>

  $?date?>>?datum.txt
  $?date?>>?datum.txt
  $?date?>>?datum.txt
  $?date?>>?datum.txt
  $?date?>>?datum.txt
  $?cat?datum.txt?
  Pi?mar??6?20:59:46?CET?2009
  Pi?mar??6?20:59:48?CET?2009
  Pi?mar??6?20:59:49?CET?2009
  Pi?mar??6?20:59:50?CET?2009
  Pi?mar??6?20:59:51?CET?2009

7.3.2009                        18/40
File deskriptor

   
       Pridelen? ka?dmu otvorenmu sboru
   
       ?slo pridelen OS
   
       Identifiktor sl?iaci na poskytnutie
       prstupu
   
       Pre pou?vate?a 0-9
   
       exec (3 -Ubuntu, 5-LDP)



7.3.2009                                       19/40
/dev/fd

 $?ls??la?/dev/fd
 /dev/fd??>?/proc/self/fd

  $??ls??la?/dev/fd/        $??ls??la?/dev/fd/

  0??>?/dev/pts/1           0??>?/dev/pts/0
  1??>?/dev/pts/1           1??>?/dev/pts/0
  2??>?/dev/pts/1           2??>?/dev/pts/0
  3??>?/proc/7381/fd        3??>?/proc/7381/fd



7.3.2009                                     20/40
0,1,2 File deskriptor

   
       3 typy I/O
           C   standard input:    FD 0
           C   standard output:   FD 1
           C   standard error:    FD 2
   
       ?sla s s identifiktory
           C   > je ekvivalent k 0>
           C   < je ekvivalent k <1



7.3.2009                                 21/40
Prklad 1> a 2>
$?mkdir?testdir
$?cd?testdir
$?>?file1
$?ls
File1

$?ls?file1?file2?
ls:?nie?je?mo?n?pristupova? ?k?file2:?No?such?file?or?directory?File1


$?ls?file1?file2?1>?dirlist?2>?errorlist

$?cat?dirlist
File1


$?cat?errorlist
ls:?nie?je?mo?n?pristupova? ?k?file2:?No?such?file?or?directory

7.3.2009                                                           22/40
>N vs. >&N

   
       >N
            C   Presmerovanie do suboru N
   
       >&N
            C   Presmerovanie na FD N




7.3.2009                                    23/40
stderr a stdout v jednom
 sbore
$?ls?file1?file2?>?dirlist?2>&1
$?cat?dirlist
ls:?nie?je?mo?n?pristupova? ?k?file2:?No?such?file?
or?directory
file1

$?ls?file1?file2?&>?dirlist
$?cat?dirlist
ls:?nie?je?mo?n?pristupova? ?k?file2:?No?such?file?
or?directory
file1
 $?ls?file1?file2?2>&1?>?dirlist
 ls:?nie?je?mo?n?pristupova? ?k?file2:?No?such?file?
 or?directory
 $?cat?dirlist?
 file1
7.3.2009                                           24/40
Prezeranie ch?b

   
       Ak program generuje ve?a ch?b
      command 2>&1 | less




7.3.2009                               25/40
Bash 4 (23. 2. 2009)




  &>>      ekvivalent   cmd >>subor 2>&1
  |&       ekvivalent   cmd 2>&1 | cmd




7.3.2009                                   26/40
Presmerovanie ch?b

  
      Presmerovanie ch?b do inho terminlu
$?tty
/dev/pts/1

$?make?2>?/dev/pts/0

$?tty
/dev/pts/0

 $?make:?***?No?targets?specified?and?no?
 makefile?found.??Stop.
 make:?***?No?targets?specified?and?no?makefile?
 found.??Stop.
7.3.2009                                      27/40
Zapisovanie do sborov a
 zrove na obrazovku
$?ls?file1?file2?|?tee?dirlist?
ls:?nie?je?mo?n?pristupova? ?k?file2:?No?such?file?
or?directory
file1

$?cat?dirlist?
file1
$?ls?file1?file2?2>&1?|?tee?dirlist?
ls:?nie?je?mo?n?pristupova? ?k?file2:?No?such?file?
or?directory
file1

 $?cat?dirlist?
 ls:?nie?je?mo?n?pristupova? ?k?file2:?No?such?file?
 or?directory
 file1
7.3.2009                                           28/40
Vypisovanie ch?b v
 skriptoch
  $?echo?Chybova?hlaska?>&2
  Chybova?hlaska

  case?"$1"?in
  ??start)
     #?No?op
     ;;
  ??restart|reload|force?reload)
     echo?"Error:?argument?'$1'?not?supported"?>&2
     exit?3
     ;;
  ??stop)
     do_stop
     ;;
  ??*)
     echo?"Usage:?$0?start|stop"?>&2
     exit?3
     ;;

7.3.2009                                             29/40
Vytvorenie FD N<>
 Zatvorenie FD n<&-, n>&-
$?echo?1234567890?>?File
$?exec?3<>?File
$?read??n?4?<&3
$?echo??n?.?>&3
$?exec?3>&?
$?cat?File
1234.67890




7.3.2009                    30/40
Prklad <<a <<-
 Here documents

   #!/bin/bash
   interactive-program <<-LimitString
     command #1
     command #2
     ...
   LimitString




7.3.2009                                31/40
Trval presmerovanie
 exec

   
       exec - vytvorenie novho procesu,
       nahradenm starho
   
       exec ovplyvn presmerovanie, pre cel?
       skript, alebo shell (PID)
   
       exec >filename (cely shell, skript)
   
       > filename (len pre novo forknut
       procesy)


7.3.2009                                       32/40
Prklad na exec

#!/bin/bash
exec?6<&0??????????#?zapametame?si?stdin
exec?<?data?file???#?nahradime?stdin?s?"data?file"
read?a1????????????#?nacitame?prvy?riadok?z?"data?file".
read?a2????????????#?nacitame?druhy?riadok?"data?file."

echo?$a1
echo?$a2
echo;?echo;?echo

exec?0<&6?6<&??????#?vratime?hodnotu?stdin?a?zmazeme?fd6

echo??n?"Enter?data??"
read?b1????????????#?citanie?z?klavesnice.
echo?"b1?=?$b1"
exit?0
7.3.2009                                             33/40
Filtrovanie v?stupu

   
       grep
   
       cut
   
       sort
   
       sed




7.3.2009               34/40
grep

    
        Vyh?adva v sboroch, alebo zo stdin

$?grep?users?/etc/group
users:x:100:dusko,michal,marticka
vboxusers:x:1001:michal

michal@axserver:~$?ps??ef?|?grep?apache
root??????2445?????1??0?Feb28???????/usr/sbin/apache2??k?start
michal????2905??2891??0?11:57?pts/0?grep?apache
www?data?21638??2445??0?Mar01???????/usr/sbin/apache2??k?start
www?data?25639??2445??0?Mar02???????/usr/sbin/apache2??k?start

michal@axserver:~$?ps??ef?|?grep?apache?|?wc
??????4??????39?????285


7.3.2009                                                         35/40
cut
$?ls??la?|?cut??c?35?50

2009?03?07?12:08
2009?03?07?10:39
2009?03?07?12:06
2009?03?07?12:06



$?ls??la?|?cut???d"?"??f?12,13
12:06?a
12:06?b
12:06?c
12:08?d
12:08?e
12:08?f
12:08?g




7.3.2009                         36/40
sort

   
       Zoradenie pod?a    michal@doma:~/testdir$?ls?|?
                          sort??r
       r?znych kriteri   g
                          f
                          e
   
       -r reverse         d
                          c
   
       -R random          b
                          a
                          michal@doma:~/testdir$?ls?|?
   
       -u iba uniktne    sort??R
                          g
                          e
                          b
                          d
                          c
                          f
                          a


7.3.2009                                             37/40
sort

$?ls??la?|?cut??c?35?50

2009?03?07?12:08
2009?03?07?10:39
2009?03?07?12:06
2009?03?07?12:06
2009?03?07?12:06
2009?03?07?12:08
2009?03?07?12:08
2009?03?07?12:08
2009?03?07?12:08


$?ls??la?|?cut??c?35?50?|?sort??u

2009?03?07?10:39
2009?03?07?12:06
2009?03?07?12:08


7.3.2009                            38/40
sed

   
       Stream editor
   
       Regulrne v?razy
$?echo?Windows?je?dominantny?OS?a?Balmer?je?stastny?|?sed?'
s/Windows/Linux/
s/stastny/nestastny/'
Linux?je?dominantny?OS?a?Balmer?je?nestastny




7.3.2009                                                  39/40
Zhrnutie

   
       Presmerovanie
   
       Opertory >, >>, &>, &>>, |&, |
   
       File deskriptory >&, <>, <&-, >&-
   
       exec
   
       Filtre grep, cut, sort, sed




7.3.2009                                   40/40
?akujem za pozornos?
           Na prpadn otzky rd odpoviem

           michal6103 [at] gmail [dot] com


7.3.2009                                     41/40
LinuxFest podporuj
Referencie




http://tldp.org/LDP/abs/html/io-redirection.html

More Related Content

Linux: Presmerovanie I/O