0

I have a bash for loop that looks like this:

for entry in ~/properties/*  
do
  PROP=$(basename "$entry")
  echo $PROP
  my_converter $entry -o $OPATH$PROP.vcl --tf --tf-file=$OPATH$PROP.tf
done

This does most of what I need. I set the OPATH variable before the loop and create a new file with the same name and append a different file extension.

What's missing is to write the output generated by running my_converter into another file with the same name but with a file extension of .info.

Some things I've tried (based on this question):

my_converter $entry -o $OPATH$PROP.vcl --tf --tf-file=$OPATH$PROP.tf >> $OPATH$PROP.info

and creating a new file descriptor with the .info file as the target:

  ...
  do
    exec 3>$entry.info
    PROP=$(basename "$entry")
    echo $PROP
    my_converter $entry -o $OPATH$PROP.vcl --tf --tf-file=$OPATH$PROP.tf 1>3
  ...

And for completeness, I've also tried just doing a regular redirect:

my_converter $entry -o $OPATH$PROP.vcl --tf --tf-file=$OPATH$PROP.tf > $OPATH$PROP.info

I know there's some redirect voodoo I can do to get this working. Any help greatly appreciated.

Ramy
  • 20,541
  • 41
  • 103
  • 153
  • 1
    If you want to redirect to a pre-opened file descriptor, it should be `>&3`; just `1>3` opens a file named `3`. – Charles Duffy Dec 09 '20 at 16:57
  • That said, there's no reason you _can't_ just use `>"$entry.info"`. – Charles Duffy Dec 09 '20 at 16:58
  • Can you build a [mre] that doesn't require tools (like `my_converter`) and file/directory structures (`~/properties`) that only you have, so others can see the problem themselves and test proposed answers? – Charles Duffy Dec 09 '20 at 16:58
  • creating a minimal reproducible example is proving a bit difficult. I'm not sure why exactly but everything else I'm doing write the output to the file as expected. For everything I've tried to make an MRE I've been using a linux command (on my mac). I'm not sure it matters but `my_converter` is a ruby application. When I use the standard `> output.file` on the commandline running the app for a single conversion it works just fine. – Ramy Dec 09 '20 at 17:14
  • That being the case, I wonder if we have something unusual, like a carriage return, in your variable's value. Consider using `set -x` to enable logging in your script. – Charles Duffy Dec 09 '20 at 17:20
  • (`echo $whatever` doesn't really tell you nearly as much as the content of the variable `whatever` as one might expect; see [I just assigned a variable, but `echo $variable` prints something else!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) and [Why is `printf` better than `echo`?](https://unix.stackexchange.com/a/65819/3113)). And run your code through http://shellcheck.net/ -- it's got a fair bit of potential for bugs triggered by inadequate quoting. – Charles Duffy Dec 09 '20 at 17:21
  • I am thinking this might have just been a quoting issue. I cleaned up all my directories and retrying now. – Ramy Dec 09 '20 at 17:52
  • 1
    Yeah looks like I just needed some double quotes and to clean up my input and output directories to see the changes. Thanks for the help @CharlesDuffy! – Ramy Dec 10 '20 at 19:43

0 Answers0