Skip to content

Instantly share code, notes, and snippets.

@alganet
Last active November 19, 2018 14:21
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alganet/9531262 to your computer and use it in GitHub Desktop.
Save alganet/9531262 to your computer and use it in GitHub Desktop.
sh/bat hybrid that checks available commands. Despite its hackish look, it was tested under Windows XP, wine, ksh, zsh, dash and bash.
: ::\
@echo off
rem () ( command -v $1 >/dev/null 2>&1; return $? )
rem : "Usage sh: $ sh exists.cmd ls && echo exists "
rem : "Usage bat: > exists.cmd notepad "
rem : " > echo %ErrorLevel% "
rem $1 <<REM
:exists
if exist %1 exit /b 0
for %%P in ("%PATH:;=" "%") do (
if exist %%~P\%1 ( exit /b 0 )
call :exists_ext %%~P\%1
if not errorlevel 1 ( exit /b 0 )
)
exit /b 1
:exists_ext
for %%P in ("%PATHEXT:;=" "%") do (
if exist %1%%~P ( exit /b 0 )
)
exit /b 1
REM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment