Converte permissões de octal para textual e vice-versa

Publicado por Rafael Ancinelo Adolpho (última atualização em 30/06/2014)

[ Hits: 5.285 ]

Download converte_permissoes.sh

Download 1403705672.converte_permissoes.sh (versão 2)




Shell Script que faz a conversão de permissões no formato OCTAL para TEXTUAL e vice-versa.

  



Versões atualizadas deste script

Versão 2 - Enviado por Vitor Augusto M. Pio em 25/06/2014

Changelog: Script com o mesmo propósito, porém com a lógica mais bem definida e código mais sucinto.

Download 1403705672.converte_permissoes.sh


Esconder código-fonte

#!/bin/bash
#Autor radolpho 2012
echo " Escolha com opcao voce deseja (a ou b)
a) Octal para texto
b) Texto para octal "
read resp

if [ $resp == a ] ;then #Conversao de OCTAL para TEXTUAL
   echo "Digite a permissao em  OCTAL Ex. 776"
   read poct

   #Permissao de user
   if [ ${poct:0:1} == 0 ] ;then
      u="---"   
   elif [ ${poct:0:1} == 1 ] ;then
      u="--x"
   elif [ ${poct:0:1} == 2 ] ;then
      u="-w-"
   elif [ ${poct:0:1} == 3 ] ;then
      u="-wx"
   elif [ ${poct:0:1} == 4 ] ;then
      u="r--"
   elif [ ${poct:0:1} == 5 ] ;then
      u="r-x"
   elif [ ${poct:0:1} == 6 ] ;then
      u="rw-"
   elif [ ${poct:0:1} == 7 ] ;then
      u="rwx"
   fi

   #Permissao de group
        if [ ${poct:1:1} == 0 ] ;then
                g="---"   
        elif [ ${poct:1:1} == 1 ] ;then
                g="--x"
        elif [ ${poct:1:1} == 2 ] ;then
                g="-w-"
        elif [ ${poct:1:1} == 3 ] ;then
                g="-wx"
        elif [ ${poct:1:1} == 4 ] ;then
                g="r--"
        elif [ ${poct:1:1} == 5 ] ;then
                g="r-x"
        elif [ ${poct:1:1} == 6 ] ;then
                g="rw-"
        elif [ ${poct:1:1} == 7 ] ;then
                g="rwx"
        fi


   #Permissao de other
        if [ ${poct:2:1} == 0 ] ;then
                o="---"
        elif [ ${poct:2:1} == 1 ] ;then
                o="--x"
        elif [ ${poct:2:1} == 2 ] ;then
                o="-w-"
        elif [ ${poct:2:1} == 3 ] ;then
                o="-wx"
        elif [ ${poct:2:1} == 4 ] ;then
                o="r--"
        elif [ ${poct:2:1} == 5 ] ;then
                o="r-x"
        elif [ ${poct:2:1} == 6 ] ;then
                o="rw-"
        elif [ ${poct:2:1} == 7 ] ;then
                o="rwx"
        fi

   echo "Conversao de OCTAL para TEXTUAL\n"
   echo $poct" >>>> "$u$g$o

elif [ $resp == b ] ;then #Conversao de TEXTUAL para OCTAL

   echo "Digite a permissao em TEXTUAL Ex. rwxrw--w-"
   read ptxt
   
   #Permissao de user
        if [ ${ptxt:0:3} == "---" ] ;then
                u="0"   
        elif [ ${ptxt:0:3} == "--x" ] ;then
                u="1"
        elif [ ${ptxt:0:3} == "-w-" ] ;then
                u="2"
        elif [ ${ptxt:0:3} == "-wx" ] ;then
                u="3"
        elif [ ${ptxt:0:3} == "r--" ] ;then
                u="4"
        elif [ ${ptxt:0:3} == "r-x" ] ;then
                u="5"
        elif [ ${ptxt:0:3} == "rw-" ] ;then
                u="6"
        elif [ ${ptxt:0:3} == "rwx" ] ;then
                u="7"
        fi

    #Permissao de group
        if [ ${ptxt:3:3} == "---" ] ;then
                g="0"
        elif [ ${ptxt:3:3} == "--x" ] ;then
                g="1"
        elif [ ${ptxt:3:3} == "-w-" ] ;then
                g="2"
        elif [ ${ptxt:3:3} == "-wx" ] ;then
                g="3"
        elif [ ${ptxt:3:3} == "r--" ] ;then
                g="4"
        elif [ ${ptxt:3:3} == "r-x" ] ;then
                g="5"
        elif [ ${ptxt:3:3} == "rw-" ] ;then
                g="6"
        elif [ ${ptxt:3:3} == "rwx" ] ;then
                g="7"
        fi


   #Permissao de other
        if [ ${ptxt:6:3} == "---" ] ;then
                o="0"   
        elif [ ${ptxt:6:3} == "--x" ] ;then
                o="1"
        elif [ ${ptxt:6:3} == "-w-" ] ;then
                o="2"
        elif [ ${ptxt:6:3} == "-wx" ] ;then
                o="3"
        elif [ ${ptxt:6:3} == "r--" ] ;then
                o="4"
        elif [ ${ptxt:6:3} == "r-x" ] ;then
                o="5"
        elif [ ${ptxt:6:3} == "rw-" ] ;then
                o="6"
        elif [ ${ptxt:6:3} == "rwx" ] ;then
                o="7"
        fi

        echo "Conversao de TEXTUAL para OCTAL\n"
        echo $ptxt" >>>> "$u$g$o

fi

Scripts recomendados

Backup de arquivos importantes e MySQL

verifica_rede

Agenda de contatos em DIALOG

Script para Backup Banco Firebird

Script Admin PDC


  

Comentários
[1] Comentário enviado por vitinho217 em 25/06/2014 - 11:13h

O script ficou muito grande, existe uma lógica que pode incurta-lo.


Contribuir com comentário