I need a script in bash that reads a file and recognize an delimiter (“;”) and store the values between the delimiters into variables to build a dialog menu later.
What i’ve done:
#!/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 that it reads is :
Minha Tarefa;/home/;/home/;Diferencial;;N;S; Minha Tarefa;/home/thalesg;/home/;Diferencial;;N;S;
And the output:
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
Advertisement
Answer
With a while
you can split a line into a lot of vars in 1 call.
You need to temporary change the FieldSep (IFS) into a ;
And avoid an extra call to cat
: do not use cat $file | ..
but use < $file
file="Tarefas.cfg" while IFS=";" read nomeTarefa dirOrigem dirDest tipoBkp agendarBkp compactarBkp gerarLog; do echo "$nomeTarefa $dirOrigem $dirDest $tipoBkp $agendarBkp $compactarBkp $gerarLog" echo "Show one field, the compactarBkp: $compactarBkp" done < $file