0

I have a script that processes files based on drop folders, I created a function to do the processing and based on a set of variables I process a folder

funtion filedetect {
//
<some processing code code>
//
}

folder=1
source="/dir_1/"
reciep="me@mo.ma"
filedetect    

folder=2
source="/dir_2"
reciep="ma@me.mo"
filedetect

Now I would like to add a peace of code to create 1 text file with a counter that basically counts how many files are found and processed in each folder. That is why I added the variable 'folder' so that the text file contains something like:

FOLDER 1 = [count]
FOLDER 2 = [count]
etc.

But for that I need to read by line the previous 'count' and replace that with count=count+1

how do I read the correct line based on the text file.

2

Assuming you've started with something like this

folder1=10
folder2=4
folder3=7

You can write it out with a command such as this

set | grep -E '^folder[0-9]+=' > counter.txt

To read it back in again you can simply source the file

source counter.txt

If you have a shell that handles arrays (bash, zsh) you can index the set of folders to an arbitrarily large number

folder[1]=10
folder[2]=4
folder[3]=7

And write this one variable array out with

set | grep '^folder=' > counter.txt

Read it back in using source, as above.

For arrays you would reference them like this echo "${folder[1]}" or foreach f "${folder[@]}"; do ... done. Or even this if your index values are in strict ascending order from 1:

i=1
while [[ $i -le ${#folder[*]} ]]
do
    echo "$i => ${folder[$i]}"
    ((i++))
done
  • This I will try to add into the script. – SHLelieveld Dec 10 '18 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.