Skip to content

Instantly share code, notes, and snippets.

@zvodd
Last active August 8, 2017 12:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zvodd/1111fc3cf7c5387d44750bed079f5624 to your computer and use it in GitHub Desktop.
Save zvodd/1111fc3cf7c5387d44750bed079f5624 to your computer and use it in GitHub Desktop.
Bash & Batch / Bat Polyglot template and Argument shim
a1 = a1;
a2 = a2;
a3 = a3;
a4 = a4;
a5 = a5;
a6 = a6;
a7 = a7;
a8 = a8;
a9 = a9;
bash("a1=\"$1\"");
bash("a2=\"$2\"");
bash("a3=\"$3\"");
bash("a4=\"$4\"");
bash("a5=\"$5\"");
bash("a6=\"$6\"");
bash("a7=\"$7\"");
bash("a8=\"$8\"");
bash("a9=\"$9\"");
batch("set a1=%1");
batch("set a2=%2");
batch("set a3=%3");
batch("set a4=%4");
batch("set a5=%5");
batch("set a6=%6");
batch("set a7=%7");
batch("set a8=%8");
batch("set a9=%9");
p = p;
batch("set p=\"C:\\Program Files\\Sublime Text 3\\sublime_text.exe\"");
bash("p=\"C:\\Program Files\\Sublime Text 3\\sublime_text.exe\"");
call(p, a1, a2, a3, a4, a5, a6, a7, a8, a9);
echo off
echo ; set +v # > NUL
echo ; function GOTO { true; } # > NUL
GOTO WIN
# Bash start
a1="$a1"
a2="$a2"
a3="$a3"
a4="$a4"
a5="$a5"
a6="$a6"
a7="$a7"
a8="$a8"
a9="$a9"
a1="$1"
a2="$2"
a3="$3"
a4="$4"
a5="$5"
a6="$6"
a7="$7"
a8="$8"
a9="$9"
p="$p"
p="C:\Program Files\Sublime Text 3\sublime_text.exe"
"$p" "$a1" "$a2" "$a3" "$a4" "$a5" "$a6" "$a7" "$a8" "$a9"
# Bash end
exit 0
:WIN
REM Batch start
@echo off
setlocal EnableDelayedExpansion
setlocal EnableExtensions
set a1=!a1!
set a2=!a2!
set a3=!a3!
set a4=!a4!
set a5=!a5!
set a6=!a6!
set a7=!a7!
set a8=!a8!
set a9=!a9!
set a1=%1
set a2=%2
set a3=%3
set a4=%4
set a5=%5
set a6=%6
set a7=%7
set a8=%8
set a9=%9
set p=!p!
set p="C:\Program Files\Sublime Text 3\sublime_text.exe"
!p! !a1! !a2! !a3! !a4! !a5! !a6! !a7! !a8! !a9!
REM Batch end
echo off
echo ; set +v # > NUL
echo ; function GOTO { true; } # > NUL
GOTO WIN
# Bash start
# Bash end
exit 0
:WIN
REM Batch start
REM Batch end
@zvodd
Copy link
Author

zvodd commented Nov 18, 2016

This demonstrates shims for 2 features that are missing from Batsh.

  • An ugly way to get script arguments.
  • A polyglot build option.
    It would be nice to see these features added to the Batsh compiler.

The polyglot is created by copying the output of the respective compiles into template.bat
The example script launches a program not found in $PATH (sublime text).
The final script polygot_argument_launch_compiled.bat can be launched from both git-bash and cmd.exe with a filename argument.

Compiled using http://batsh.org/

template.bat was found at:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment