Read TXT file and convert certain values into variables

0

I have to compare the sizes of some folders to make a% difference between them and depending on the result send an email. I want to make the for for each SITE folder, the SITE folders are all from a directory, ... / backup / SITE1..2..3. My intention is when SITE1 ends use the same variables for SITE2.

The part of taking folder sizes and extracting the% and sending mail separately for each folder I have already solved, but I would like to simplify the code and for example have a txt file with the folder routes, which read it up to one character and use the variables, when it ends up with the next SITE etc

    :SITE1
    Carpeta1
    Carpeta1.bkp
    Carpeta2
    Carpeta2.bkp

    :SITE2
    Carpeta1
    Carpeta1.bkp
    Carpeta2
    Carpeta2.bkp

I want to assign the values of Folder1, ... to variables until I find ":", at that moment execute a command and when I finish I continue reading SITE2 and reassign its variables

Thank you!

    
asked by Juan Moreno 09.08.2018 в 16:42
source

2 answers

0

I wrote the following script, I think it can help you, you just need to adapt it to what you need. Greetings.

comando=0
for linea in $(cat prueba.txt)
do
    if [[ $linea == :* ]]
    then
        comando=$((comando+1));
        echo    "EJECUTAR COMANDO"
        if [ $comando -eq 1 ]
        then
            echo "GUARDAR VARIABLES"
        fi
    fi

done

    
answered by 09.08.2018 в 17:17
0

You do not explain yourself very well or I am the one who does not understand you. I understood that you want:

  • Read all folders in one go from site to site.
  • Apply a function to all folders.
  • I also understand that the format of the file where you have the folders is arbitrary and you set the one you want. So I would eliminate the colon and simply, I would separate one site from another with a blank line (if you want to keep the two points there is no problem either.

    I would solve it like this ::

    awk -F '\n' -v RS='\n\n' '{for(i=1;i<=NF;i++) $i="\""$i"\""; print}' listado.txt | \
    while read -r sitio; do
        eval set -- $sitio
        manipulacion "$@"
    done
    

    And that's it: "manipulation" is the function that you already have prepared according to accounts and that receives as the first argument the site (SITE1) and as other arguments the folders. I have complicated the code a bit to allow the folders (and the site) to have spaces. If you wanted to keep the two points in the file you would have to modify the loop a bit after the "set" ::

    sitio=${1#:}
    shift
    manipulacion "$sitio" "$@"
    
        
    answered by 16.08.2018 в 19:36