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.190 ]
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.
Zabbix Server 2.0 no CentOS - Instalação e configuração
Instalação do Fail2Ban no CentOS 7
Debian Linux 4 iniciando o Windows como terminal server
Programando em shell script (conceitos básicos)
Explorando a entrada de dados com READ em Bash Shell
Personalize seu shell na entrada de seu logon
Backup automático em Shell Script
Como aprovar Pull Requests em seu repositório Github via linha de comando
Como gerar um podcast a partir de um livro em PDF
Automatizando digitação de códigos 2FA no browser
Resolver problemas de Internet
Como compartilhar a tela do Ubuntu com uma Smart TV (LG, Samsung, etc.)
Resolvendo o erro "libQt6Widgets.so.6: cannot open shared object file" no Linux
Como instalar protetores de tela (Debian e derivados)
Conheça a 4Devs, caixa de ferramentas online para desenvolvedores
Como converter um vídeo MP4 para um GIF para publicar no README.md do seu repositório Github
Copia e Cola no SED --> Processador de textos (3)