Unix Shell - command line calculator

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2007 > command line calculator





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author command line calculator
Gary Wessle

2007-02-15, 1:26 am

Hi

is there a command line calculator, simple for light duty.
expr 1004 * 30 does not do it.
I did

echo -n "#" > pc
echo -n ! >> pc
echo "/usr/bin/perl" >> pc
echo "print eval(join(' ',@ARGV)).qq{
};" >> pc
chmod +x pc
sudo mv pc /usr/bin/

# pc 1004 * 30
Bareword found where operator expected at (eval 1) line 1, near "1044 anaconda"
(Missing operator before anaconda?)
Warning: Use of "log" without parentheses is ambiguous at (eval 1) line 1.


thanks
Janis Papanagnou

2007-02-15, 1:26 am

Gary Wessle wrote:
> Hi
>
> is there a command line calculator, simple for light duty.
> expr 1004 * 30 does not do it.


The "wildcard" * will usually be expanded by the shell to the filenames
in the current directory. Just escape it...

$ expr 1004 \* 30
30120


Janis

> I did
>
> echo -n "#" > pc
> echo -n ! >> pc
> echo "/usr/bin/perl" >> pc
> echo "print eval(join(' ',@ARGV)).qq{
> };" >> pc
> chmod +x pc
> sudo mv pc /usr/bin/
>
> # pc 1004 * 30
> Bareword found where operator expected at (eval 1) line 1, near "1044 anaconda"
> (Missing operator before anaconda?)
> Warning: Use of "log" without parentheses is ambiguous at (eval 1) line 1.
>
>
> thanks

Kenny McCormack

2007-02-15, 1:26 am

In article <m3odnwmb9p.fsf@localhost.localdomain>,
Gary Wessle <phddas@yahoo.com> wrote:
>Hi
>
>is there a command line calculator, simple for light duty.
>expr 1004 * 30 does not do it.


Here's a script I call "iPerl":

#!/usr/local/bin/perl
print ":-) ";
while (<> ) { print eval; print ( ($@ || "\n") . ":-) " ) }

Or, you could try this in (t)csh:

alias c "echo '\!*' | sed 's/:/;/g' | bc -l"

Chris F.A. Johnson

2007-02-15, 1:26 am

On 2007-02-15, Gary Wessle wrote:
> Hi
>
> is there a command line calculator, simple for light duty.
> expr 1004 * 30 does not do it.


That's because you need to escape the asterisk:

expr 1004 * 30

With a POSIX shell (bash, ksh, etc.):

echo $(( 1004 * 30 ))

I use this function:

calc()
{
awk 'BEGIN { OFMT="%f"; print '"$*"'; exit}'`
}

Example:

$ calc '1004 * 30' ## Note: escape asterisks and other special characters
30120

> I did
>
> echo -n "#" > pc
> echo -n ! >> pc
> echo "/usr/bin/perl" >> pc
> echo "print eval(join(' ',@ARGV)).qq{
> };" >> pc


What? Do you mean:

{
echo '#! /usr/bin/perl'
echo "print eval(join(' ',@ARGV)).qq "\n";"
} > pc

> chmod +x pc
> sudo mv pc /usr/bin/
>
> # pc 1004 * 30
> Bareword found where operator expected at (eval 1) line 1, near "1044 anaconda"
> (Missing operator before anaconda?)
> Warning: Use of "log" without parentheses is ambiguous at (eval 1) line 1.


You didn't escape the asterisk.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Chris F.A. Johnson

2007-02-15, 1:26 am

On 2007-02-15, Chris F.A. Johnson wrote:
> On 2007-02-15, Gary Wessle wrote:
>
> That's because you need to escape the asterisk:
>
> expr 1004 * 30
>
> With a POSIX shell (bash, ksh, etc.):
>
> echo $(( 1004 * 30 ))
>
> I use this function:
>
> calc()
> {
> awk 'BEGIN { OFMT="%f"; print '"$*"'; exit}'`
> }
>
> Example:
>
> $ calc '1004 * 30' ## Note: escape asterisks and other special characters
> 30120
>
>
> What? Do you mean:
>
> {
> echo '#! /usr/bin/perl'
> echo "print eval(join(' ',@ARGV)).qq "\n";"


That should be:

echo "print eval(join(' ',@ARGV)).qq \"\\n\";"

> } > pc
>
>
> You didn't escape the asterisk.


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Xicheng Jia

2007-02-15, 1:26 am

On Feb 14, 8:59 pm, Gary Wessle <phd...@yahoo.com> wrote:
> Hi
>
> is there a command line calculator, simple for light duty.
> expr 1004 * 30 does not do it.
> I did
>
> echo -n "#" > pc
> echo -n ! >> pc
> echo "/usr/bin/perl" >> pc
> echo "print eval(join(' ',@ARGV)).qq{};" >> pc


In Perl, joining array elements with spaces is the same as double-
quoting the array. so

join(' ', @ARGV) is the same as "@ARGV"

thus your PERL script can be like:

#!/usr/bin/perl -lw
print eval("@ARGV");
__END__

or wrap it with a function(bash):

pc() { PERL -le 'print eval("@ARGV")' "$*"; }

Regards,
Xicheng

> chmod +x pc
> sudo mv pc /usr/bin/
>
> # pc 1004 * 30
> Bareword found where operator expected at (eval 1) line 1, near "1044 anaconda"
> (Missing operator before anaconda?)
> Warning: Use of "log" without parentheses is ambiguous at (eval 1) line 1.
>
> thanks



Anderson Deluiz

2007-02-15, 1:19 pm

On 15 fev, 02:36, "Xicheng Jia" <xich...@gmail.com> wrote:
> On Feb 14, 8:59 pm, Gary Wessle <phd...@yahoo.com> wrote:
>
>
>
>
> In Perl, joining array elements with spaces is the same as double-
> quoting the array. so
>
> join(' ', @ARGV) is the same as "@ARGV"
>
> thus your PERL script can be like:
>
> #!/usr/bin/perl -lw
> print eval("@ARGV");
> __END__
>
> or wrap it with a function(bash):
>
> pc() { PERL -le 'print eval("@ARGV")' "$*"; }
>
> Regards,
> Xicheng
>
>
>
>
>
>
> - Mostrar texto entre aspas -


Why not use "bc" ?

Regards

William Park

2007-02-16, 7:16 pm

Gary Wessle <phddas@yahoo.com> wrote:
> Hi
>
> is there a command line calculator, simple for light duty.
> expr 1004 * 30 does not do it.


rpn 1004 30 x =

Ref:
http://home.eol.ca/~parkw/index.html#rpn

--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com