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.445 ]
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.
Debian Linux 4 iniciando o Windows como terminal server
Tutorial de instalação do Squid no CentOS
Zabbix Server 2.0 no Ubuntu Server 12.04 - Instalação e configuração
Instalação do Fail2Ban no CentOS 7
Backup automático em Shell Script
Servidor de Backup com Ubuntu Server 24.04 LTS, RAID e Duplicati (Dell PowerEdge T420)
Visualizar câmeras IP ONVIF no Linux sem necessidade de instalar aplicativos
Atualizar Debian Online de uma Versão para outra
Instalar driver Nvidia no Debian 13
Redimensionando, espelhando, convertendo e rotacionando imagens com script
Debian 13 Trixie para Iniciantes
Convertendo pacotes DEB que usam ZSTD (Padrão Novo) para XZ (Padrão Antigo)
Debian 13 "trixie&... lançado! (44)
Rust é o "C da nossa geração"? (3)
Gerenciador de arquivos é finalizado ao abrir pasta específica (2)