Page 1 of 1

Execute the command from a Text file directly

Posted: 24 Feb 2023, 05:38
by paulcheung
Hi, I am an absolute newbie to programming. I wonder whether I can execute the commands from a Text file directly

Suppose that has a text file that
1) is called: example.txt
2) is located at: \\wsl$\Ubuntu\home\myname\example.txt
3) has the following two lines of commands

$p = new Polytope(POINTS=>[[1, 1, 2, 3,], [1, 4, 5, 2,], [1, 4, 5, 2,],]);
print $p->FACETS;

Is there any way that I can use the interactive session to call out the text file and execute these commands? Thank you!

Re: Execute the command from a Text file directly

Posted: 24 Feb 2023, 09:48
by gawrilow
Yes, you can use the script function:

Code: Select all

script("example.txt");
In case your current working directory is not your home dir:

Code: Select all

script("~/example.txt");
You can learn various tips and tricks regarding scripting in this chapter of documentation.

Re: Execute the command from a Text file directly

Posted: 24 Feb 2023, 17:26
by paulcheung
Thank you very much for the reply! I am still having some trouble.

When I type in

Code: Select all

script("example.txt");
It gives the error message.

ERROR: Package "Polytope" does not exist at /home/myname/example.txt line 1.

Yet, the two command lines work perfectly if I copy and paste them into the interactive sessions. So I tried to read from the chapter of documentation, and I added in the preamble so that the text file now contain

Code: Select all

use application "NAME"; $p = new Polytope(POINTS=>[[1, 1, 2, 3,], [1, 4, 5, 2,], [1, 4, 5, 2,],]); print $p->FACETS;
And then I type script("example.txt") again, it gives another error message (two lines)..

ERROR: Unknown application NAME
BEGIN failed--compilation aborted at /home/myname/example.txt line 1.

I don't know how to fix this... I sincerely appreciate it you could help me. Thank you!!

Re: Execute the command from a Text file directly

Posted: 24 Feb 2023, 17:48
by paulcheung
Ohhhhhh, I guess I just figured it out from your replies in other posts. I should type this in the text instead

Code: Select all

use application "polytope"; $p = new Polytope(POINTS=>[[1, 1, 2, 3,], [1, 4, 5, 2,], [1, 4, 5, 2,],]); print $p->FACETS;
Thank you anyway! :lol: