Ask Your Question
1

Running Sage in Bash

asked 2020-01-08 18:37:32 +0200

StrongPiccolo gravatar image

I would like to execute a Sage file from my bash, similar to how I can run 'python Documents/test.py' and have Python execute the file. Is this possible? If so, how? I'm attempting to circumvent the interpreter and automate execution.

edit retag flag offensive close merge delete

Comments

Find where Sage is installed, and do /path/to/sage Documents/test.py. Or add the directory containing sage to your PATH, and then do sage Documents/test.py, just like with Python.

John Palmieri gravatar imageJohn Palmieri ( 2020-01-08 20:14:25 +0200 )edit
1

If you have a file with Sage commands, make sure you name the file with .sage suffix.

At least in Linux, I can do this:

[bryanso@localhost ~]$ cat test.sage
f = sin(x) / x
d = diff(f, x)
show(d)

[bryanso@localhost ~]$ sage test.sage
cos(x)/x - sin(x)/x^2
bso gravatar imagebso ( 2020-01-09 02:45:35 +0200 )edit

Actually .py suffix should work too. I just notice this:

[bryanso@localhost ~]$ sage -help
SageMath version 8.8, Release Date: 2019-06-26

Optional arguments:
  file.[sage|py|spyx] -- run given .sage, .py or .spyx file
...
bso gravatar imagebso ( 2020-01-09 02:49:08 +0200 )edit
1

I am just learning sage, but there is yet another way to envoke sage that is even more bash-like. Assuming that sage is installed in "/usr/bin/sage" you can do:

$ cat ./sagetest.sage
#!/usr/bin/sage 
import sys
args = len(sys.argv)
if args == 2:
   arg1 = int(sys.argv[1]) # read command argument
   print(arg1) # do something with command argument

f = sin(x) / x
d = diff(f, x)
show(d)
..
Note the '#!/path/to/sage' must be on the first line of the file
You must also make the file executable before it can be run the first time
$ chmod 755 ./sagetest.sage

Now you can run the file with "./sagetest.sage" or from in your PATH environment, while even with passing it arguments like in python:
$ ./ssagetest.sage 123
slcoleman gravatar imageslcoleman ( 2020-01-10 23:42:44 +0200 )edit
1

@slcoleman: it's best if sage is in your PATH, and then the first line should be #!/usr/bin/env sage.

John Palmieri gravatar imageJohn Palmieri ( 2020-01-11 00:15:35 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-01-11 00:23:08 +0200

StrongPiccolo gravatar image

updated 2020-01-11 00:24:13 +0200

As shown by @slcoleman and @John Palmieri, the easiest way to invoke Sage via Bash is through /path/to/sage, followed by the file to be run. This can be made simpler through an alias added to ~/.bashrc or the PATH. I'll also add a small Bash script I wrote that runs a Sage file on multiple threads:

for j in {1..10}; do   echo Launching thread....;   ../sage ./loader.sage.py & done 2>/dev/null
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-01-08 18:37:32 +0200

Seen: 738 times

Last updated: Jan 11 '20