Ask Your Question

How do I get output from sage -c ?

asked 2010-09-03 05:02:17 -0500

kcrisman gravatar image

Please compare:

$ Downloads/sage-4.5.3.rc0/sage -c 'is_prime(991)'

$ Downloads/sage-4.5.3.rc0/sage -c 'print is_prime(991)'


Should sage -c also print to the standard output the result of the command? That would seem a lot more useful. Right now Sage has to get all fired up just to act as a once-off calculator. But maybe this is actually a feature, consistent with other programs that one can call in once-off fashion?

edit retag flag offensive close merge delete


What do you suggest as an alternative? Perhaps a "-p" flag? Perl has a flag to loop over stdin and do similar things. Please state constructive input, thanks. :)

ccanonc gravatar imageccanonc ( 2010-09-03 06:26:33 -0500 )edit

I'm not sure what you mean. I want to be able to just send a Sage command to Sage and get a result, without having a whole Sage session open up, and don't want to have to use the print argument (because I will always forget to do so). Why isn't printing the output the default behavior?

kcrisman gravatar imagekcrisman ( 2010-09-03 06:34:20 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2010-09-03 07:23:55 -0500

niles gravatar image

The most compelling reason for this behavior (if there is any) is probably that python does the same thing. There doesn't seem to be a way to get python to print its results either: see here.

So maybe you're suggesting adding a "calculator" option to sage, so that it will print its results, but is that actually faster than just starting sage? (That is, does sage get all fired up, but just in the background?). And, even better, maybe you're suggesting a "quickstart" option for sage, so that it just loads some very basic functionality and can do calculator-like tasks, but nothing fancy. But I bet that's a hard problem :)

edit flag offensive delete link more


No, I just don't want the Sage hanging around in the background. What is the point of `python -c`? Also, does `python - c command` need to have the command as a string, as seems to be the case for Sage? Not looking for calculator - I want fancy, but without the `sage:` prompt hanging out.

kcrisman gravatar imagekcrisman ( 2010-09-03 08:02:21 -0500 )edit

oh, well, I guess I don't really know what the point of `python -c` is; presumably execute some piece of code that's so simple it's not worth the trouble to put it in a separate file . . . maybe this could be useful to call from a shell script? Anyway, it does seem to require string argument too.

niles gravatar imageniles ( 2010-09-03 08:09:38 -0500 )edit

It's not the same as the "quickstart" option that Niles suggests, but with 'sage.misc.lazy_import' and ticket #8456, we'll be able to reduce Sage's overall startup time (cf. #8254).

Mitesh Patel gravatar imageMitesh Patel ( 2010-09-03 10:13:16 -0500 )edit

I think that a well-documented 'calculator' option would be good for this.

kcrisman gravatar imagekcrisman ( 2010-09-03 16:14:26 -0500 )edit

oh, it just occurred to me that you could write a shell function `psage` which takes a string as input, adds "print " to the front, and passes it to `sage -c`. This wouldn't work for every situation, but would be fine for 1-liners (which I guess is what you're looking for).

niles gravatar imageniles ( 2010-09-04 01:44:07 -0500 )edit

answered 2010-09-03 07:34:50 -0500

ccanonc gravatar image

This sounds to me like a duplicate of the following question:

The question there is how to connect to a running server with a terminal interface, over ssh, or locally.

edit flag offensive delete link more


No, I'm not asking about that. I want to just start it, answer a question, and close it, all in one command.

kcrisman gravatar imagekcrisman ( 2010-09-03 07:59:36 -0500 )edit

kcrisman: That's fine, but then the startup-time is a separate question/complaint. Why not write a short program, then make the -c command run it (so it loops over all the numbers)?

ccanonc gravatar imageccanonc ( 2010-09-03 09:01:49 -0500 )edit

It's not the startup time I'm concerned with. The problem is that I don't know how to get the output of my short program (such as the very short program `factorial(20)` other than using `print`!

kcrisman gravatar imagekcrisman ( 2010-09-03 09:52:37 -0500 )edit

kcrisman: What's wrong with using print? It this is a user-preference issue IMHO.

ccanonc gravatar imageccanonc ( 2010-09-03 09:55:16 -0500 )edit

But it's not obvious. I'm thinking of a new user (or an old one who forgets); if it says it's executing a command, you'd think you would get to see the output as well. At the very least `sage -help` should be more informative about this.

kcrisman gravatar imagekcrisman ( 2010-09-03 16:13:44 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2010-09-03 05:02:17 -0500

Seen: 357 times

Last updated: Sep 03 '10