compilemg - bash4beginners

31
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners Bash4Beginners #!/bin/bash echo “por: Lucas Souza Fernandes”

Upload: lucasfernandes

Post on 31-May-2015

730 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Bash4Beginners

#!/bin/bash

echo “por: Lucas Souza Fernandes”

Page 2: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Just one day...

● ~/.bash_history● man - How to get help● ~/.bashrc● Hello world...● Shell script● Debug mode on

Page 3: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Just one day...

● Commands● test ● Hands on● Prática

Page 4: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

~/.bash_history

● Bourne Again Shell – BASH

● Korn shell● C shell● IEEE Posix

USUARIO → SHELL → KERNEL → HD

Page 5: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

man - How to get help... :S

● Internet● e-books● apt-get install abs-

guide● man

● whereis● which● apropos

– apropos banner

Page 6: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

~/.bashrc

● /etc/profile● ~/.bash_profile, ~/.bash_login or ~/.profile● ~/.bash_logout● cp /etc/skel/.bashrc ~/.bashrc● echo $?● echo $-● vi ~/.bashrc

Page 7: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Hello world...

#!/bin/bashecho Hello Worldecho Bkp hometar -cZf /tmp/my-backup.tgz /home/me

Page 8: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Hello world...

#!/bin/bashecho Hello Worldecho Bkp hometar -cZf /tmp/my-backup.tgz /home/me

Page 9: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Hello world...

Seu bkp foi criado ???

Page 10: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Hello World...

#!/bin/bashecho Hello Worldecho Bkp hometar -cZf /tmp/my-backup.tgz /home/meIf [ $? = 0 ] ; then

echo Successfully completedelse

echo Error...fi

Page 11: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Shell script...

Um arquivo no qual temos uma lista de comandos a serem executados, que podem ser chamados a qualquer momento.

#!/bin/bashdatedfw

Page 12: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Shell script...

● Escolha um nome para seu script (filho);● Criar o arquivo e listar os comandos;● Evocando o shell na primeira linha:

– #!/bin/bash

● Tornar o script um executável:– chmod u+rx meuscript.sh

● Executar seu script.

Page 13: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Debug mode on...

● “Comando não encontrado”– echo $PATH

● “Permissão negada”– chmod u+rx seuscript

● “Erro de sintaxe”– set -xv

Page 14: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

#!/bin/bashecho Hello Userecho Bkp your home? [yn]read answer test “$answer” = 'n' && exittar -cZf /tmp/my-backup.tgz /home/cefetif [ $? = 0 ] ; then

echo Successfully completedelse

echo Error...fi

Hello user...

Page 15: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Commands...

● cat● cut● date● find● grep● head● printf

● rev● sed● seq● sort● tail● tr● uniq

Page 16: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Commands...

● wc● man comando● comando --help

Page 17: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

test...

test variables● -lt → LessThan● -gt → GreaterThan● -le → LessEqual● -ge →

GreaterEqual● -eq → EQual

● ne → NotEqual● = → String● != → Not equal● -n → not Null● -z → is null

Page 18: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

test...

test files● -d → directory ?● -f → file ?● -r → read ?● -s → file size > 0● -w → write ?● -nt → NeweThan

● -ot → OlderThan● -ef → EqualFile● -a → AND● -o → OR

Page 19: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Hands on...

Let's rock... um script no qual o usuário informe o nome do arquivo e o script irá testar

se este arquivo existe. Se sim diz se é um arquivo ou diretório.

Page 20: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

./argumento.sh arg1 arg2 arg3 arg4

#!/bin/bash#argumentos – algumas variaveis especiaisecho “o nome deste script é: $0”echo “recebidos $# argumentos: $*”echo “1st argumento: $1”echo “2sd argumento: $2”

Hands on...

Page 21: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Hands on...

$((...))

echo $((2*3-2/2+3))

Page 22: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Hands on...

if test “$var” -gt 10then

echo maior que 10else

echo menor que 10fi

Page 23: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Hands on...

If [ “$var” -gt 10 ]then

echo maior que 10else

echo menor que 10fi

Page 24: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Hands on...

while test -f /tmp/lockdo

echo “script travado...”sleep 1

done

Page 25: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Hands on...

while :do

if test -f /tmp/lockthen

echo “ainda em lock...”sleep 1

elsebreak

fidone

Page 26: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Prática...- Receba dois números como parametro e mostre a relação entre eles../relacao.sh 3 53 é menor que 5

- Recebe um número como parametro e o diminui até chegar a zero mostrando cada passo na tela./zerador.sh 55 4 3 2 1 0

Page 27: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Prática...

- Recebe 2 palavras como parametro e verifica se a primeira esta na segunda. (grep)./substring.sh ana bananaana esta contida em banana

- Exibe todos os paramentros recebidos “juntos” (tr)./juntatudo.sh o l a m u n d o c r u e lolamundocruel

Page 28: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

- Do arquivo /etc/passwd, mostra usuário e o nome completo, campos 1 e 5 separados por um TAB. (cut)./users.shftpftp usernobodynobodylucas lucas souza fernandes

Prática...

Page 29: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Prática...

- Do arquivo /etc/passwd, mostra todos os shells (ultimo campo). (uniq)./shell.sh/bin/bash/bin/false

Page 30: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Prática...

Mostra na tela todos os parametros recebidos (shift)./parametros a b c d eParametro 1 : aParametro 2 : bParametro 3 : cParametro 4 : dParametro 5 : e

Page 31: CompileMG - Bash4beginners

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Obrigado...

[email protected]