Ask Your Question
1

Running Sage in Bash

asked 5 years ago

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.

Preview: (hide)

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 ( 5 years ago )
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 ( 5 years ago )

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 ( 5 years ago )
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 ( 5 years ago )
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 ( 5 years ago )

1 Answer

Sort by » oldest newest most voted
0

answered 5 years ago

StrongPiccolo gravatar image

updated 5 years ago

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
Preview: (hide)
link

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: 5 years ago

Seen: 1,256 times

Last updated: Jan 11 '20