Ferramentas do usuário

Ferramentas do site


infra-estrutura:linux:shellscripts_variados

Shellscripts diversos e variados

Vou colocar aqui os shellscript's que desenvolvo ou que encontrei de amigos e considero muito útil. Quero deixar para caso alguém precise vou deixar documentando o código.

Em uma listagem de arquivos de backup, script coleta campos da data e utiliza para remover o arquivo de certa data informada

root@aquiles:# ls
bck_imanager0101180100.zip  bck_imanager2012170100.zip  bck_imanager2709160100.zip              users_groups_42_imanager0912170100.zip  users_groups_42_imanager2407170100.zip
bck_imanager0102180100.zip  bck_imanager2101170100.zip  bck_imanager2709170100.zip              users_groups_42_imanager1001180100.zip  users_groups_42_imanager2408160100.zip
bck_imanager0103180100.zip  bck_imanager2101180100.zip  bck_imanager2710160100.zip              users_groups_42_imanager1002180100.zip  users_groups_42_imanager2408170100.zip
bck_imanager0104180100.zip  bck_imanager2102170100.zip  bck_imanager2710170100.zip              users_groups_42_imanager1003180100.zip  users_groups_42_imanager2409160100.zip
bck_imanager0105180100.zip  bck_imanager2102180100.zip  bck_imanager2711160100.zip              users_groups_42_imanager1004180100.zip  users_groups_42_imanager2409170100.zip
bck_imanager0106180100.zip  bck_imanager2103170100.zip  bck_imanager2711170100.zip              users_groups_42_imanager1005180100.zip  users_groups_42_imanager2410160100.zip
bck_imanager0107180100.zip  bck_imanager2103180100.zip  bck_imanager2712160100.zip              users_groups_42_imanager1006180100.zip  users_groups_42_imanager2410170100.zip
bck_imanager0201180100.zip  bck_imanager2104170100.zip  bck_imanager2712170100.zip              users_groups_42_imanager1007180100.zip  users_groups_42_imanager2411160100.zip
bck_imanager0202180100.zip  bck_imanager2104180100.zip  bck_imanager2801170100.zip              users_groups_42_imanager1011170100.zip  users_groups_42_imanager2411170100.zip
bck_imanager0203180100.zip  bck_imanager2105160100.zip  bck_imanager2801180100.zip              users_groups_42_imanager1012170100.zip  users_groups_42_imanager2412160100.zip
bck_imanager0204180100.zip  bck_imanager2105170100.zip  bck_imanager2802170100.zip              users_groups_42_imanager1101180100.zip  users_groups_42_imanager2412170100.zip
bck_imanager0205180100.zip  bck_imanager2105180100.zip  bck_imanager2802180100.zip              users_groups_42_imanager1102180100.zip  users_groups_42_imanager2501170100.zip
bck_imanager0206180100.zip  bck_imanager2106160100.zip  bck_imanager2803170100.zip              users_groups_42_imanager1103180100.zip  users_groups_42_imanager2501180100.zip
bck_imanager0207180100.zip  

Vamos pegar um arquivo e analisar o modelo

bck_imanager0912170100.zip

queremos excluir todos os arquivos do ano de 2017 então vamos extrair o “17” da parte “0912170100” e depois fazer um teste e caso encontre o valor igual a 17, remova :)

Eis o script

remove.sh
#!/bin/bash
 
# criamos uma variavel chamada ARQUIVO para o script ficar portavel
ARQUIVO="bck_imanager"
# listamos todos os arquivos que começam com o o conteudo da variavel ARQUIVO
FILES=$(ls ${ARQUIVO}*)
 
# Executo um loop varrendo todos os arquivos extraindo o ano e dia e mes coloquei o dia e mes para tentar ser didatico
for i in ${FILES}
do
        # aqui utilizamos um recurso do bash substring o nome da variavel e i
        # entao queremos no começo da string pegar a substring começando no digito 12
        # ou seja excluindo a string "bck_imanager"
        # e na parte final excluir -4 digitos excluido o ".zip" no final ficaremos somente com a porção de data
        AUX=$(echo ${i:12:-4})
        # aqui aplicamos o mesmo com a substring andando com os digitos e pegando DIA
        DIA=$(echo ${AUX:0:-8})
        # pegando MES
        MES=$(echo ${AUX:2:-6})
        # e finalmente pegando o ANO
        ANO=$(echo ${AUX:4:-4})
        # testamos se o ano e 17 se for 
        # vamos entrar no if e remover os arquivos desde que temos o nome do arquivo na variavel i
        if [ ${ANO} == 17 ]
        then
                echo "Removendo arquivo: ${i}"
                rm ${i}
        fi
done

Discussão

Insira seu comentário. Sintaxe wiki é permitida:
 
infra-estrutura/linux/shellscripts_variados.txt · Última modificação: 2018/07/23 15:54 por ricardobarbosams