Shebang to embed <*your language*> in Windows BAT file - windows

How do you embed scripting language inside BAT file in Windows?
Effectively I am after Unix shebang for Windows: put JS/C#/Python/Java text into a BAT file, have a small header on top then have it compiled/run by just running the container BAT file.
Some scripting engines are guaranteed to exist: VBScript, JS, MSHTA/HTML — are there good shebangs for those?
Others are technically optional, but may be expected: C#, VB.NET, PowerShell, node.js, Python — any clues as to how to wrap those with a shebang?
Finally, there might be a way to smuggle a compiled binary within a BAT file, and shake/goad OS into running it. Any ideas how to do that?

Two useful Javascript shebangs I've come across. Those are crafted for CSCRIPT, MS JScript guaranteed to be bundled with Windows. Can be trivially retargeted for node.js too -- if you're certain node.js would be on the path at runtime.
DARTH shebang, called after the /*-*\ smiley:
title /*-*\&#cscript/nologo /E:jscript %~f0 %*&#goto:EOF&:*/=0
WScript.Echo('OK');
Stealth clubman shebang, called after the scary man with a club emoticon:
0</* :
#cscript/nologo /E:jscript %~f0 %*&#goto:EOF&:*/0
WScript.Echo('OK');
The tradeoff between those two is whether we make it apparent the script is shebanged at runtime or have a more conspicuous header. DARTH would print one line to console, and set the console title. Stealth clubman would take two lines at the JS header.

Related

VS Code does not recognize shebang for bash

According to this post, VS Code is supposed to recognize shebangs in files without extensions, and associate those files with the Shell Script language mode. In that post, they say it works for the shebang #!/bin/sh. However, this doesn't seem to work for shebang #!/bin/bash. I need to use Bash for my project, otherwise I would just switch to the Shell Command Language (sh). Also, I cannot add the .sh extension to the end of the filename, because I'm submitting a batch job to a supercomputer, and the filename needs to be named using the following pattern: jobname-batch. Is there a way to configure VS Code to have the same behavior for extension-less Bash scripts than for extension-less Shell Command Language scripts?
Edit:
#Julia suggested in the comments to make the file executable, and that got it working. :) Thanks!
The "set the executable bit" trick works, but I'm not sure where that information is stored or if it's persisted across reboots. I just remembered a better solution: file associations!
Just add this to project/.vscode/vscode.json (the key should be the name of the file):
{
"files.associations": {
"jobname-batch": "bash"
}
}

Shell script sh executable - edit to see script

Is there a way to see the original code of a executable sh script. (I am very new to Linux and trying to understand what things do and such.)
If you know how I need very clear step by step process so I can just type i the commands and run them.
Thanks for your help. Trying to learn (Windows man for 25 years here)
A shell script specifically can be seen in the original text form by simply printing the contents of the file:
cat disk-space.sh.x
Several caveats:
If you mean an executable rather than a script the situation is different. Scripts are read by an interpreter at runtime, which then executes it line by line. Executables may be either scripts or ELF binaries. The latter have been transformed from the original source code to a machine readable form which is very much harder to read for humans.
The extension of the file (.sh.x or .x) does not control whether the file contents are executed as a binary or script.
If the file really is a script it may have been obfuscated, meaning that the source code on your system has deliberately been changed to make the resulting file hard to read.

problems in Perl script with localized user name on Windows

I have a perl script to start a script file with default program.
system("Start C:\\Temp\\test.jsx");
It works file with English user names but when I change user name to ai𥹖Ц中 it doesn't work.
Also no error message appears to I'm not able to debug.
perl on Windows uses so called ANSI functions to interface with the outside world. That means, if you use interesting characters (for example, certain Turkish letters on a US-English Windows install), perl cannot see them. As I wrote on my blog:
You can't pass characters that are outside of the Windows code page to perl on the command line. It doesn't matter whether you have set the code page to 65001 and use the -CA command line argument to perl: Because perl uses main instead of wmain as the entry point, it never sees anything other than characters in the ANSI code page.
For example:
$ chcp 65001
$ perl -CAS -E "say for #ARGV" şey
sey
That's because ş does not appear in CP 437 which is what my laptop is using. By the time it reaches the internals of perl, it has already become s.
So, there is not much you can do with a stock perl. I was working on a set of patches, but things intervened. I may still get around to it this summer.
Now, in your case, you are passing "interesting characters" to an external program via system. The same problem applies. Because perl is using the ANSI versions of functions used to spawn processes etc, the program spawned will not see a Unicode environment. So, if you are trying to use Korean or Japanese programs with a system code page that does not include them, I am not sure what will happen.
There is not much you can do once perl is running. The environment, command line arguments, everything lives in the ANSI world from that point on. There may be funky work-arounds, but for that one would need to know exactly how 'ai𥹖Ц中' gets from your perl program to the external program.

