#!/usr/bin/perl
# calc-0.1
#
# The ultimate command line calculation program :-^
# You now have full Perl calculating power at your
# fingertips (say: commandline or scripts).
# You optionally can format your output using printf formatting
# instructions (shell scripts!).
# Building floating point tables in shell scripts
# with spaces and indentation is real fun now.
# Usage calc []
#
# Input is a string like (10+3)/7 or "(10 + 3) / 7"
# Optional second block of input is considered formatting string.
#
# Take care that
# a) there are no spaces between the stuff to calculate or enclose into ()
# b) to multiply you have to escape '*' as '\*'
#
# Samples:
# calc 10+3
# calc ( 10 + 3 )
# calc 100/7
# calc 12\*7
# calc sqrt(100)
# calc sqrt( 2 + .1 ) %d
# calc 10/3 %3.3f
# Comments/improvements to:
# Hans Zoebelein
# Enjoy!
# Hans
if (@ARGV == 0 || @ARGV > 2)
{
$myname = `basename $0`;
chomp($myname);
die("Usage: $myname <\"what_to_calculate\"> []\n");
}
$format = "";
$calcme = $ARGV[0];
(@ARGV == 2) && ($format = $ARGV[1]);
$output = eval($calcme);
if(@ARGV == 1)
{
print(STDOUT "$output\n");
}
else
{
printf(STDOUT "$format\n", $output);
}
exit(0);