SlideShare uma empresa Scribd logo
1 de 43
Baixar para ler offline
LinuxFEST 7.3.2009




               GNU/Linux
                      Presmerovanie I/O



Autor: Michal Sedlák
Email: michal6103 [at] gmail [dot] com
Prezentácia je založená na


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

           Vizuálna téma prezentácie je odvodená
                      témy vytvorenej:
               Sakari Koivunen and Henrik Omma
                Released under the LGPL license.



7.3.2009                                               2/40
Obsah

   ●
       Jednoduché presmerovania
       –   Štandardný vstup a výstup
       –   Operátory
   ●
       Pokročilé presmerovania
            –   File deskriptor
   ●
       Filtre



7.3.2009                               3/40
Presmerovanie

   ●
       Zachytenie výstupu
           –   Súboru
           –   Príkazu
           –   Programu
           –   Skriptu
           –   Bloku kódu v skripte
   ●
       Poslanie na vstup
           –   Súboru, príkazu, programu, alebo skriptu

7.3.2009                                              4/40
Input, Output, Error


●
    Program
           –   Standard input
                  ●
                      Klávesnica
           –   Standrad output
                  ●
                      Obrazovka
                  ●
                      Tlačiareň
                  ●
                      Súbor
           –   Standard error

7.3.2009                           5/40
Operátory > a |

   ●
        prikaz > subor
           –   Presmeruje stdout do súboru
           –   Ak súbor neexistuje, tak bude vytvorený
           –   Ak súbor existuje, bude prepísaný

    ●
        prikaz1 | prikaz2
           –   Presmeruje stdout príkazu jedna do stdin
                príkazu dva


7.3.2009                                                  6/40
Príklad >

   $ 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
Príklad >

#zlúč enie súborov 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
Prepísanie súborov >|
   ●
       Noclobber – bash ochrana (.bashrc)
$ set ­o noclobber

 cat > test3
bash: test3: nedá sa prepísať  existujúci súbor

$ cat >| test3
prepisal som subor test2        

$ cat test3
prepisal som subor test2

$ set +o noclobber
7.3.2009                                      9/40
Vytvorenie súboru

$ 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:
           –   nepredvídateľné dôsledky
           –   Netcat
           –   /usr/share/doc/bash/README.Debian.gz




7.3.2009                                              11/40
Rúra |

    ●
        Presmerovanie výstup jedného programu
        do druhého
    ●
        grep retazec1 subor | grep -v retazec2
    ●
        ls -la | less
    ●
        cat script | mysql




7.3.2009                                         12/40
Rúra |



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




7.3.2009                13/40
Príklad <
   ●
       Presmerovanie obsahu súboru do
       štandardného vstupu programu
$ ls Obrázky/
Avatar  Experimenty  Webcam

$ ls Obrázky/ > dir.txt

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

$ sort ­r < dir.txt
Webcam
Experimenty
Avatar
7.3.2009                                14/40
Kombinovanie operátorov
    ●
        Operátory 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 operátorov

   ●
       Ľubovoľný počet |
   ●
       Niektoré programy nepoužívajú stdin a
       stdout (čítajte man stránky)




7.3.2009                                       16/40
Operátor >>

   ●
       Pripojenie stdout na koniec súboru
   ●
       Ak súbor neexistuje, tak vytvorí súbor
   ●
       Ak súbor existuje, tak ho rozšíri o výstup
       spusteného programu




7.3.2009                                        17/40
Príklad >>

  $ 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ždému otvorenému súboru
   ●
       Číslo pridelené OS
   ●
       Identifikátor slúžiaci na poskytnutie
       prístupu
   ●
       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
           –   standard input:    FD 0
           –   standard output:   FD 1
           –   standard error:    FD 2
   ●
       Čísla sú sú identifikátory
           –   > je ekvivalent k 0>
           –   < je ekvivalent k <1



7.3.2009                                 21/40
Príklad 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
            –   Presmerovanie do suboru N
   ●
       >&N
            –   Presmerovanie na FD N




7.3.2009                                    23/40
stderr a stdout v jednom
 súbore
$ 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 iného terminálu
$ 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 súborov a
 zároveň 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
Príklad <<a <<-
 Here documents

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




7.3.2009                                31/40
Trvalé presmerovanie
 exec

   ●
       exec - vytvorenie nového procesu,
       nahradením starého
   ●
       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
Príklad 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ľadáva v súboroch, 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 unikátne    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
   ●
       Regulárne 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
   ●
       Operátory >, >>, &>, &>>, |&, |
   ●
       File deskriptory >&, <>, <&-, >&-
   ●
       exec
   ●
       Filtre grep, cut, sort, sed




7.3.2009                                   40/40
Ďakujem za pozornosť
           Na prípadné otázky rád odpoviem

           michal6103 [at] gmail [dot] com


7.3.2009                                     41/40
LinuxFest podporujú
Referencie




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

Mais conteúdo relacionado

Destaque

CRE-024-製商整合科技與產業創新
CRE-024-製商整合科技與產業創新CRE-024-製商整合科技與產業創新
CRE-024-製商整合科技與產業創新
handbook
 
Hr 047 從職場經驗看學習方向
Hr 047 從職場經驗看學習方向Hr 047 從職場經驗看學習方向
Hr 047 從職場經驗看學習方向
handbook
 
(Ingenieriacivilesia.blogspot.com)criterios de-diseno-estructural
(Ingenieriacivilesia.blogspot.com)criterios de-diseno-estructural(Ingenieriacivilesia.blogspot.com)criterios de-diseno-estructural
(Ingenieriacivilesia.blogspot.com)criterios de-diseno-estructural
Robert Puchulan Reyes
 
Transexualidad 120626153152-phpapp02
Transexualidad 120626153152-phpapp02Transexualidad 120626153152-phpapp02
Transexualidad 120626153152-phpapp02
Tenemos Voz
 
Expo quimica gases
Expo quimica   gasesExpo quimica   gases
Expo quimica gases
Luis Jimenez
 
Sistema operativo
Sistema operativoSistema operativo
Sistema operativo
Florbs
 

Destaque (16)

Presentacion de reconocimiento cuencas
Presentacion de reconocimiento cuencasPresentacion de reconocimiento cuencas
Presentacion de reconocimiento cuencas
 
CRE-024-製商整合科技與產業創新
CRE-024-製商整合科技與產業創新CRE-024-製商整合科技與產業創新
CRE-024-製商整合科技與產業創新
 
Fbcannounc18 september2016
Fbcannounc18 september2016Fbcannounc18 september2016
Fbcannounc18 september2016
 
Seminario 2
Seminario 2 Seminario 2
Seminario 2
 
MONKINO Apresentação para os IBP's
MONKINO Apresentação para os IBP'sMONKINO Apresentação para os IBP's
MONKINO Apresentação para os IBP's
 
Nadia
NadiaNadia
Nadia
 
Hr 047 從職場經驗看學習方向
Hr 047 從職場經驗看學習方向Hr 047 從職場經驗看學習方向
Hr 047 從職場經驗看學習方向
 
(Ingenieriacivilesia.blogspot.com)criterios de-diseno-estructural
(Ingenieriacivilesia.blogspot.com)criterios de-diseno-estructural(Ingenieriacivilesia.blogspot.com)criterios de-diseno-estructural
(Ingenieriacivilesia.blogspot.com)criterios de-diseno-estructural
 
Transexualidad 120626153152-phpapp02
Transexualidad 120626153152-phpapp02Transexualidad 120626153152-phpapp02
Transexualidad 120626153152-phpapp02
 
Servicios de Internet: Foros
Servicios de Internet: ForosServicios de Internet: Foros
Servicios de Internet: Foros
 
E-Commerce
E-CommerceE-Commerce
E-Commerce
 
Buscadores
BuscadoresBuscadores
Buscadores
 
relaciones
relacionesrelaciones
relaciones
 
Expo quimica gases
Expo quimica   gasesExpo quimica   gases
Expo quimica gases
 
Sistema operativo
Sistema operativoSistema operativo
Sistema operativo
 
Revista uñas 03
Revista uñas 03Revista uñas 03
Revista uñas 03
 

Semelhante a Linux: Presmerovanie I/O

Semelhante a Linux: Presmerovanie I/O (11)

Academy vol.13
Academy vol.13Academy vol.13
Academy vol.13
 
Linux: Procesy
Linux: ProcesyLinux: Procesy
Linux: Procesy
 
Linux: Zaklady Linuxu
Linux: Zaklady LinuxuLinux: Zaklady Linuxu
Linux: Zaklady Linuxu
 
Linux: Filesystems
Linux: FilesystemsLinux: Filesystems
Linux: Filesystems
 
Debian na minimum | Energo TT Live 2012
Debian na minimum | Energo TT Live 2012Debian na minimum | Energo TT Live 2012
Debian na minimum | Energo TT Live 2012
 
Tipy a triky na používanie funkcionálnych vlastností Pythonu
Tipy a triky na používanie funkcionálnych vlastností PythonuTipy a triky na používanie funkcionálnych vlastností Pythonu
Tipy a triky na používanie funkcionálnych vlastností Pythonu
 
Junior Internet 2015 - PHP moderne a správne
Junior Internet 2015 - PHP moderne a správneJunior Internet 2015 - PHP moderne a správne
Junior Internet 2015 - PHP moderne a správne
 
There is much more to C
There is much more to CThere is much more to C
There is much more to C
 
Ať se z kódu nepráší!
Ať se z kódu nepráší!Ať se z kódu nepráší!
Ať se z kódu nepráší!
 
Programovanie v C++
Programovanie v C++Programovanie v C++
Programovanie v C++
 
Libraries and tools for C++
Libraries and tools for C++Libraries and tools for C++
Libraries and tools for C++
 

Mais de Michal Sedlak (6)

Nodemcu - introduction
Nodemcu - introductionNodemcu - introduction
Nodemcu - introduction
 
Raspberry Pi Introduction
Raspberry Pi IntroductionRaspberry Pi Introduction
Raspberry Pi Introduction
 
Multipath
MultipathMultipath
Multipath
 
Linux: Desktop na kazdy den
Linux: Desktop na kazdy denLinux: Desktop na kazdy den
Linux: Desktop na kazdy den
 
Linux: LVM
Linux: LVMLinux: LVM
Linux: LVM
 
Twisted
TwistedTwisted
Twisted
 

Linux: Presmerovanie I/O