Created
December 29, 2016 05:10
Star
You must be signed in to star a gist
ShellScript Tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Este é meu primeiro script | |
cd / | |
ls -l | |
echo "Esta foi a listagem de arquivos e diretórios dentro da raiz" | |
echo -n " O usuário logado no momento é " | |
whoami |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Informações do usuário | |
echo "Usuário: $USER" | |
echo "Diretório home: $HOME" | |
echo "UID do usuário: $UID" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Teste de Variáveis | |
idade=25 | |
nome="Mônica" | |
echo "A $nome tem $idade anos de idade." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Teste de variáveis | |
echo "|-----------------------------------|" | |
echo "| O usuário $USER tem o UID $UID |" | |
echo "|-----------------------------------|" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Copiar a listagem de um diretório para arquivos únicos, de acordo com data e hora. | |
#A variável "arq" recebe o valor de saída da variável de ambiente "date" com especificações de atributos %d=dia %m=mês %y=ano %H=hora %M=minuto | |
arq=`date +%d%m%y%H%M` | |
#o comando ">" redireciona o resultado p/ dentro de um arquivo,">"=sobrescreve ">>"=acrescenta ao final do arquivo | |
ls -la /home/jprox000 > log.$arq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Cálculo cimples no bash | |
var1=10 | |
var2=5 | |
var3=2 | |
var4=$[$var1 * ($var2 - $var3)] | |
echo "O resultado do cálculo é $var4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Verificar se o usuário HOME do úsuario exite e mostrar o conteúdo | |
# Verifica se HOME existe e se é um diretório | |
if [ -d $HOME ] | |
then | |
echo "Seu diretório existe e o conteúdo é:" | |
cd $HOME | |
ls -m | |
else | |
echo "Diretório não encontrado" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Verificar se um objeto é um arquivo | |
# Verifica se o HOME é um diretório ou objeto | |
if [ -e $HOME ] | |
then | |
echo "O objeto existe. Vamos ver se é arquivo ou diretório" | |
# If dentro de outro, verifica se HOME é um arquivo | |
if [ -f $HOME ] | |
then | |
echo "É um arquivo!" | |
else | |
echo "É um diretório" | |
fi | |
else | |
echo "Objeto não encontrado" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Verifica permição de leitura em um arquivo | |
arquivo=/etc/passwd | |
# Verifica se o arquivo existe | |
if [ -f $arquivo ] | |
then | |
echo "Arquivo existe! Testar se usuário tem permição p/ leitura" | |
# Verifica se o arquivo possui permissão de leitura p/ usuário | |
if [ -r $arquivo ] | |
then | |
echo "Possui permição de leitura; mostrando as 5 últimas linhas:" | |
# tail -5 -> Mostra as 5 ultimas linhas do conteúdo do arquivo | |
tail -5 $arquivo | |
else | |
echo "Sem permição p/ leitura" | |
fi | |
else | |
echo "Arquivo não encontrado" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
var1=20 | |
var2=5 | |
var3=`bc << MARQ | |
scale=3 | |
x = ($var2 / $var1) | |
x | |
MARQ | |
` | |
echo "Resultado:$var3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Uasando o comando case | |
local=banheiro | |
case $local in | |
quarto) | |
echo "Você está no quarto";; | |
cozinha) | |
echo "Aqui é a cozinha";; | |
sala) | |
echo "Está na sala agora";; | |
banheiro | lavanderia) | |
echo "Foi ao banheiro ou à lavanderia";; | |
*) | |
echo "Você não está dentro de casa";; | |
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Teste de parâmetros na linha de comandos | |
echo "Programa que calcula o quadrado de um número" | |
quadrado=$[ $1*$1 ] | |
echo "O quadrado de $1 é $quadrado" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Verificação de número de parâmetros | |
if [ $# -ne 1 ] | |
then | |
echo "Digite ao menos um valor" | |
else | |
result=$[ $1 * 3 ] | |
echo "O triplo de $1 é $result" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Testar comparações compostas. Verificar se o usário logado atualmente é o jprox000 e se ele tem permissão de escrita no arquivo .bashrc | |
# [Usuário é igual a jprox000] e [se o usuário tem permissão p/ alterar o arquivo .bashrc dentro de HOME] | |
if [ $USER=jprox000 ] && [ -w $HOME/.bashrc ] | |
then | |
echo "O usuário $USER tem permissão p/ alterar o arquivo" | |
else | |
echo "O usuário $USER não pode alterar o arquivo" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Teste de if-then aninhado | |
var1="jprox000" | |
var2="Akira" | |
if ls /$var1 | |
then | |
echo "Diretório do usuário $var1 encontrado!" | |
elif ls /$var2 | |
then | |
echo "Diretório do usuário $var2 é que foi encontrado!" | |
else | |
echo "Nenhum dos dois diretórios foi encontrado!" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
var1=10 | |
var2=2 | |
var3=$[$var1 * $var2] | |
echo $var3 | |
exit 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
echo "Temos:" | |
for lista in Laranja Abacaxi Cajá Melancia | |
do | |
echo "$lista" | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Arquivo arq1 contém uma lista de nomes, um por linha, incluindo nomes compostos. | |
arquivo=/home/jprox000/arq1 | |
IFSOLD=$IFS | |
IFS=$'\n' | |
for nome in `less $arquivo` | |
do | |
echo "O nome é: $nome" | |
done | |
IFS=$IFSOLD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Iterando por todos os itens de um diretório, indica qual é arquivo e qual é diretório | |
# O '*' indica que terá que verificar todos diretórios do usuário | |
for item in /home/jprox000/* | |
do | |
# Verifica se o arquivo é um diretório | |
if [ -d "$item" ] | |
then | |
echo "O $item é um diretório" | |
# Verifica se é um arquivo | |
elif [ -f "$item" ] | |
then | |
echo "O item $item é um arquivo" | |
fi | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
for (( i=0; i<=15; i++ )) | |
do | |
echo "Número: $i" | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Condicional Simple em um script | |
if cd /xyz | |
then | |
echo "Diretório raiz encontrado" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Teste condicional composto | |
if ls /home/jprox000/testee | |
then | |
echo "Diretório Encontrado!" | |
else | |
echo "Diretório não Existe!" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ -n "$1" ] | |
then | |
while [ -n "$1" ] | |
do | |
case "$1" in | |
a) echo "Opção selecionada: A";; | |
b) echo "Opção selecionada: B";; | |
c) echo "Opção selecionada: C";; | |
*) echo "Opção incorreta";; | |
esac | |
shift | |
done | |
else | |
echo " " | |
echo "ERRO: Para rodar o script corretamente, entre com o parâmetro da opção desejada:" | |
echo " " | |
echo "'a' p/ escolher a opção 'A'" | |
echo "'b' p/ escolher a opção 'B'" | |
echo "'c' p/ escolher a opção 'C'" | |
echo " " | |
echo "Ex: ./opção.sh a" | |
echo " " | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
printf "Digite seu nome: " | |
read nome | |
echo "Seu nome é $nome" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
echo Digite seu nome: | |
if read -t 4 nome | |
then | |
echo "Olá $nome, tudo bem por aí?" | |
else | |
echo "Você demorou mais de 4 segundos para responder!" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
i=1 | |
while [ -n "$1" ] | |
do | |
echo "O parâmetro $i tem o valor: $1" | |
i=$[ $i + 1 ] | |
shift | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Testar igualdade de strings | |
nome=jprox000 | |
if [ $USER = $nome ] | |
then | |
echo "Olá $nome" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Testar a não-igualdade entre strings | |
fruta=morango | |
if [ $fruta != laranja ] | |
then | |
echo "A fruta não é uma laranja, e sim uma $fruta" | |
else | |
echo "A fruta é $fruta!" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Testar se a variável possui conteúdo | |
var1=abacaxi | |
var2='' | |
# Verificando se var1 contem algum valor | |
if [ -n $var1 ] | |
then | |
echo "Variável não está vázia, contém o valor $var1" | |
fi | |
# Verificando se var2 não contém algum valor | |
if [ -z $var2 ] | |
then | |
echo "Variável vázia!" | |
else | |
echo "String não está vázia" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
var1=100 | |
var2=30 | |
var3=`echo "scale=4; $var1/$var2" | bc` | |
echo "Resultado: $var3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Comparação Númericas com test e if-then | |
var1=7 | |
var2=7 | |
# Verifica se var1 é maior do que 8 | |
if [ $var1 -gt 8 ] | |
then | |
echo "A variável de maior valor $var1 é maior que 8" | |
fi | |
# Verifica se var1 é igual a var2 | |
if [ $var1 -eq $var2 ] | |
then | |
echo "Os valores são iguais" | |
else | |
echo "Os valores são diferentes" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
var=50 | |
until [ $var -eq 0 ] | |
do | |
echo $var | |
var=$[ $var-2 ] | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#bin/bash | |
var=1 | |
# Enquanto var for maior que 0, será mostrado o valor de var | |
while [ $var -le 100 ] | |
do | |
echo $var | |
# Cada vez que o loop for executado, var será decrementado com -1 | |
var=$[ $var +1 ] | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment