AdeEla
AdeEla

Reputation: 289

Apply command on many files from a directory and save output in file

I want to write a bash script to loop through all the files in the directory and calculate some scores. My directory has 100 files and I want to calculate the score of 1 with 2, 1 with 3, ... , 1 with 100, 2 with 3, 2 with 4, ... , 2 with 100, ... , 99 with 100 and save the results in a file. I already have the package that calculates the scores - score_function in the code below:

#!/bin/bash

for file1,file2 in ~/My_directory/*
do 
  while file2 > file1
  do
    score_function file1 file2
done >my_outputfile.txt

but I am not sure why this is not working, does anyone have any idea? The error that I get is:

line 9: syntax error: unexpected end of file

Upvotes: 1

Views: 97

Answers (1)

To begin with, you are having a syntax error since you are never closing a loop.

In bash you start loops with do and end them with done, however you have two does and just a single done, hence the syntax error.

Also, I'd recommend you to use two separate for loops (one for the first file, the other one for the other files) and you should go on fine.

Something like this (in pseudocode)

for comparingFile in directory; do
    for comparedFile in directory; do
        score=0
        if [[ "${comparingFile}" -ne "${comparedFile}" ]]; then
            score="$(score_function ${comparingFile} ${comparedFile})"
            echo "Compared ${comparingFile} to ${comparedFile}. Score: ${score}"
        fi
    done
done > output.txt

Upvotes: 2

Related Questions