Cross-platform command line script (e.g. .bat and .sh)

I noticed that Windows 7 enables to execute .sh files as if they were .bat files. That got me wondering whether it is possible to write a .sh file such that it can be executed in Windows and Linux (let's say bash).
The first thing that comes to my mind is to fabricate an if-statement such that Windows and Ubuntu can deal with it and jump into the according block to execute plattform-specific commands. How could this be done?
Note: I know this is not good practice. I also know that scripting languages like Python are far better suited to solve this problem than a mixed-syntax command line script would be. I'm just curious...
You could use this:
rem(){ :;};rem '
#goto b
';echo sh;exit
:b
#echo batch
It's valid shell script and batch, and will execute different blocks depending on how it's run.
Modify the echo and #echo lines to do what you want.
AFAIK, you can't directly run .sh files from Windows' cmd.exe. For that you'll need a *nix emulation layer to run the shell. Check out Cygwin or Msys/MinGW

Preprocessor to add functionality to Windows's CMD?

I need to do a fair bit of scripting in my job as a SQL Server DBA. Sometimes, I need to deploy a fix script to a very restricted environment, where the only option for scripting may be DOS Batch. In one such environment, even VBScript/WSH isn't a possibility, let alone PowerShell. Anyone who has written enough batch files on DOS and Windows knows that it's very limited and a huge PIA when you need to do anything too complicated. This is especially true for folks who have worked with Unix shell scripting, Perl, Tcl, Python, Ruby, etc.
A possible solution to this would be a CMD preprocessor that would add some of the useful functionality from more capable scripting languages. I've tried to find such a utility, but so far I've had no luck.
Which finally leads to my question: is anyone aware of a such a CMD preprocessor? If not, what functionality would you like to see in one?
Addendum:
If you're unfamiliar with the idea of a preprocessor see this Wikipedia entry.
To clarify, I'm thinking of a tool that would add features like:
Functions
Backtick (`) ala Unix shell
...and possibly others. Those are two features I've wished CMD had and can think of a way to implement them with a CMD preprocessor. Functions could be implemented with env vars and GOTO/labels; backticks by piping to a temp file and using set /p =< to read in the result to an env var.
You can already achieve these same ends, but it gets to be very tedious and verbose- which is how I came to the idea of having a preprocessor handle the boilerplate for features like those.
Example
Using the example of backticks, here is an example of unprocessed code from my hypothetical Batch++ and processed vanilla batch script, ready to be run by CMD.exe:
Batch++ Source (test.batpp)
copy `dir /b /s c:\ | find "CADR-README.htm"` \\srv01\users
Run it through the preprocessor
bpp test.batpp > post_test.bat
Resulting CMD/BAT code (post_test.bat)
dir /b /s c:\ | find "CADR-README.htm" > _bt001.tmp
set /p _BT001 =< _bt001.tmp
copy %_BT001% \\srv01\users
set _BT001=
del _bt001.tmp
I am not sure to interpret correctly your question. If you run in a controlled environment that doesn't allow you to run any scripting extension, how are you going to access such a preprocessor?
However, and in regard of the two features you request, you are OK with .BATs. Both features are supported by BAT processing in current Windows versions.
Functions: you have the extended CALL syntax, that supports parameter passing thru argument references %1 .. %9, and enhanced with expansion substitution using %~ syntax. Read HELP CALL.
Backtick: not sure what you want but, in the FOR /F command you may pass a backticked string to be run and its output captured. Read HELP FOR.

Resources