0

I have a bash shell script that calls a C++ binary. I would like the output of the binary to be saved to a file, with a name based on run-time parameters that were used to call the binary.

#!/bin/bash
c_command="./code -v 1 -i file.txt -o out.txt -a 3 -w"
output=$($c_command)
echo output > ${c_command}_output.txt

Unfortunately, this is resulting in the following error:

./script.sh: line 4: ${c_command}_output.txt: ambiguous redirect

I was inspired by this stack exchange post, but clearly, something is not right.

HMLDude
  • 1,547
  • 7
  • 27
  • 47
  • There are bigger problems with this code. See [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050). – Charles Duffy Feb 28 '20 at 20:59
  • 1
    However, the **immediate** issue is caused by the lack of quotes. – Charles Duffy Feb 28 '20 at 20:59
  • Making it `>"${c_command}_output.txt"` would fix that syntax error, but do you really want to make a file named `code -v 1 -i file.txt -o out.txt -a 3 -w_output.txt`? – Charles Duffy Feb 28 '20 at 20:59
  • And `echo output` is writing the string "output". It's not writing the content of the variable named `output`. – Charles Duffy Feb 28 '20 at 21:00
  • Immediately, though, this is just too broad to be answerable -- you've got at least four separate bugs; there's not just one question here. – Charles Duffy Feb 28 '20 at 21:01
  • ...the issue you asked about most immediately is a duplicate of [getting an ambiguous redirect error](https://stackoverflow.com/questions/2462385/getting-an-ambiguous-redirect-error). – Charles Duffy Feb 28 '20 at 21:03

0 Answers0