Alexis King
Alexis King

Reputation: 43842

Loop through lines in file for input, then output files with unique names?

I'm using a tool that converts file types, but only works on one file at a time. I've put the URLs to these files into a text file, which I want to loop through with bash. The command then outputs files, but I want each file to be named uniquely. For example, output file one should be named output1.pdf, two should be output2.pdf, etc.

Here's what I have so far:

for i in `cat input.txt`; do
    converttopdf $i output.pdf
done

But that will simply overwrite output.pdf over and over. How can I make it output a unique file each time?

Upvotes: 0

Views: 859

Answers (2)

c00kiemon5ter
c00kiemon5ter

Reputation: 17604

num=0
while read -r; do
    ((num++))
    converttopdf "$REPLY" "output_${num}.pdf"
done < file_with_urls

keep a counter, and place in the output filename

Upvotes: 2

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272467

With a counter?

c=1
echo $c     # "1"
c=$(($c+1))
echo $c     # "2"

Upvotes: 0

Related Questions