help-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: script to provide responses to prompts (badly written C program)


From: Bob Proulx
Subject: Re: script to provide responses to prompts (badly written C program)
Date: Mon, 31 Oct 2011 18:43:48 -0600
User-agent: Mutt/1.5.21 (2010-09-15)

Moving this discussion from bug-bash over to help-gnu-utils as it is a
generic shell programming question.

Fernan Aguero wrote:
> The Problem: a badly written C program (mktrace) that doesn't accept
> input as usual.

What is usual?  It says it is using 'gets()' which reads from stdin.
Reading from stdin is normal even if using of gets() is trouble due to
the possible buffer overrun.

Do you have the source of this program?  What does this program do?

> E.g. you cannot do this: 'mktrace filename', nor this 'mktrace < filename'
> 
> Instead, you have to run it like this:
> 
> [fernan@host] mktrace
> warning: this program uses gets(), which is unsafe.
> enter FASTA filename: (type filename here)
> enter output filename: (type another filename here)
> 
> which of course does not make it easy when you have to run mktrace on
> 1000 files.

Have you tried to pre-answer those questions?  Try this:

  $ printf "file1\nfile2\n"
  file1
  file2

  $ printf "file1\nfile2\n" | mktrace

If the program behaves as I expect it to behave then the first gets()
should get the first string before the newline and the second will get
the secone one and so forth.

You could also use echo in the traditional way too.  That might be
more readable and understandable this way.  It is a matter of taste.

  {
    echo file1  # In answer to: enter FASTA filename:
    echo file2  # In answer to: enter output filename:
  } | mktrace

> I've started playing with bash, trying to work around this, with
> mixed success:
> 
> This doesn't work:
> #!/bin/bash
> mktrace
> echo "filename"
> echo "output"

That has no hope of working.  It shows me that you are still learning
what is happening.  (We all have to start somewhere.)  In the above it
starts up mktrace normally.  Then after mktrace finishes it echo two
strings.  There isn't any connection between the programs.

> Nor this:
> #!/bin/bash
> mktrace | {
>  echo "filename"
>  echo "output"
> }

That takes the *output* of mktrace and pipes it into the echo
commands.  But since the echo commands do not read any input the
output is discarded.  The program should get an EPIPE error and exit
due to the error but if as you say the program isn't well behaved then
it might do anything.

> However, this kind of works (though I don't quite understand why):
> #!/bin/bash
> find . -type f -name '*.fasta' | {
>   while read f
>     do
>       mktrace
>       echo "$f"
>       echo "$f.ab1"
>     done
> }

I have no idea why that would appear to be doing anything either.  But
you could combine that with what I think is a working solution this way:

  find . -type f -name '*.fasta' | while read f; do
    printf "$f\n$f.out\n"
  done

Observe that it prints out the filename first followed by the filename
again with a .out appended to the end making it a different filename
than the input file.  Use this to pipe into your mktrace program.

  find . -type f -name '*.fasta' | while read f; do
    printf "$f\n$f.out\n" | mktrace
  done

Hope this helps,
Bob



Fernan Aguero wrote:
> Hi,
> 
> please accept my apologies, as this is my first post here. I'm sure
> I'm asking a very stupid questions, but I'm kind of stuck with this
> ...
> 
> The Problem: a badly written C program (mktrace) that doesn't accept
> input as usual.
> 
> E.g. you cannot do this: 'mktrace filename', nor this 'mktrace < filename'
> 
> Instead, you have to run it like this:
> 
> [fernan@host] mktrace
> warning: this program uses gets(), which is unsafe.
> enter FASTA filename: (type filename here)
> enter output filename: (type another filename here)
> 
> which of course does not make it easy when you have to run mktrace on
> 1000 files.
> 
> 
> I've started playing with bash, trying to work around this, with mixed 
> success:
> 
> This doesn't work:
> #!/bin/bash
> mktrace
> echo "filename"
> echo "output"
> 
> Nor this:
> #!/bin/bash
> mktrace | {
>  echo "filename"
>  echo "output"
> }
> 
> However, this kind of works (though I don't quite understand why):
> #!/bin/bash
> find . -type f -name '*.fasta' | {
>   while read f
>     do
>       mktrace
>       echo "$f"
>       echo "$f.ab1"
>     done
> }
> 
> 
> In this latter case, my script gets mktrace to do its magic, BUT:
> 
> i) only for one file in the directory (there are many files that match
> the globbing pattern)
> ii) it overwrites the original .fasta file with the expected binary
> output, and generates a new file (*.phd.1), as expected.
> 
> [Note: in addition to the output filename specified, mktrace generates
> another output file, with the same namebase but ending in '.phd.1']
> 
> Any idea or suggestion would be much appreciated. I'm particularly
> interested in understanding and learning along the way :)
> 
> Cheers,
> 
> -- 
> fernan
> 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]