Search code examples
arrayslinuxbashshellfilenames

Creating files using array items as filenames


I'm trying to create files given an ID and its respective Date. I want to use array items to set the name of these files. Problem is that the outcome is not what i'm expecting when creating the files. I would appreciate your help.

Desired output:

I want to create files on the following format:

FILE_300002_20170515.txt
FILE_500032_20170426.txt
FILE_400044_20170101.txt

Current output:

FILE_300002_400044.txt
FILE_300002_300002.txt
FILE_300002_500032.txt

File Sample:

This is the content of my input file:

300002,20170515,500032,20170426,400044,20170101

My code:

IFS=',' read -r -a array <<< "$input"   

ini=1
ID=0
Da=1
num="${#array[@]}"
let num=num/2

while [ $ini -le $num ];do
touch "/path/FILE_${array[Da]}_${array[ID]}.txt"
let ini=ini+1
let Da=Da+2
let ID=ID+2
done

****I've noticed that using only '/path/FILE_${array[ID]}.txt' on the filename, displays all the correct ID's, but when using both, '/path/FILE_${array[Da]}_${array[ID]}.txt' is messed up


Solution

  • Could do it like this

    IFS=',' read -r -a array <<< "$input"
    
    for((i=0;i<${#array[@]}-1;i+=2));do
    
        touch "/path/FILE_${array[i]}_${array[i+1]}.txt"
    
    done