Vous êtes sur la page 1sur 23

Shell Script

Melhorando tarefas
Daniel Bauermann
dbauermann@uol.com.br

O que vou ver hoje?

Introduo sobre Shell Script

Um curso rel!mpa"o# de Shell Script



Introduo

O que $ Shell?

Shell % &oncha
'onte( http())t*i+i.so,t*arelivre.or")bin)vie*)-.i+iBar)-.i+iBar/apo001

/ara que uso o Shell?

23ecutar pro"ramas do sistema


$ cd documentos
$ cat conteudo.txt
$ ls
$ rm arquivo_antigo.pdf

4SDOS melhorado 5bem melhorado67



23istem op8es de Shell?

Bourne Shell 5sh7

9orn Shell 5ksh7

Bourne :"ain Shell 5bash7

& Shell 5csh7

-2;2< & Shell 5tcsh7

= Shell 5zsh7

>ual a di,erena do Shell Script?

? que comandos b@sicosA possui estruturas


de lin"ua"ens de pro"ramao

vari@veisA i,A ,orA *hileA ...

Diversos usos

&BIs

instaladores

rotinas bac+up

rotinas de administrao

&urso rel!mpa"o#

O que vou aprender?

&riar Shell Script bsico

2strutura elementar

escolhendo o Shell

como de,inir e usar vari@veis

usar condio 5if7



O que vou precisar?

&onhecimento b@sico de comandos Cinu3

5dateA echoA whoA etc7

1 editor

&riatividade (7

2 comea quando?

4ontando o problema...
>uero saber quem est@ conectado no
sistema 6
:hA que horas so a"ora mesmo?

4as posso ,aDer via comando?

Eesolvendo via comandos


$ w
$ date

/osso simpli,icar?

&om a criao de um script tornamos mais


simples e a"rad@vel a tare,a de
administrao
$ vi quem.sh
#!/bin/sh
# quem.sh - Script que mostra quem esta logado e
# data/hora atual
#
# 28/06/2007 Daniel Bauermann
# Verifica quais usuarios estao logados
w
# Informa a data/hora
date

2 como e3ecuto?

:justar permisso de e3ecuo


$ chmod u+x quem.sh

Eodar script
$ ./quem.sh
1

:hA $ simples6 /osso melhorar?

4elhorando a ,ormatao da data)hora...


# Informa a data/hora
date +"%nHoje eh dia %d/%m/%Y e agora sao %H:%M"

O resultado da e3ecuo comea a tornarF


se mais ami"@vel 5obaA vamos impressionar
o che,e67
G

/osso melhorar mais?

Simpli,icar o resultado da lista"em de


usu@rios
# Verifica quais usuarios estao logados
echo Usuarios logados no sistema:
echo ----------------------------
w -h | cut -d -f1 | sort | sort -u
H

SI isto?

&riando um condio de teste


# Informa a data/hora
date +"%nHoje eh dia %d/%m/%Y e agora sao %H:%M"
# Verifica se acabou o expediente
if [ `date +%H` -ge 18 ]
then
echo Acabou o expediente, vai pra casa!
fi

5isto o che,e no vai "ostar67


J

Isto eu "ostei6 /osso melhorar?

Usando vari@vel para de,inir hora ,inal


### Variaveis
HORA_FINAL_EXPEDIENTE=18
(...)
# Verifica se acabou o expediente
if [ `date +%H` -ge ${HORA_FINAL_EXPEDIENTE} ]
then
echo Acabou o expediente, vai pra casa!
fi

2 a"oraA o que ,aDer?

/ense em suas tare,as cotidianas

2ncontre al"o para automatiDar

2labore

:rre"ace as man"as

-enteA e3perimente

DivertaFse acima de tudo666



Ee,erKncias

:ur$lio 4arinho Lar"as

http())aurelio.net

/apo de Botequim M LNlio ;eves

http())***.julioneves.com

Shell Script
Melhorando tarefas
Perguntas?
Daniel Bauermann
dbauermann@uol.com.br

:presenta8es

1O)0P)G00Q M RSo,t*areCivreSST Dia da Ciberdade do So,t*are G00Q F


;ovo Uambur"o F ES

&opVri"ht 5c7 G00QA Daniel E. Bauermann
:ll ri"hts reserved.
Eedistribution and use in source and binarV ,ormsA *ith or *ithout modi,icationA are
permitted provided that the ,ollo*in" conditions are met(
W Eedistributions o, source code must retain the above copVri"ht noticeA this list o,
conditions and the ,ollo*in" disclaimer.
W Eedistributions in binarV ,orm must reproduce the above copVri"ht noticeA this list o,
conditions and the ,ollo*in" disclaimer in the documentation and)or other materials
provided *ith the distribution.
W ;either the name o, the :nauK Solu8es em In,orm@tica nor the names o, its contributors
maV be used to endorse or promote products derived ,rom this so,t*are *ithout speci,ic
prior *ritten permission.
-UIS SO'-.:E2 IS /EOSID2D BX -U2 &O/XEIBU- UOCD2ES :;D &O;-EIBU-OES
Y:S ISY :;D :;X 2</E2SS OE I4/CI2D .:EE:;-I2SA I;&CUDI;BA BU- ;O-
CI4I-2D -OA -U2 I4/CI2D .:EE:;-I2S O' 42E&U:;-:BICI-X :;D 'I-;2SS 'OE
: /:E-I&UC:E /UE/OS2 :E2 DIS&C:I42D. I; ;O 2S2;- SU:CC -U2 &O/XEIBU- O.;2E OE
&O;-EIBU-OES B2 CI:BC2 'OE :;X DIE2&-A I;DIE2&-A I;&ID2;-:CA S/2&I:CA
2<24/C:EXA OE &O;S2>U2;-I:C D:4:B2S 5I;&CUDI;BA BU- ;O- CI4I-2D -OA
/EO&UE242;- O' SUBS-I-U-2 BOODS OE S2ESI&2SZ COSS O' US2A D:-:A OE
/EO'I-SZ OE BUSI;2SS I;-2EEU/-IO;7 UO.2S2E &:US2D :;D O; :;X -U2OEX O'
CI:BICI-XA .U2-U2E I; &O;-E:&-A S-EI&- CI:BICI-XA OE -OE- 5I;&CUDI;B
;2BCIB2;&2 OE O-U2E.IS27 :EISI;B I; :;X .:X OU- O' -U2 US2 O' -UIS
SO'-.:E2A 2S2; I' :DSIS2D O' -U2 /OSSIBICI-X O' SU&U D:4:B2.

Vous aimerez peut-être aussi