Search code examples
bashcygwin

Command works in command line but gives 'no such file or directory' in bash script


Like the tags say, this is in Cygwin.

I have a command line program that I can run from the Cygwin console:

./optAlg.exe data/5Node/1.dat 500 2>&1 > output/1.out

I wrote the following bash script to put it in a loop

#!/bin/bash

for inputFile in data/5Node/*.dat
do
  outputFile=${inputFile##*/} #strip output name from input name
  outputFile=${outputFile%.*} #strip output name input file
  outputFile+=".out"
  "./optAlg.exe $inputFile 500 2>&1 > output/$outputFile"
done

When I run the bash script, for every iteration of the for loop I get 'No such file or directory', e.g.,

./batchOpt.sh: line 8: ./optAlg.exe data/5Node/1.dat 500 2>&1 > output/1.out: No such file or directory

What's happening here? What I'm not sure what I'm doing wrong.


Solution

  • Remove the quotes around line 8.
    Like this:

    ./optAlg.exe "$inputFile" 500 2>&1 > "output/$outputFile"
    

    By placing quotes around the whole line you tell bash to execute a command which is called exactly ./optAlg.exe $inputFile 500 2>&1 > output/$outputFile and of course there is no such command. In reality you want to run ./optAlg.exe with parameters. Do not forget to place quotes around the variables because otherwise filenames that have whitespace characters are going to be passed as several arguments.
    Please read about arguments.
    And you can read about common pitfalls as well.