Introdução ao Shell Script
O artigo traz uma introdução a Shell Script. Criaremos alguns exemplos práticos para que você consiga criar seus próprios scripts de automatização. Shell Script é um tipo de linguagem de programação que é utilizado por administradores Linux/Unix. Também é possível criar scripts para outros sistemas operacionais, mas o meu foco aqui será mesmo os sistemas Linux e suas distribuições.
[ Hits: 21.213 ]
Por: Renato Diniz Marigo em 15/02/2016 | Blog: http://www.renatomarigo.com.br
#!/bin/bash for line in $(cat lista.txt) do echo "$line" done
#!/bin/bash while read LINE do echo $LINE done < lista.txt
#!/bin/bash echo "Script criado para o uso Case" echo "Opção 1: Listar o conteúdo de /etc/init.d" echo "Opção 2: Listar o conteúdo de /etc/kernel" read OPCAO case $OPCAO in 1) echo "Você escolheu a opção $OPCAO " ls /etc/init.d;; 2) echo "Você escolheu a opção $OPCAO " ls /etc/kernel;; *)echo "Você não digitou uma opção válida";; esac
#!/bin/bash while true do clear; #Scripts e comandos done
#!/bin/bash while true do clear; echo "Script criado para o uso Case" echo "Opção 1: Listar o conteúdo de /etc/init.d" echo "Opção 2: Listar o conteúdo de /etc/kernel" echo "Opção 3: Sair" read OPCAO case $OPCAO in 1) echo "Você escolheu a opção $OPCAO " ls /etc/init.d sleep 3;; 2) echo "Você escolheu a opção $OPCAO " ls /etc/kernel sleep 3;; 3) echo "Você escolheu a opção $OPCAO " sleep 3 exit;; *)echo "Você não digitou uma opção válida" sleep 3;; esac done
#!/bin/bash if [ -e "/etc/teste" ] then echo "O diretório existe" else echo "O diretório não existe" fi
-eq Igual -ne Diferente -gt Maior -lt Menor -o Ou -d Se for um diretório -e Se existir -z Se estiver vazio -f Se conter texto -o Se o usuário for o dono -r Se o arquivo pode ser lido -w Se o arquivo pode ser alterado -x Se o arquivo pode ser executadoA seguir mostro a execução dele na tela:
#!/bin/bash printf "Deseja exibir estas informações /etc/init.d ?" read OPCAO if [[ $OPCAO == "S" || $OPCAO == "s" ]] then echo 'O conteúdo será exibido' ls /etc/init.d else echo 'O conteúdo não será exibido' echo "" fi
File operators: -a FILE True if file exists. -b FILE True if file is block special. -c FILE True if file is character special. -d FILE True if file is a directory. -e FILE True if file exists. -f FILE True if file exists and is a regular file. -g FILE True if file is set-group-id. -h FILE True if file is a symbolic link. -L FILE True if file is a symbolic link. -k FILE True if file has its `sticky' bit set. -p FILE True if file is a named pipe. -r FILE True if file is readable by you. -s FILE True if file exists and is not empty. -S FILE True if file is a socket. -t FD True if FD is opened on a terminal. -u FILE True if the file is set-user-id. -w FILE True if the file is writable by you. -x FILE True if the file is executable by you. -O FILE True if the file is effectively owned by you. -G FILE True if the file is effectively owned by your group. -N FILE True if the file has been modified since it was last read. FILE1 -nt FILE2 True if file1 is newer than file2 (according to modification date). FILE1 -ot FILE2 True if file1 is older than file2. FILE1 -ef FILE2 True if file1 is a hard link to file2. All file operators except -h and -L are acting on the target of a symbolic link, not on the symlink itself, if FILE is a symbolic link. String operators: -z STRING True if string is empty. -n STRING STRING True if string is not empty. STRING1 = STRING2 True if the strings are equal. STRING1 != STRING2 True if the strings are not equal. STRING1 < STRING2 True if STRING1 sorts before STRING2 lexicographically. STRING1 > STRING2 True if STRING1 sorts after STRING2 lexicographically.
Nagios 3 + NagiosQL no Ubuntu Server 12.04
VLAN Tagging nos sistemas GNU/Linux derivados do Red Hat
Instalação do Docker no CentOS 7
Programar em Shell - Script só para os baixinhos
Reconhecimento de Voz no Linux
Linguagem Lua - Introdução ao Aprendizado da Linguagem
Aprenda a Gerenciar Permissões de Arquivos no Linux
Como transformar um áudio em vídeo com efeito de forma de onda (wave form)
Como aprovar Pull Requests em seu repositório Github via linha de comando
Aplicativo simples para gravar tela
Quebra de linha na data e hora no Linux Mint
tentando instalar em um notebook antigo o Linux LegacyOS_2023... [RESO... (8)
Problema com Conexão Outlook via Firewall (OpenSUSE) com Internet Fibr... (5)