1. Bash4Beginners
#!/bin/bash
echo “por: Lucas Souza Fernandes”
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
2. Just one day...
● ~/.bash_history
● man - How to get help
● ~/.bashrc
● Hello world...
● Shell script
● Debug mode on
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
3. Just one day...
● Commands
● test
● Hands on
● Prática
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
4. ~/.bash_history
● Bourne Again Shell
– BASH
● Korn shell
● C shell
● IEEE Posix
USUARIO → SHELL → KERNEL → HD
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
5. man - How to get help... :S
● Internet ● whereis
● e-books ● which
● apt-get install abs- ● apropos
guide – apropos banner
● man
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
6. ~/.bashrc
● /etc/profile
● ~/.bash_profile, ~/.bash_login or ~/.profile
● ~/.bash_logout
● cp /etc/skel/.bashrc ~/.bashrc
● echo $?
● echo $-
● vi ~/.bashrc
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
9. Hello world...
Seu bkp foi criado ???
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
10. Hello World...
#!/bin/bash
echo Hello World
echo Bkp home
tar -cZf /tmp/my-backup.tgz /home/me
If [ $? = 0 ] ; then
echo Successfully completed
else
echo Error...
fi
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
11. Shell script...
Um arquivo no qual temos uma lista de
comandos a serem executados, que podem
ser chamados a qualquer momento.
#!/bin/bash
date
df
w
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
12. 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.
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
13. Debug mode on...
● “Comando não encontrado”
– echo $PATH
● “Permissão negada”
– chmod u+rx seuscript
● “Erro de sintaxe”
– set -xv
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
14. Hello user...
#!/bin/bash
echo Hello User
echo Bkp your home? [yn]
read answer
test “$answer” = 'n' && exit
tar -cZf /tmp/my-backup.tgz /home/cefet
if [ $? = 0 ] ; then
echo Successfully completed
else
echo Error...
fi
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
15. Commands...
● cat ● rev
● cut ● sed
● date ● seq
● find ● sort
● grep ● tail
● head ● tr
● printf ● uniq
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
16. Commands...
● wc
● man comando
● comando --help
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
17. test...
test variables ● ne → NotEqual
● -lt → LessThan ● = → String
● -gt → GreaterThan ● != → Not equal
● -le → LessEqual ● -n → not Null
● -ge → ● -z → is null
GreaterEqual
● -eq → EQual
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
18. test...
test files ● -ot → OlderThan
● -d → directory ? ● -ef → EqualFile
● -f → file ? ● -a → AND
● -r → read ? ● -o → OR
● -s → file size > 0
● -w → write ?
● -nt → NeweThan
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
19. 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.
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
20. Hands on...
./argumento.sh arg1 arg2 arg3 arg4
#!/bin/bash
#argumentos – algumas variaveis especiais
echo “o nome deste script é: $0”
echo “recebidos $# argumentos: $*”
echo “1st argumento: $1”
echo “2sd argumento: $2”
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
21. Hands on...
$((...))
echo $((2*3-2/2+3))
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
22. Hands on...
if test “$var” -gt 10
then
echo maior que 10
else
echo menor que 10
fi
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
23. Hands on...
If [ “$var” -gt 10 ]
then
echo maior que 10
else
echo menor que 10
fi
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
24. Hands on...
while test -f /tmp/lock
do
echo “script travado...”
sleep 1
done
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
25. Hands on...
while :
do
if test -f /tmp/lock
then
echo “ainda em lock...”
sleep 1
else
break
fi
done
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
26. Prática...
- Receba dois números como parametro e
mostre a relação entre eles.
./relacao.sh 3 5
3 é menor que 5
- Recebe um número como parametro e o
diminui até chegar a zero mostrando cada
passo na tela
./zerador.sh 5
543210
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
27. Prática...
- Recebe 2 palavras como parametro e
verifica se a primeira esta na segunda. (grep)
./substring.sh ana banana
ana 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 l
olamundocruel
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
28. Prática...
- Do arquivo /etc/passwd, mostra usuário e o
nome completo, campos 1 e 5 separados por
um TAB. (cut)
./users.sh
ftpftp user
nobodynobody
lucas lucas souza fernandes
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
29. Prática...
- Do arquivo /etc/passwd, mostra todos os
shells (ultimo campo). (uniq)
./shell.sh
/bin/bash
/bin/false
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
30. Prática...
Mostra na tela todos os parametros recebidos
(shift)
./parametros a b c d e
Parametro 1 : a
Parametro 2 : b
Parametro 3 : c
Parametro 4 : d
Parametro 5 : e
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
31. Obrigado...
lucascoala@gmail.com
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners