Testing360
Testing360

Reputation: 101

Obtaining file names from directory in Bash

I am trying to create a zsh script to test my project. The teacher supplied us with some input files and expected output files. I need to diff the output files from myExecutable with the expected output files.

Question: Does $iF contain a string in the following code or some kind of bash reference to the file?

#!/bin/bash
inputFiles=~/project/tests/input/*
outputFiles=~/project/tests/output

for iF in $inputFiles
do
./myExecutable $iF > $outputFiles/$iF.out
done

Note:

Any tips in fulfilling my objectives would be nice. I am new to shell scripting and I am using the following websites to quickly write the script (since I have to focus on the project development and not wasting time on extra stuff):

Grammar for bash language

Begginer guide for bash

Upvotes: 0

Views: 81

Answers (2)

hawksight
hawksight

Reputation: 81

If you need to diff the files you can do another for loop on your output files. Grab just the file name with the basename command and then put that all together in a diff and output to a ".diff" file using the ">" operator to redirect standard out.

Then diff each one with the expected file, something like:

expectedOutput=~/<some path here>
diffFiles=~/<some path>

for oF in ~/project/tests/output/* ; do
file=`basename ${oF}`
diff $oF "${expectedOutput}/${file}" > "${diffFiles}/${file}.diff"
done

Upvotes: 2

Jahid
Jahid

Reputation: 22428

As your code is, $iF contains full path of file as a string.

N.B: Don't use for iF in $inputFiles

use for iF in ~/project/tests/input/* instead. Otherwise your code will fail if path contains spaces or newlines.

Upvotes: 3

Related Questions