Receive file lines, and treat them (BASH)

Asked

Viewed 1,591 times

1

I need a bash script that will read a file, recognize the delimiter (in case ";") and store the values that are in the delimiters in variables, to later mount a menu with the dialog...

What I’ve done so far is :

#!/bin/bash
file="Tarefas.cfg"
nomeTarefa=''
dirOrigem=''
dirDest=''
tipoBkp=''
agendarBkp=''
compactarBkp=''
gerarLog=''
echo
for linha in $(cat $file)
do
    nomeTarefa=$(echo $linha | cut -d\; -f1 )
    dirOrigem=$(echo $linha | cut -d\; -f2 )
    dirDest=$(echo $linha | cut -d\; -f3 )
    tipoBkp=$(echo $linha | cut -d\; -f4 )
    agendarBkp=$(echo $linha | cut -d\; -f5 )
    compactarBkp=$(echo $linha | cut -d\; -f6 )
    gerarLog=$(echo $linha | cut -d\; -f7 )
    echo "$nomeTarefa $dirOrigem $dirDest $tipoBkp $agendarBkp $compactarBkp $gerarLog"
    echo
done

the file he reads, is as follows:

Minha Tarefa;/home/;/home/;Diferencial;;N;S;
Minha Tarefa;/home/thalesg;/home/;Diferencial;;N;S;

The output is as follows::

Minha Minha Minha Minha Minha Minha Minha

Tarefa /home/ /home/ Diferencial  N S

Minha Minha Minha Minha Minha Minha Minha

Tarefa /home/thalesg /home/ Diferencial  N S

2 answers

1

Excuse me: this is more a set of comments than an answer...

As an alternative to what you wrote, I suggested:

  • use of while read varinstead of the for (because of the spacing)
  • use of arrays

(It would probably be better to write this type of scripts in perl, python, etc)

#!/bin/bash
file="Tarefas.cfg"    
cat $file | while read linha 
do
  arr=(${linha//;/ })                # split por ";"

  echo "==> ${arr[1]} ${arr[2]} "
done
  • Hello friend, I forgot to comment that I managed to solve the problem... I will post the answer

1


The code below solved my problem:

#!/bin/bash

file="Tarefas.cfg"
count=0;
declare arrNomeTarefa;
declare arrDirOrigem;
declare arrDirDest;
declare arrTipoBkp;
declare arrAgendarBkp;
declare arrCompactarBkp;
declare arrGerarLog;

function carregaTarefas {
    while IFS=";" read nomeTarefa dirOrigem dirDest tipoBkp agendarBkp compactarBkp gerarLog || [[ -n "$gerarLog" ]]; do #RECEBE NAS VARS OS VALORES DELIMITADOS POR ;
        count=$((count + 1)); #INICIA O COUNT PARA INCREMENTAR O OPTIONS
        options[$count]=$count" $nomeTarefa" #CONCATENA O OPTIONS

        arrNomeTarefa[$count]="$nomeTarefa"
        arrDirOrigem[$count]="$dirOrigem"
        arrDirDest[$count]="$dirDest"
        arrTipoBkp[$count]="$tipoBkp"
        arrAgendarBkp[$count]="$agendarBkp"
        arrCompactarBkp[$count]="$compactarBkp"
        arrGerarLog[$count]="$gerarLog"
    done < $file ##END READ FILE
}

function criaLista {
    options=(${options[@]}) #STRING DINAMICA COM OS NOMES DAS TAREFAS
    cmd=(dialog --keep-tite --menu "Select options:" 22 76 16) #COMANDO DE CRIAÇÃO DA DIALOG
    choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty) #EXECUÇÃO DA DIALOG  

}
  • @Tiauris, the script is very interesting (+1) -- reading the question was not easy to understand what you were looking for. What is the "22 76 16"?

  • @J Joao are Width x Height x parameters "something I can’t remember at the moment" that adjust the command --dialog

Browser other questions tagged

You are not signed in. Login or sign up in order to post.