Skip to content

Question: how to execute scripts directly from the shell as if they were executables. #664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
maximetinu opened this issue Feb 23, 2022 · 2 comments

Comments

@maximetinu
Copy link
Contributor

maximetinu commented Feb 23, 2022

Hi! 👋

The documentation, in the section

https://github.com/filipw/dotnet-script#running-scripts

says that

Scripts can be executed directly from the shell as if they were executables.

How can I do that in Windows? I have tried, and the running ./helloworld.csx opens Visual Studio Code for me.

I have tried both from CMD and PowerShell.

It makes sense it opens VS Code, since that's what it says if I right click > properties any .csx that they will open with.

image

If I change it to open with dotnet-script.exe instead, at the paths of

  • C:/Users/username/.dotnet/tools/dotnet-script.exe
  • or C:/ProgramData/dotnet-script/dotnet-script/dotnet-script.exe

Then it kind of works.

  • if I double click it, it opens a cmd, runs the script and instantly closes. So I need to ad a System.Console.ReadKey() at the end of the script
  • if I run ./helloworld.csx from powershell, it does the same, it opens another CMD window and runs in there, to instantly close it afterwards.
  • if I run helloworld.csx directly from the CMD, it works as intended

I can understand that if you run it with double click from the explorer, it closes as soon as it finishes. That makes sense.

But it would be nice if it could be run directly from the powershell, instead of opening the CMD.

Any thoughts on how to achieve that behaviour? The documentation I linked at the beginning explains nothing about it.

P.S: I have already tried adding the shebang #!/usr/bin/env dotnet-script at the top of the file

Thanks in advance!

@filipw
Copy link
Member

filipw commented Feb 23, 2022

There is no way on Windows AFAIK, the documentation should explain that this is for Unix only.

On Windows you can run dotnet script register which will do the following registry scaffolding https://github.com/filipw/dotnet-script/blob/114aed0792045559eddc05a860a7c34841b37397/src/Dotnet.Script.Core/Scaffolder.cs#L79-L88
but it would effectively do what you already did manually and just register dotnet.exe with dotnet-script global tool as the execution handler.

@maximetinu
Copy link
Contributor Author

aah I see. I misunderstood the documentation and thought it should be possible on windows too. Thanks for the quick answer! 👍

maximetinu added a commit to maximetinu/dotnet-script that referenced this issue Feb 23, 2022
filipw added a commit to maximetinu/dotnet-script that referenced this issue Mar 21, 2022
filipw added a commit that referenced this issue Mar 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants