Fundamentos do sistema Linux - direcionadores
Direcionadores de fluxo fazem o que o nome sugere: direcionam o fluxo da saída dos programas, enviando-as para locais diferentes da saída padrão. Esses locais podem ser arquivos, dispositivos, processos e outros programas. Nesse artigo veremos como utilizar os direcionadores do Linux para facilitar a vida do administrador de sistemas.
Parte 8: O direcionador |
Por fim, temos o último direcionador, o |, também conhecido como pipe (duto, cano), utilizado para direcionar a saída de um comando para outro comando, e é utilizado todo o tempo para a realização de todo tipo de tarefas e procedimentos.
Um dos usos mais comuns do | é com os comandos ls e more (ou less), para ver o conteúdo de um diretório, quando este não cabe na tela, como o diretório /usr/bin/, por exemplo. Para que possamos ver todos os arquivos, direcionamos a saída do ls para o more, que faz a paginação do conteúdo:
$ ls /usr/bin | more
[
411toppm
822-date
a2p
aaxine
aclocal
aclocal-1.4
aconnect
acpi
acpi_available
acpi_listen
activation-client
addr2line
alsamixer
amidi
amixer
anytopnm
aplay
aplaymidi
apm_available
apropos
apt-cache
apt-cdrom
--More--
Agora, basta pressionar Enter para rolar a lista, de forma que podemos ver todos os arquivos. O | pode usado também para diversas tarefas. Veja esse uso avançado do |, onde direcionamos vários comandos para instalar o OpenOffice.org e todos os seus pacotes para o português brasileiro (pt-br), através do APT, no Debian:
# apt-cache search openoffice |\
grep pt-br |\
cut -d' ' -f1 |\
tr '\n' ' ' |\
xargs apt-get install -y
Vamos entender agora cada comando executado.
Primeiramente, rodamos o comando apt-cache search openoffice. A saída desse comando é a seguinte:
# apt-cache search openoffice
boxshade - [Biology] Pretty-printing of multiple sequence alignments
dictionaries-common - Common utilities for spelling dictionary tools
dmake - make utility used to build OpenOffice.org
extract - displays meta-data from files of arbitrary type
fig2sxd - convert XFig files to OpenOffice.org format
gkrellm-hdplop - A hard drive activity monitor GKrellM plugin
gramps - Genealogical Research and Analysis Management Program
libaltlinuxhyph-dev - ALTLinux hyphenation library development files
libextractor1 - extracts meta-data from files of arbitrary type
libooolib-perl - Perl library for creating OpenOffice.org documents (calc/writer)
libspreadsheet-writeexcel-perl - create Excel spreadsheets
myspell-bg - The Bulgarian dictionary for myspell
myspell-ca - Catalan dictionary for myspell
myspell-de-at - Austrian (German) dictionary for myspell
myspell-de-ch - Swiss (German) dictionary for myspell
myspell-de-de - German dictionary for myspell
myspell-de-de-oldspell - German dictionary for myspell (old orthography)
myspell-el-gr - Greek (el_GR) dictionary for myspell
myspell-en-gb - English (GB) dictionary for myspell
myspell-en-us - English (US) dictionary for myspell
myspell-eo - The Esperanto dictionary for myspell
myspell-es - The Spanish dictionary for myspell
myspell-fi - The Finnish dictionary for myspell
myspell-fr-gut - The French dictionary for myspell (GUTenberg version)
myspell-ga - An Irish (Gaeilge) dictionary for OpenOffice and Mozilla
myspell-gd - Scots Gaelic dictionary for myspell
myspell-gl-es - Galician dictionary for myspell
myspell-gv - Manx Gaelic dictionary for myspell
myspell-he - Hebrew dictionary for myspell
myspell-hu - The Hungarian dictionary for myspell
myspell-it - Italian dictionary for myspell
myspell-lt - myspell dictionary for Lithuanian (LT)
myspell-lv - Latvian MySpell dictionary
myspell-nb - Norwegian bokm�¥l dictionary for myspell
myspell-nl - Dutch dictionary for myspell
myspell-nn - Norwegian nynorsk dictionary for myspell
myspell-pl - The Polish dictionary for myspell
myspell-pt-br - The Brazilian Portuguese dictionary for myspell
myspell-ru - The Russian dictionary for myspell
myspell-sl - Slovenian dictionary for myspell
myspell-sv-se - Swedish (SE) dictionary for myspell
myspell-uk - Ukrainian dictionary for myspell
oooqs-kde - OpenOffice.org QuickStarter applet for KDE
ooqstart-gnome - OpenOffice.org QuickStarter applet for GNOME 2
openoffice.org - high-quality office productivity suite
openoffice.org-bin - OpenOffice.org office suite binary files
openoffice.org-debian-files - Debian specific parts of OpenOffice.org
openoffice.org-dev - OpenOffice.org SDK - development files
openoffice.org-dev-doc - OpenOffice.org SDK - documentation and examples
openoffice.org-evolution - Evolution 2 Addressbook support for OpenOffice.org
openoffice.org-gnomevfs - GNOME VFS support for OpenOffice.org
openoffice.org-gtk-gnome - Gtk UI Plugin and GNOME File Picker for OpenOffice.org
openoffice.org-help-de - OpenOffice.org office suite help (German)
openoffice.org-help-el - OpenOffice.org office suite help (Greek)
openoffice.org-help-en - OpenOffice.org office suite help (English)
openoffice.org-help-es - OpenOffice.org office suite help (Spanish)
openoffice.org-help-fr - OpenOffice.org office suite help (French)
openoffice.org-help-it - OpenOffice.org office suite help (Italian)
openoffice.org-help-ja - OpenOffice.org office suite help (Japanese)
openoffice.org-help-ko - OpenOffice.org office suite help (Korean)
openoffice.org-help-pt-br - OpenOffice.org office suite help (Brazilian Portuguese)
openoffice.org-help-sv - OpenOffice.org office suite help (Swedish)
openoffice.org-help-tr - OpenOffice.org office suite help (Turkish
openoffice.org-help-zh-cn - OpenOffice.org office suite help (simplified Chinese)
openoffice.org-help-zh-tw - OpenOffice.org office suite help (traditional Chinese)
openoffice.org-kde - KDE UI Plugin and KDE File Picker for OpenOffice.org
openoffice.org-l10n-af - Afrikaans language package for OpenOffice.org
openoffice.org-l10n-ar - Arabic language package for OpenOffice.org
openoffice.org-l10n-ca - Catalan language package for OpenOffice.org
openoffice.org-l10n-cs - Czech language package for OpenOffice.org
openoffice.org-l10n-cy - Welsh language package for OpenOffice.org
openoffice.org-l10n-da - Danish language package for OpenOffice.org
openoffice.org-l10n-de - German language package for OpenOffice.org
openoffice.org-l10n-el - Greek language package for OpenOffice.org
openoffice.org-l10n-en - English (US) language package for OpenOffice.org
openoffice.org-l10n-es - Spanish language package for OpenOffice.org
openoffice.org-l10n-et - Estonian language package for OpenOffice.org
openoffice.org-l10n-eu - Basque language package for OpenOffice.org
openoffice.org-l10n-fi - Finnish language package for OpenOffice.org
openoffice.org-l10n-fr - French language package for OpenOffice.org
openoffice.org-l10n-gl - Galician language package for OpenOffice.org
openoffice.org-l10n-he - Hebrew language package for OpenOffice.org
openoffice.org-l10n-hi - Hindi language package for OpenOffice.org
openoffice.org-l10n-hu - Hungarian language package for OpenOffice.org
openoffice.org-l10n-it - Italian language package for OpenOffice.org
openoffice.org-l10n-ja - Japanese language package for OpenOffice.org
openoffice.org-l10n-kn - Kannada language package for OpenOffice.org
openoffice.org-l10n-ko - Korean language package for OpenOffice.org
openoffice.org-l10n-lt - Lithuanian language package for OpenOffice.org
openoffice.org-l10n-nb - Norwegian Bokmal language package for OpenOffice.org
openoffice.org-l10n-nl - Dutch language package for OpenOffice.org
openoffice.org-l10n-nn - Norwegian Nynorsk language package for OpenOffice.org
openoffice.org-l10n-ns - Northern Sotho language package for OpenOffice.org
openoffice.org-l10n-pl - Polish language package for OpenOffice.org
openoffice.org-l10n-pt - Portuguese language package for OpenOffice.org
openoffice.org-l10n-pt-br - Portuguese (Brazil) language package for OpenOffice.org
openoffice.org-l10n-ru - Russian language package for OpenOffice.org
openoffice.org-l10n-sk - Slovak language package for OpenOffice.org
openoffice.org-l10n-sl - Slovenian language package for OpenOffice.org
openoffice.org-l10n-sv - Swedish language package for OpenOffice.org
openoffice.org-l10n-th - Thai language package for OpenOffice.org
openoffice.org-l10n-tn - Tswana language package for OpenOffice.org
openoffice.org-l10n-tr - Turkish language package for OpenOffice.org
openoffice.org-l10n-zh-cn - Chinese Simplified language package for OpenOffice.org
openoffice.org-l10n-zh-tw - Chinese Traditional language package for OpenOffice.org
openoffice.org-l10n-zu - Zulu language package for OpenOffice.org
openoffice.org-mimelnk - OpenOffice.org MIME bindings for KDE
openoffice.org-thesaurus-de - German Thesaurus for OpenOffice.org
openoffice.org-thesaurus-en-us - English (US) Thesaurus for OpenOffice.org
openoffice.org-thesaurus-it - Italian Thesaurus for OpenOffice.org
tex4ht - LaTeX and TeX for Hypertext (HTML)
ttf-opensymbol - The OpenSymbol TrueType font
wmhdplop - A hard drive activity monitor dockapp
wpd2sxw - WordPerfect to OpenOffice.org converter
zope-textindexng2 - full text index for Zope
mozilla-mplayer - MPlayer-Plugin for Mozilla, Konqueror and OpenOffice.org
openoffice.org-hyphenation-da - Danish hyphenation pattern for OpenOffice.org
openoffice.org-hyphenation-de - German hyphenation pattern for OpenOffice.org
openoffice.org-hyphenation-en-gb - English (GB) hyphenation pattern for OpenOffice.org
openoffice.org-hyphenation-en-us - English (US) hyphenation pattern for OpenOffice.org
openoffice.org-hyphenation-it - Italian hyphenation pattern for OpenOffice.org
openoffice.org-hyphenation-ru - Russian hyphenation pattern for OpenOffice.org
Realmente, uma lista muito grande. Temos muitos pacotes específicos para outros idiomas. Como estamos no Brasil, interessa-nos somente os pacotes para o português brasileiro. Para isso, utilizamos o comando grep, para só exibir as linhas onde aparecer o padrão "pt-br", sigla de "português brasileiro":
# apt-cache search openoffice | grep pt-br
myspell-pt-br - The Brazilian Portuguese dictionary for myspell
openoffice.org-help-pt-br - OpenOffice.org office suite help (Brazilian Portuguese)
openoffice.org-l10n-pt-br - Portuguese (Brazil) language package for OpenOffice.org
A lista já está consideravelmente menor. Agora, vamos filtrar essa lista para que ela possa ser utilizada pelo apt-get. Primeiramente, usamos o comando cut para excluir a descrição do arquivo, deixando somente o nome do pacote:
# apt-cache search openoffice | grep pt-br | cut -d' ' -f1
myspell-pt-br
openoffice.org-help-pt-br
openoffice.org-l10n-pt-br
Agora, temos somente os nomes dos pacotes. Precisamos agora colocá-los em seqüência, separados por espaços. Para isso, vamos usar o comando tr, para substituir os caracteres de novas linhas (\n) por espaços:
# apt-cache search openoffice | grep pt-br | cut -d' ' -f1 |\
tr '\n' ' '
myspell-pt-br openoffice.org-help-pt-br openoffice.org-l10n-pt-br
Agora, basta jogar essa linha como argumentos para o comando apt-get install -y. Para isso, usamos o comando xargs:
# apt-cache search openoffice |\
grep pt-br |\
cut -d' ' -f1 |\
tr '\n' ' ' |\
xargs apt-get install -y
Esse comando acima é equivalente a:
# apt-get install -y myspell-pt-br openoffice.org-help-pt-br \
openoffice.org-l10n-pt-br
Dessa forma, o direcionador | nos poupou um enorme trabalho. Imagine se você tivesse que procurar em toda a lista gerada pelo apt-cache search openoffice pelos pacotes que você quer, anotar seus nomes e depois utilizar o apt-get install -y, digitando o nome de cada pacote. Muito mais trabalho. Com o | fazemos a mesma coisa com apenas um comando!
Um dos usos mais comuns do | é com os comandos ls e more (ou less), para ver o conteúdo de um diretório, quando este não cabe na tela, como o diretório /usr/bin/, por exemplo. Para que possamos ver todos os arquivos, direcionamos a saída do ls para o more, que faz a paginação do conteúdo:
$ ls /usr/bin | more
[
411toppm
822-date
a2p
aaxine
aclocal
aclocal-1.4
aconnect
acpi
acpi_available
acpi_listen
activation-client
addr2line
alsamixer
amidi
amixer
anytopnm
aplay
aplaymidi
apm_available
apropos
apt-cache
apt-cdrom
--More--
Agora, basta pressionar Enter para rolar a lista, de forma que podemos ver todos os arquivos. O | pode usado também para diversas tarefas. Veja esse uso avançado do |, onde direcionamos vários comandos para instalar o OpenOffice.org e todos os seus pacotes para o português brasileiro (pt-br), através do APT, no Debian:
# apt-cache search openoffice |\
grep pt-br |\
cut -d' ' -f1 |\
tr '\n' ' ' |\
xargs apt-get install -y
Vamos entender agora cada comando executado.
Primeiramente, rodamos o comando apt-cache search openoffice. A saída desse comando é a seguinte:
# apt-cache search openoffice
boxshade - [Biology] Pretty-printing of multiple sequence alignments
dictionaries-common - Common utilities for spelling dictionary tools
dmake - make utility used to build OpenOffice.org
extract - displays meta-data from files of arbitrary type
fig2sxd - convert XFig files to OpenOffice.org format
gkrellm-hdplop - A hard drive activity monitor GKrellM plugin
gramps - Genealogical Research and Analysis Management Program
libaltlinuxhyph-dev - ALTLinux hyphenation library development files
libextractor1 - extracts meta-data from files of arbitrary type
libooolib-perl - Perl library for creating OpenOffice.org documents (calc/writer)
libspreadsheet-writeexcel-perl - create Excel spreadsheets
myspell-bg - The Bulgarian dictionary for myspell
myspell-ca - Catalan dictionary for myspell
myspell-de-at - Austrian (German) dictionary for myspell
myspell-de-ch - Swiss (German) dictionary for myspell
myspell-de-de - German dictionary for myspell
myspell-de-de-oldspell - German dictionary for myspell (old orthography)
myspell-el-gr - Greek (el_GR) dictionary for myspell
myspell-en-gb - English (GB) dictionary for myspell
myspell-en-us - English (US) dictionary for myspell
myspell-eo - The Esperanto dictionary for myspell
myspell-es - The Spanish dictionary for myspell
myspell-fi - The Finnish dictionary for myspell
myspell-fr-gut - The French dictionary for myspell (GUTenberg version)
myspell-ga - An Irish (Gaeilge) dictionary for OpenOffice and Mozilla
myspell-gd - Scots Gaelic dictionary for myspell
myspell-gl-es - Galician dictionary for myspell
myspell-gv - Manx Gaelic dictionary for myspell
myspell-he - Hebrew dictionary for myspell
myspell-hu - The Hungarian dictionary for myspell
myspell-it - Italian dictionary for myspell
myspell-lt - myspell dictionary for Lithuanian (LT)
myspell-lv - Latvian MySpell dictionary
myspell-nb - Norwegian bokm�¥l dictionary for myspell
myspell-nl - Dutch dictionary for myspell
myspell-nn - Norwegian nynorsk dictionary for myspell
myspell-pl - The Polish dictionary for myspell
myspell-pt-br - The Brazilian Portuguese dictionary for myspell
myspell-ru - The Russian dictionary for myspell
myspell-sl - Slovenian dictionary for myspell
myspell-sv-se - Swedish (SE) dictionary for myspell
myspell-uk - Ukrainian dictionary for myspell
oooqs-kde - OpenOffice.org QuickStarter applet for KDE
ooqstart-gnome - OpenOffice.org QuickStarter applet for GNOME 2
openoffice.org - high-quality office productivity suite
openoffice.org-bin - OpenOffice.org office suite binary files
openoffice.org-debian-files - Debian specific parts of OpenOffice.org
openoffice.org-dev - OpenOffice.org SDK - development files
openoffice.org-dev-doc - OpenOffice.org SDK - documentation and examples
openoffice.org-evolution - Evolution 2 Addressbook support for OpenOffice.org
openoffice.org-gnomevfs - GNOME VFS support for OpenOffice.org
openoffice.org-gtk-gnome - Gtk UI Plugin and GNOME File Picker for OpenOffice.org
openoffice.org-help-de - OpenOffice.org office suite help (German)
openoffice.org-help-el - OpenOffice.org office suite help (Greek)
openoffice.org-help-en - OpenOffice.org office suite help (English)
openoffice.org-help-es - OpenOffice.org office suite help (Spanish)
openoffice.org-help-fr - OpenOffice.org office suite help (French)
openoffice.org-help-it - OpenOffice.org office suite help (Italian)
openoffice.org-help-ja - OpenOffice.org office suite help (Japanese)
openoffice.org-help-ko - OpenOffice.org office suite help (Korean)
openoffice.org-help-pt-br - OpenOffice.org office suite help (Brazilian Portuguese)
openoffice.org-help-sv - OpenOffice.org office suite help (Swedish)
openoffice.org-help-tr - OpenOffice.org office suite help (Turkish
openoffice.org-help-zh-cn - OpenOffice.org office suite help (simplified Chinese)
openoffice.org-help-zh-tw - OpenOffice.org office suite help (traditional Chinese)
openoffice.org-kde - KDE UI Plugin and KDE File Picker for OpenOffice.org
openoffice.org-l10n-af - Afrikaans language package for OpenOffice.org
openoffice.org-l10n-ar - Arabic language package for OpenOffice.org
openoffice.org-l10n-ca - Catalan language package for OpenOffice.org
openoffice.org-l10n-cs - Czech language package for OpenOffice.org
openoffice.org-l10n-cy - Welsh language package for OpenOffice.org
openoffice.org-l10n-da - Danish language package for OpenOffice.org
openoffice.org-l10n-de - German language package for OpenOffice.org
openoffice.org-l10n-el - Greek language package for OpenOffice.org
openoffice.org-l10n-en - English (US) language package for OpenOffice.org
openoffice.org-l10n-es - Spanish language package for OpenOffice.org
openoffice.org-l10n-et - Estonian language package for OpenOffice.org
openoffice.org-l10n-eu - Basque language package for OpenOffice.org
openoffice.org-l10n-fi - Finnish language package for OpenOffice.org
openoffice.org-l10n-fr - French language package for OpenOffice.org
openoffice.org-l10n-gl - Galician language package for OpenOffice.org
openoffice.org-l10n-he - Hebrew language package for OpenOffice.org
openoffice.org-l10n-hi - Hindi language package for OpenOffice.org
openoffice.org-l10n-hu - Hungarian language package for OpenOffice.org
openoffice.org-l10n-it - Italian language package for OpenOffice.org
openoffice.org-l10n-ja - Japanese language package for OpenOffice.org
openoffice.org-l10n-kn - Kannada language package for OpenOffice.org
openoffice.org-l10n-ko - Korean language package for OpenOffice.org
openoffice.org-l10n-lt - Lithuanian language package for OpenOffice.org
openoffice.org-l10n-nb - Norwegian Bokmal language package for OpenOffice.org
openoffice.org-l10n-nl - Dutch language package for OpenOffice.org
openoffice.org-l10n-nn - Norwegian Nynorsk language package for OpenOffice.org
openoffice.org-l10n-ns - Northern Sotho language package for OpenOffice.org
openoffice.org-l10n-pl - Polish language package for OpenOffice.org
openoffice.org-l10n-pt - Portuguese language package for OpenOffice.org
openoffice.org-l10n-pt-br - Portuguese (Brazil) language package for OpenOffice.org
openoffice.org-l10n-ru - Russian language package for OpenOffice.org
openoffice.org-l10n-sk - Slovak language package for OpenOffice.org
openoffice.org-l10n-sl - Slovenian language package for OpenOffice.org
openoffice.org-l10n-sv - Swedish language package for OpenOffice.org
openoffice.org-l10n-th - Thai language package for OpenOffice.org
openoffice.org-l10n-tn - Tswana language package for OpenOffice.org
openoffice.org-l10n-tr - Turkish language package for OpenOffice.org
openoffice.org-l10n-zh-cn - Chinese Simplified language package for OpenOffice.org
openoffice.org-l10n-zh-tw - Chinese Traditional language package for OpenOffice.org
openoffice.org-l10n-zu - Zulu language package for OpenOffice.org
openoffice.org-mimelnk - OpenOffice.org MIME bindings for KDE
openoffice.org-thesaurus-de - German Thesaurus for OpenOffice.org
openoffice.org-thesaurus-en-us - English (US) Thesaurus for OpenOffice.org
openoffice.org-thesaurus-it - Italian Thesaurus for OpenOffice.org
tex4ht - LaTeX and TeX for Hypertext (HTML)
ttf-opensymbol - The OpenSymbol TrueType font
wmhdplop - A hard drive activity monitor dockapp
wpd2sxw - WordPerfect to OpenOffice.org converter
zope-textindexng2 - full text index for Zope
mozilla-mplayer - MPlayer-Plugin for Mozilla, Konqueror and OpenOffice.org
openoffice.org-hyphenation-da - Danish hyphenation pattern for OpenOffice.org
openoffice.org-hyphenation-de - German hyphenation pattern for OpenOffice.org
openoffice.org-hyphenation-en-gb - English (GB) hyphenation pattern for OpenOffice.org
openoffice.org-hyphenation-en-us - English (US) hyphenation pattern for OpenOffice.org
openoffice.org-hyphenation-it - Italian hyphenation pattern for OpenOffice.org
openoffice.org-hyphenation-ru - Russian hyphenation pattern for OpenOffice.org
Realmente, uma lista muito grande. Temos muitos pacotes específicos para outros idiomas. Como estamos no Brasil, interessa-nos somente os pacotes para o português brasileiro. Para isso, utilizamos o comando grep, para só exibir as linhas onde aparecer o padrão "pt-br", sigla de "português brasileiro":
# apt-cache search openoffice | grep pt-br
myspell-pt-br - The Brazilian Portuguese dictionary for myspell
openoffice.org-help-pt-br - OpenOffice.org office suite help (Brazilian Portuguese)
openoffice.org-l10n-pt-br - Portuguese (Brazil) language package for OpenOffice.org
A lista já está consideravelmente menor. Agora, vamos filtrar essa lista para que ela possa ser utilizada pelo apt-get. Primeiramente, usamos o comando cut para excluir a descrição do arquivo, deixando somente o nome do pacote:
# apt-cache search openoffice | grep pt-br | cut -d' ' -f1
myspell-pt-br
openoffice.org-help-pt-br
openoffice.org-l10n-pt-br
Agora, temos somente os nomes dos pacotes. Precisamos agora colocá-los em seqüência, separados por espaços. Para isso, vamos usar o comando tr, para substituir os caracteres de novas linhas (\n) por espaços:
# apt-cache search openoffice | grep pt-br | cut -d' ' -f1 |\
tr '\n' ' '
myspell-pt-br openoffice.org-help-pt-br openoffice.org-l10n-pt-br
Agora, basta jogar essa linha como argumentos para o comando apt-get install -y. Para isso, usamos o comando xargs:
# apt-cache search openoffice |\
grep pt-br |\
cut -d' ' -f1 |\
tr '\n' ' ' |\
xargs apt-get install -y
Esse comando acima é equivalente a:
# apt-get install -y myspell-pt-br openoffice.org-help-pt-br \
openoffice.org-l10n-pt-br
Dessa forma, o direcionador | nos poupou um enorme trabalho. Imagine se você tivesse que procurar em toda a lista gerada pelo apt-cache search openoffice pelos pacotes que você quer, anotar seus nomes e depois utilizar o apt-get install -y, digitando o nome de cada pacote. Muito mais trabalho. Com o | fazemos a mesma coisa com apenas um comando!
direcionadores é matéria para LPI, caso alguém tiver interesse.
valeu