Você está na página 1de 44

SHELL SCRIPT

Professor: Felipe Correia


SUMRIO

Introduo
Conceitos Bsicos
Editores
Entrada e Sada
Filtragem de Dados
Editores
Prticas
INTRODUO
INTRODUO

Shell script um programa de computador desenvolvido para ser


executado por um shell Unix, um interpretador de linha de
comando
Operaes tpicas de um shell script incluem manipulao de
arquivos, execuo de programas, e impresso de texto
INTRODUO

Shells tpicos
Korn shell (ksh)
Bourne shell (sh)
C Shell (csh)
Bourne Again Shell (bash)
Remote Shell (rsh)
Secure Shell for SSL telnet connections (ssh)
HELLO WORLD
HELLO WORLD

#!/bin/bash
# My first script
echo "Hello World!
HELLO WORLD

$ chmod 755 meu_script

$ ./meu_script.sh
HELLO WORLD

$ echo $PATH

$ export PATH=$PATH:diretrio

# mover arquivo para o diretrio /bin

meu_script
VARIVEIS
VARIVEIS

#!/bin/bash

NAME="Criptografia"
echo $NAME
VARIVEIS

Variveis Especiais
$0 - O nome do script atual.
$n - Correspondem aos argumentos de um script, em que n
um nmero natural positivo que corresponde posio do
argumento.
$# - O nmero de argumentos fornecidos a um script.
$* - Todos os argumentos so agrupados. Se um script recebe
dois argumentos $* equivalente a $1 $2.
$@ - Mesmo que o anterior.
$$ - O nmero do processo do shell atual
ARRAYS
ARRAYS

#!/bin/sh

NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo Primeiro ndice: ${NAME[0]}"
echo Segundo ndice: ${NAME[1]}"
OPERADORES
OPERADORES ARITMTICOS

Operador Descrio (ingls) Exemplo (ingls)

+ (Adio) Adds values on either side of the operator `expr $a + $b` will give 30

- (Subtrao) Subtracts right hand operand from left hand operand `expr $a - $b` will give -10

* (Multiplicao) Multiplies values on either side of the operator `expr $a \* $b` will give 200

/ (Diviso) Divides left hand operand by right hand operand `expr $b / $a` will give 2

% (Mdulo) Divides left hand operand by right hand operand and `expr $b % $a` will give 0
returns remainder

= (atribuio) a = $b would assign value of b


Assigns right operand in left operand
into a

== (igualdade) Compares two numbers, if both are same then [ $a == $b ] would return
returns true. false.

!= (desigualdade) Compares two numbers, if both are different then [ $a != $b ] would return true.
returns true.
OPERADORES RELACIONAIS
Operador Descrio (ingls) Exemplo (ingls)

-eq Checks if the value of two operands are equal or not; if [ $a -eq $b ] is not true.
yes, then the condition becomes true.

-ne Checks if the value of two operands are equal or not; if


values are not equal, then the condition becomes true. [ $a -ne $b ] is true.

-gt Checks if the value of left operand is greater than the


value of right operand; if yes, then the condition [ $a -gt $b ] is not true.
becomes true.

-lt Checks if the value of left operand is less than the value
of right operand; if yes, then the condition becomes [ $a -lt $b ] is true.
true.

-ge Checks if the value of left operand is greater than or


equal to the value of right operand; if yes, then the [ $a -ge $b ] is not true.
condition becomes true.

-le Checks if the value of left operand is less than or equal


to the value of right operand; if yes, then the condition [ $a -le $b ] is true.
becomes true.
OPERADORES RELACIONAIS

Operador Descrio (ingls) Exemplo (ingls)

! This is logical negation. This inverts a


true condition into false and vice [ ! false ] is true.
versa.
-o This is logical OR. If one of the [ $a -lt 20 -o $b -gt
operands is true, then the condition 100 ] is true.
becomes true.
-a This is logical AND. If both the
[ $a -lt 20 -a $b -gt
operands are true, then the condition
100 ] is false.
becomes true otherwise false.
OPERADORES DE STRING
Operador Descrio (ingls) Exemplo (ingls)

= Checks if the value of two operands are [ $a = $b ] is not true.


equal or not; if yes, then the condition
becomes true.

!= Checks if the value of two operands are [ $a != $b ] is true.


equal or not; if values are not equal then
the condition becomes true.

-z Checks if the given string operand size is [ -z $a ] is not true.


zero; if it is zero length, then it returns
true.

-n Checks if the given string operand size is [ -n $a ] is not false.


non-zero; if it is nonzero length, then it
returns true.
str Checks if str is not the empty string; if it [ $a ] is not false.
is empty, then it returns false.
CONTROLE DE FLUXO
IFELSE
CONTROLE DE FLUXO
#!/bin/sh
# This is some secure program that uses security.

VALID_PASSWORD="secret" #this is our password.

echo "Please enter the password:"


read PASSWORD

if [ "$PASSWORD" == "$VALID_PASSWORD" ]; then


echo "You have access!"
else
echo "ACCESS DENIED!"
fi
CASE
CASE
#!/bin/sh

echo "Enter a number between 1 and 5. "


read NUM

case $NUM in
1) echo "one" ;;
2) echo "two" ;;
3) echo "three" ;;
4) echo "four" ;;
5) echo "five" ;;
*) echo "INVALID NUMBER!" ;;
esac
LAOS
FOR
FOR

for VARIAVEL in VALOR_1,VALOR_2 VALOR_N;


do
AES
done
FOR

#!/bin/bash

echo Testando o loop for

for i in {10..0};
do
echo $i
done
FOR

#!/bin/bash

echo Testando o comando seq

for i in $(seq 1 5 100);


do
echo $i
done
FOR

#!/bin/bash

echo Testando o comando seq

for i in $(seq 1 100);


do
echo $i
done
WHILE
FOR

while [ CONDICAO ];
do
AES
done
FOR

#!/bin/bash
echo Informe o que voc quiser, -1 para sair
read dado;
while [ $dado != -1 ];
do
echo Voc digitou $dado
read dado;
done
FOR

#!/bin/bash
echo Informe at que valor positivo e maior que zero contar:
read valor;
i=1
while [ $i -le $valor ];
do
echo $i
((i=$i+1))
done
FUNES
FUNES

nome_funcao()
{
AES
}
FUNES
#!/bin/sh

# Define your function here


Hello () {
echo "Hello World"
}

# Invoke your function


Hello
FUNES
#!/bin/sh

# Define your function here


Hello () {
echo "Hello World $1 $2"
}

# Invoke your function


Hello Zara Ali
FUNES
#!/bin/sh

Hello () {
echo "Hello World $1 $2"
return 10
}
Hello Zara Ali
# Capture value returnd by last command
ret=$?
echo "Return value is $ret"
EDITORES
EDITORES

nano

vim

atom
PRTICAS
QUEBRA DE SENHA
DEFESA

Você também pode gostar