Advertisement
JamieRobillard

shebang.bat

Nov 30th, 2018
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 2.17 KB | None | 0 0
  1. @echo off
  2.  
  3. rem shebang.bat - Unix shell behaviour from windows.
  4. :: Use
  5. ::     assoc .=shebangfile
  6. ::     ftype shebangfile="c:\your\path\to\shebang.bat" "%1" "%*"
  7. ::     setx /M pathext %pathext%;.
  8.  
  9. rem Use setx so that the change is permanent though the effects will not happen until your next invocation of the shell (cmd.exe)
  10. :: As of Window 8.x ftype requires a fully qualified path to the executable and it cannot
  11. :: merely be on the path as it could in previous versions of Windows. Ms does not mention this in documentation though testing will
  12. :: show that a fully qualified path is require. There example in help ftype will fail as documented.
  13.  
  14. :: All of these commands will require administrative privileges.
  15.  
  16. rem You will also need env.bat which can be found here: https://pastebin.com/aVrMnK8H or on the original author's website tutorial.
  17. :: make sure the directory that env.bat is in is also in the system path.
  18.  
  19. rem You should visit the original Author's website for the full tutorial. Though the batch offered, at the time I posted this, has bugs
  20. :: that have been fixed here.
  21.  
  22. rem Author: Dion Nicolaas <dion@nicaolaas.net>
  23. :: http://whitescreen.nicolaas.net/programming/windows-shebangs
  24.  
  25. rem save filename
  26. set filename=%1
  27.  
  28. rem Get the first line of the file
  29. set /p line=<%1
  30. rem Remove all quotes from the string, they shouldn't be there anyway
  31. set line=%line:"=%
  32. rem turn each part of the path into a quoted string, separated by spaces
  33. set line="%line:/=" "%"
  34. rem set first to the first part ("#!"), last to the last part (e.g. perl -w)
  35. for %%i in (%line%) do call :firstlast %%i
  36. rem if it was a shebang line, set command accordingly, else use "type"
  37. set myCommand=type
  38.  
  39. if "%first%"=="#!" set myCommand=%last%
  40. if "%first%"=="#! " set myCommand=%last%
  41.  
  42. rem Run command on the command line
  43. if "%myCommand%"=="type" %myCommand% %filename%
  44. if not "%myCommand%"=="type" %myCommand% %*
  45.  
  46. goto :EOF
  47.  
  48. :firstlast
  49. rem Get first and last token, unquote in the process (which will also strip
  50. :: spaces). In Unix scripts 'perl -w' is much more likely than "the language
  51. :: processor" (long file name with spaces)
  52. if "%first%"=="" set first=%~1
  53. set last=%~1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement