My Profile

Keep Up to Date:
Blog RSS
Blog
Forum RSS
Forum
Post New Topic Post Reply
Posted 1 Year, 1 Month ago
RAZA
Expert Boarder
Posts: 109
graph
User Offline
 
I'm trying to come up with a Perl script that will allow me to type in a command-line math expression, push enter, and have the expression's evaluation printed in the terminal. Here's a simple example:

#!/usr/bin/perl print 'Enter expression:n'; $a = <STDIN>; $a =~ s/n//g; print ($a); print 'n';

The file is called 'm', it's been chmod to 755, and placed in my path. So if I type 'm' at the terminal, and enter 1+2, the string '1+2' is returned instead of the integer '3'.

This might be more of a Perl than a Linux/bash question, but how do I get the variable $a to be viewed as a math expression rather than a string?

Thanks, and best regards,
The administrator has disabled public write access.
Posted 1 Year, 1 Month ago
groundtwelve
Expert Boarder
Posts: 85
graphgraph
User Offline
 
You could also do this from the shell:

perl -e 'print 1+2'
The administrator has disabled public write access.
Posted 1 Year, 1 Month ago
ejtaal
Expert Boarder
Posts: 80
graphgraph
User Offline
 
I'm sorry that I can't help you with Perl, but here's a Bash version:

echo -n 'Enter expression: ' read expr echo $(($expr))
The administrator has disabled public write access.
Posted 1 Year, 1 Month ago
nfdouglas
Senior Boarder
Posts: 69
graphgraph
User Offline
 
So is awk, when

calc () { echo $* bc -l }

will give you a real calculator. For that matter,

echo $expr bc -l

is pretty easy without being a Bash function.
The administrator has disabled public write access.
Posted 1 Year, 1 Month ago
dachs
Senior Boarder
Posts: 71
graphgraph
User Offline
 
Thanks, Tony. Works like a charm!

All the best,
The administrator has disabled public write access.
Posted 1 Year, 1 Month ago
chandu
Senior Boarder
Posts: 75
graphgraph
User Offline
 
Perhaps, but I'm introducing some high-school kids to (rudimentary) Linux and Perl, and I'd like to give them a quick way to try out the logic/syntax of their math before they 'paste' it into a Perl template. The Perl they'd be doing is only the math, and just enough to crunch a few lab-collected numbers and to get an idea of how computers are used other than for web, video, and music.

Best regards, And thanks for the feedback,
The administrator has disabled public write access.
Posted 1 Year, 1 Month ago
Linda2
Senior Boarder
Posts: 70
graphgraph
User Offline
 
Yeah, Bill Gates making it an artform.

All the way up to when they want to change a system script.

Provide them with all the tools and let them pick the best tool to the job.
The administrator has disabled public write access.
Posted 1 Year, 1 Month ago
gromit
Senior Boarder
Posts: 61
graphgraph
User Offline
 
Now you are being ignorant.

Awk, for instance, is clearly an extremely good and easy to use language which is in many cases superior to perl for handling structured text. And, as such, should be used when it is suitable. The same applies to shell programming and sed.
The administrator has disabled public write access.
Posted 1 Year, 1 Month ago
Howard
Senior Boarder
Posts: 69
graphgraph
User Offline
 
Agreed...to a point. These are high school kids in a physics class, so introducing them to Linux/Perl is a perk that's being fitted into a tight time frame. It there's just one scripting language they get exposed to, I've guessed that Perl is probably the one it should be...mostly because it's the one that I know most about...but also because it's more than just for scripting.

With that said, I've always believed that one should never get so comfortable with what's in front of them that they become fearful of learning something new. I use Mac's, Windows and Linux, and enjoy having the strengths of all three at my disposal. Ditto with programming languages, although I'm at best an amateur programmer, so you're more informed to address that topic than I am.

With all that said, here's question that a little Bash would certainly be of use to answer:

3*2

6

Is there a way to get the Bash variable $N to be recognized in the Perl just like $a is seen as a math expression?

Best regards,
The administrator has disabled public write access.
Posted 1 Year, 1 Month ago
RAZA
Expert Boarder
Posts: 109
graph
User Offline
 
Nope, you might not have perl available on many *nix systems, awk/sed/ksh are extremely powerfull tools and for sure available.

Don't get me wrong, I'm using perl myself and it's great for .cgi, but it won't help you much if it's not on the system and for some reasons, not allowed to install.

Michael Heiming
The administrator has disabled public write access.
Posted 1 Year, 1 Month ago
10stone5
Senior Boarder
Posts: 73
graphgraph
User Offline
 
[..]

Some shells allow perl to get some 'env' vars, but that's all.

Michael Heiming
The administrator has disabled public write access.
 
Copyright © 2006 - Dec 2008 My Linux Gang