On Mon, Sep 03, 2007 at 11:57:51PM -0400, A. Costa wrote:
> Package: apcalc
> Version: 2.12.1.13-3
> Severity: normal
> 
> What works, and what fails:
> 
>     # add 2 to index with 'for' loop (this works)
>     % for f in `seq 3` ; do calc $f + 2 ; done 
>             3
>             4
>             5
> 
>     # same code, but with a 'while read' loop (it fails)
>     % seq 3 | while read f ; do calc $f + 2 ; done 
>             3
>             2
>             3
> 
> 'calc' behaves correctly at first, (it prints 3), then outputs the
> index '$f' without adding 2.  Most utils aren't confused by 'while
> read':
> 
>     % seq 3 | while read f ; do expr $f + 2 ; done 
>     3
>     4
>     5
> 
> I'd guess that a variable input with 'read' is subtly different,
> (how?), and that 'calc' parses the command line in an unusual way.  If
> that's not a bug, then it should be documented in 'man calc'.  (It makes
> it hard to pump in a big list of numbers to 'calc'.)

I can explain what happens, but I don't know whether it's a bug or a
feature, so I've Bcc'd the calc mailing list to get a definitive answer.

In your first test case, calc's stdin is connected to your terminal. Since
you provided command line arguments, it doesn't become interactive after
performing the calculation on the command line and exits.

In the second case, calc's stdin is connected to the same end of the "seq
3" pipe as the "read" command. Now "read" reads one line and calc is
started with the command line arguments "1 + 2", resulting in the first "3"
being printed. Now calc itself reads from stdin and reads the lines "2" and
"3". Obviously, the result of these calculations is "2" and "3" :-)

So the difference is that in the first case, calc is run three times, while
it runs only once in the second case. I don't know whether calc is supposed
to continue reading from stdin if it had command line arguments and its
stdin is not connected to a terminal. The man page only talks about
interactive mode, but this isn't really interactive mode (otherwise, you'd
get a calc command line prompt).

I could imagine that this behaviour might be a useful feature instead of a
bug, but then I agree that it should be documented in the man page.

BTW, as a workaround, you can append a "< /dev/null" to the calc invocation
in the second case. This should make it behave as you expected.

BTW#2 for the calc folks: The documentation in the calc man page for the -i
option is a bit strange: "If calc_cmd args are given, calc by default, calc
will execute them and exit." I guess the 2nd "calc" shouldn't be there. And
the "This flag will cause calc to drop into interactive mode after the
commands are executed." sentence appears twice.

Martin


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to