-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsh-hello.admin.cmd
61 lines (52 loc) · 1.92 KB
/
sh-hello.admin.cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
:::: Multicall sh runner
::
:: A multicall Windows batch file to provide a transparent method to run
:: a sh-script (ash shell script, to be precise) with busybox-w32.
::
:: Usage:
:: Suppose the sh-script is "DIR_NAME/SCRIPT_NAME.sh"
:: - Copy/hardlink this script to "DIR_NAME/". (Symlinking does NOT work.)
:: - Rename this script to SCRIPT_NAME.cmd, to run as current user.
:: - Or, rename this script to SCRIPT_NAME.admin.cmd, to run as admin.
:: - All arguments are passed through directly to the sh-script, so be
:: careful with quoting and slashing!
::
:: Prerequisites:
:: + `sh.exe` must be among PATH or this script's dir.
:: - `sh.exe` is just a renamed `busybox.exe` or `busybox64.exe`.
:: - App's website: http://frippery.org/busybox/
:: + `elevate.exe` should be among PATH or this script's dir.
:: - Not required if not to run as admin.
:: - App's website: http://code.kliu.org/misc/elevate/
::
:: Author: mr-south-guo
:: Website: https://github.com/mr-south-guo/multicall-sh-runner
:: Timestamp: 20200213.152305
@echo off
setlocal
set "_SH_NAME=sh.exe"
set "_ADMIN_BIN=elevate.exe"
set "_SCRIPT_NAME=%~n0"
set "_SCRIPT_DIR=%~dp0"
set "PATH=%_SCRIPT_DIR%;%PATH%"
:: Find `sh.exe` and `elevate.exe`
for %%I in (%_SH_NAME%) do set "_RUN_CMD=%%~$PATH:I"
for %%I in (%_ADMIN_BIN%) do set "_ADMIN_FULL=%%~$PATH:I"
if "%_RUN_CMD%"=="" (
echo Cannot find '%_SH_NAME%'. It should be among PATH or this script's dir.
exit /b
)
:: Run as admin?
for %%I in (%_SCRIPT_NAME%) do set "_AS_ADMIN=%%~xI"
if "%_AS_ADMIN%"==".admin" (
if "%_ADMIN_FULL%"=="" (
echo Cannot find "%_ADMIN_BIN%". It should be among PATH or this script's dir.
exit /b
)
set "_RUN_CMD=^"%_ADMIN_FULL%^" ^"%_RUN_CMD%^""
for %%I in (%_SCRIPT_NAME%) do set "_SCRIPT_NAME=%%~nI"
)
:: Build the sh-script's full path
for %%I in (%_SCRIPT_DIR%%_SCRIPT_NAME%.sh) do set "_SCRIPT_FULL=%%~fI"
:: Run it.
endlocal & %_RUN_CMD% "%_SCRIPT_FULL%" %*