While and if : Change the while and if checks to varieveis

Asked

Viewed 43 times

0

I am creating a script for automation of a process, but in the script I have to read a directory and insert the content (filename) in a text file and in the while read each line to process the files. The question is,?

Script:

# Declaração de variáveis e arquivos
DIRETORIO="/pasta_alvo"
> $DIRETORIO/arq.txt # limpo os arquivos do processo
> $DIRETORIO/trt.txt

ARQUIVO=$(ls -1 $DIRETORIO | egrep "^AF" | egrep ".rar$") # arquivos
ls -1 $DIRETORIO | egrep "^AF" | egrep ".rar$" > /$DIRETORIO/arq.txt

if [[ -s $DIRETORIO/arq.txt ]]; then
     while IFS='' read -r line; do
     unrar x $DIRETORIO$line $DIRETORIO ; done < /$DIRETORIO/arq.txt

#  mudar para variável a verificação de conteúdo do if(se tem conteúdo)
if [[ -s variável ]]; then 

# while IFS='' read -r line; do
      unrar x $DIRETORIO$line $DIRETORIO ; done < variavel

I have tried to change to variable following some tutorials but was unsuccessful.

  • How you tried to edit the variable?

  • I tried it like this: VAR1=FILE=$(ls -1 $DIRECTORY | egrep " AF" | egrep ".rar$") and tried to use : if [[ -s $VAR1 ]]; then

  • @Kleudysilva O -s that you are using on if, serves to verify if a file exists, ie, will not work with a variable because the code will take the contents of this variable and check if it is a file.

  • @Valdeirpsr o -s checks if there is content inside the file, I thought I could use the same concept with the variable

  • The intention to use variable was not to leave files on the servers, however I decided that this is not a big problem, as only I would need to remove the control files at the end of the script. so files will be created at the beginning of the script and at the end of the execution will be removed. Thank you to those who tried to help.

No answers

Browser other questions tagged

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