How do I get the `run` command working on Windows 11? - windows

I wanted to try testing Raku code on Windows. I managed to get Windows 11 set up on VirtualBox and the Raku binary installed and I can execute scripts.
However, I cannot seem to get the simplest run command to do anything without returning an error:
run('ls');
results in:
The spawned command 'ls' exited unsuccessfully (exit code: 1, signal: 0)
in block <unit> at .\test.txt line 5
I also tried with dir command, but had the same problem.

There is no ls program on Windows by default, and dir is not an executable that can be run, but rather a Windows command shell built-in. You could try using shell instead of run, or instead try something like:
run 'cmd.exe', '/c', 'dir'

Related

WSL closes abruptly when I execute a windows command after sourcing a bash script containing the option `set -e`

Steps to reproduce.
Please note, I am using WSL (Ubuntu 18) version 1.
Windows version 1909 (OS Build 18363.900).
Create a dummy bash script foo.sh containing the option set -e.
#!/bin/bash
set -e
pwd
Execute the following commands in WSL.
$ source foo.sh
$ explorer.exe .
After I execute the second command explorer.exe . WSL closes abruptly.
I just tried it out, and indeed: When I run explorer, the explorer Windows opens in the background, and the process which starts the Explorer returns with exit code 1. I don't know why Microsoft in their infinite wisdom decided to implement it in this way.
Of course, having set -e in an interactive shell is insane. I suggest to unset it. If, the scenario is not an interactive shell (this is not entirely clear from your question), but you run the commands from inside a script, and you do want to stick with -e, start the explorer with
explorer.exe || true
to keep the shell running.

Bash script to run a dotnet command line app getting ' as cli argument

I wrote a .net core command line app and I wanted to easily be able to run it from the command line on multiple platforms. For Windows I wrote a cmd file. For Linux and MacOs I wrote a bash script.
.net core apps compile to a DLL. To run them from the command line you enter:
dotnet myApp.dll <my command line args>
My bash script looks like:
#!/bin/sh
dotnet ./myApp.dll "$#"
rc=$?
exit $rc
When I run my app through the shell script I get the following error: ' is not recognized.
But if I skip the shell script, and run the app directly as dotnet myapp.dll I get no error whether running on Windows or Linux.
I believe the error is being generated by the library I'm using for command line parsing, but I suspect the problem may be in my bash script.
This is the library that I'm using for Command line parsing.
https://github.com/commandlineparser/commandline
If I add echo "$#" to the top of the script, I see my command line argument echoed to the console.
Is there an error in my bash script?
Is there a better way to write this script?
There could be multiple command line arguments and I want to pass them all to my application as if I had run the app directly using the dotnet cli.
Try changing your script file to unix line endings \n.

How to pipe bash script output to a file on windows

I am running on Windows 7.
I have a bash script which runs fine from a Windows command prompt using "C:\Program Files\Git\git-cmd.exe" "C:\path\myBashScript.sh" 17.1. I can see it running in a new bash window, the output looks fine and it closes. What I want to do is capture the output in a file for further processing.
I tried
"C:\Program Files\Git\git-cmd.exe" "C:\path\myBashScript.sh" 17.1 > C:\out\myBashScript.out.txt
but all I get in the output is the working folder, ie,
C:\path>
Is this possible?
Thanks
"C:\Program Files\Git\git-cmd.exe" is a frontend windows launcher. I'm surprised it takes any argument at all. You should run bash scripts using bash interpreter:
"C:\Program Files\Git\bin\bash.exe" "C:\path\myBashScript.sh"
PS: I'd advise to apply redirection inside the bash script rather than in windows commandline, they are better controlled there.
You need to discriminate between the outputs generated within the shell script, and those generated by the command line execution.
To easily capture the output of the shell script, you may find it easier to do the redirection > to the capture file within the script.
You didn't say what style of system you were on, and how it was set up (Cygwin, GfW; command line in cmd.exe, or bash; integrated or separated git commands (in the cmd.exe), etc). I suspect that the inability to do a simple redirection of the command line is because of that system setup issue.

Running a bash shell script on Windows from within in Notepad++ using NppExec (and Cygwin)

I'm trying to reconstruct how to execute a bash shell .sh file on my Windows machine from within Notepad++ using NPPExec. (I've done this successfully before, but my HDD crashed and I don't recall how I did it previously.)
When it was working before, I would run the NPPExec script that called a .sh file, and it showed me the console output of the .sh file processing in Notepad++ as if it were processing in cygwin.
This is the example .sh file that I'm trying to get to work:
message="Testing"
echo $message
This file is located in the root of C:.
Failed Attempts:
None of the following three methods work:
Execute: C:\nppexec.sh
Response:
CreateProcess() failed with error code 193:
%1 is not a valid Win32 application.
Execute: npp_exec C:\nppexec.sh
Response:
message="Testing"
CreateProcess() failed with error code 2:
The system cannot find the file specified.
$message
Adding #! /bin/bash to the .sh file as the first line just causes an additional error when npp_exec is run:
NPP_EXEC: C:\nppexec.sh
#! /bin/bash
CreateProcess() failed with error code 2:
The system cannot find the file specified.
The solution was to call bash directly:
C:\cygwin\bin\bash --login -c "command.sh"
I have the same error while trying to execute a batch file on windows.
I resolved the problem by executing at first command cmd in console of notepad++, then E:\test.bat
I also have a mksnt installed on my window pc.
by starting at first the bash in console of notepad++, the test shell work well now
bash
C:\nppexec.sh
With a single keystroke I wanted to execute the shell script of the active Tab using Cygwin within notepad.
After few hours looking online and experimenting, I finally came up with
install NppExec plugin
Hit F6
paste the following code:
//save the file
NPP_SAVE
//redirect console output to $(OUTPUT) & silent mode
npe_console v+ --
//convert winpath to cygpath
D:\cygwin64\bin\bash -lc "cygpath \"$(FULL_CURRENT_PATH)\"
//execute the file
D:\cygwin64\bin\bash -lc "$(OUTPUT)"
Hope that save some time to some people
Use this Run command
C:\cygwin64\bin\bash.exe -l -c "cd \"$0\" ; echo $#; \"./$1\"; exec bash;" "$(CURRENT_DIRECTORY)" "$(FILE_NAME)"
You can save this command for later use from Run dialog box.
For git bash, change the path like this:
C:\Progra~1\Git\bin\bash -l -c "cd \"$0\" ; echo $#; \"./$1\"; exec bash;" "$(CURRENT_DIRECTORY)" "$(FILE_NAME)"

NPPExec script failing with CreateProcess() failed with error code 2:

I have the following command in NPPExec:
NPP_SAVE
npp_exec "C:\bin\foo.sh"
Where foo.sh contains the following:
#! /bin/bash
echo "This is a test script"
This is the output from the NPPExec console:
NPP_SAVE: C:\bin\foo.sh
NPP_EXEC: "C:\bin\foo.sh"
#! /bin/bash
CreateProcess() failed with error code 2:
The system cannot find the file specified.
"This is a test script"
================ READY ================
I want NPP_EXEC to just run the script, and leave it alone after that. I had this working before, but my HD crashed, now I can't seem to get it working again.
Remove the sh-bang line (#!/bin/bash) from your code.
That line tells the process to look for an executable file named C:\bin\bash on your computer, then use that file to run the ensuing script. Apparently, the process can't find said file. But it shouldn't need to find it. Windows* can run your .sh file as an executable.
*(If you're using Notepad++, you're on Windows, right?)
Alternatively, put an executable file named "bash" in your folder "/bin", and let that "bash" file have the capability of running your shell script.

Resources