Тема: CMD/BAT: BusyBox запускалка, BusyBox Runner
Доработал обертку-запускалку для BusyBox, компактного набора консольных утилит Юникс.
Требования
Для работы необходимо:
-- Загрузить скрипт bb.bat из репозитория https://github.com/ildar-shaimordanov/busybox-runner.
-- Загрузить сам BusyBox со страницы https://frippery.org/busybox/ (любая, 64- или 32-битная, по вашему желанию).
-- Поместить оба файла в каталог, видимый из переменной окружения $PATH.
или...
-- Загрузить скрипт bb.bat из репозитория https://github.com/ildar-shaimordanov/busybox-runner.
-- Поместить файл в каталог, видимый из переменной окружения $PATH.
-- Чтобы загрузить последнюю версию 32-битной или 64-битной сборки BusyBox, выполнить одну из команд, соответственно:
bb --download win32
или
bb --download win64
Загруженный файл сохранется в одном каталоге со скриптом. Для загрузки требуется PowerShell.
Применение
1. Запуск встроенных функций BusyBox
Запустить функцию и передать, если необходимо, опции:
bb function [function-options]
Фактически, это тоже самое, что делает BusyBox:
busybox function [function-options]
2. Запуск команды или скрипта из переменной $PATH
Выполнить внешнюю команду или скрипт, найденные в переменной $PATH или казанные по абсолютному или относительному пути:
bb [shell-options] [DIR]command [command-options]
busybox [sh [shell-options]] [DIR]command [command-options]
3. Запуск однострочного скрипта
bb [shell-options] -c "script"
busybox sh [shell-options] -c "script"
Исходный код
Актуальные на момент написания этого текста версии скриптов.
bb.bat
: ; [ $# -gt 0 ] || exec sed -n "s/^:: \?//p" << '::::'
:: Simplify running scripts and commands with BusyBox
::
:: USAGE
:: Print BusyBox help pages
:: bb --help
:: bb --version
:: bb --list[-full]
::
:: Run a built-in BusyBox function
:: bb function [function-options]
::
:: Run an executable from $PATH or specified with DIR
:: bb [shell-options] [DIR]command [command-options]
::
:: Run a one-liner script
:: bb [shell-options] -c "script"
::
:: Download the latest 32-bit or 64-bit build of BusyBox
:: bb --download win32
:: bb --download win64
::
:: SEE ALSO
:: Learn more about BusyBox following these links:
::
:: https://busybox.net/
:: https://frippery.org/busybox/
:: https://github.com/rmyorston/busybox-w32
::::
: << '____CMD____'
@echo off
setlocal
set "BB_EXE="
:: Look for the latest instance next to this script
if not defined BB_EXE for /f "tokens=*" %%f in ( '
dir /b /o-n "%~dp0busybox*.exe" 2^>nul
' ) do if not defined BB_EXE if exist "%~dp0%%~f" set "BB_EXE=%~dp0%%~f"
:: Fail, if BusyBox not found and download not required
if not defined BB_EXE if /i not "%~1" == "--download" (
2>nul echo:ERROR: BusyBox executable not found
exit /b 1
)
:: ========================================================================
:: Try to download
if /i "%~1" == "--download" (
for %%p in ( "powershell.exe" ) do if "%%~$PATH:p" == "" (
>&2 echo:%%p is required
goto :EOF
)
set "BB_URL="
set "BB_DST="
if /i "%~2" == "win32" (
set "BB_URL=https://frippery.org/files/busybox/busybox.exe"
set "BB_DST=%~dp0busybox.exe"
) else if /i "%~2" == "win64" (
set "BB_URL=https://frippery.org/files/busybox/busybox64.exe"
set "BB_DST=%~dp0busybox64.exe"
) else (
>&2 echo:win32 or win64 required
goto :EOF
)
echo:Downloading started...
set BB_URL
set BB_DST
powershell -NoLogo -NoProfile -Command "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12;$w=New-Object System.Net.WebClient;$w.DownloadFile($Env:BB_URL,$Env:BB_DST)"
echo:Downloading completed
goto :EOF
)
:: ========================================================================
:: Locate the history file in the $TEMP directory
set "HISTFILE=%TEMP%\.ash_history"
:: Locate the history file next to Busybox executable
::set "HISTFILE=%~dp0.ash_history"
:: Another way to locate the history file is to set HOME dir
::for %%p in ( "%~dp0." ) do set "HOME=%%~fp"
:: ========================================================================
if defined BB_DEBUG (
@prompt +$S
@echo on
)
"%BB_EXE%" sh "%~f0" %*
exit /b %ERRORLEVEL%
:: ========================================================================
____CMD____
case "$1" in
'' )
# It's never reachable part - learn why at the top of the script
exit
;;
--help | --list | --list-full )
busybox $1
exit
;;
--version )
busybox --help | head -2
exit
;;
esac
# Add the BusyBox location to the $PATH
[[ ";$PATH;" =~ ";$( dirname "$0" );" ]] \
|| PATH="$( dirname "$0" );$PATH"
[ -z "$BB_DEBUG" ] || set -x
case "$1" in
-* | +* )
sh "$@"
;;
* )
eval 'exec "$@"'
;;
esac
# =========================================================================
# EOF
clp
Скрипт для работы с буфером обмена в обе стороны (копировать в буфер, копировать из буфера)
#!/usr/bin/env sh
# =========================================================================
#
# Simplify handling with the clipboard in BusyBox under Windows
#
# Do something with data taken from the clipboard
# clp | ...
#
# Do something with data and keep in the clipboard
# ... | clp
#
# Convert with help of dos2unix and unix2dos enabled
#
# =========================================================================
clp() {
if [ "$1" = "-h" -o "$1" = "--help" ]
then
cat <<HELP
Usage: [ clp [-u|-d] | ] ... [ | clp [-u|-d] ]
Copy data from and/or to the clipboard
-u dos2unix
-d unix2dos
HELP
return
fi
if [ ! -t 0 ]
then
# ... | clp
case "$1" in
-u ) dos2unix ;;
-d ) unix2dos ;;
* ) cat - ;;
esac | clip
else
# clp | ...
# or simply output the clipboard
powershell -NoLogo -NoProfile -Command \
'Get-Clipboard -Raw | Write-Host -NoNewLine' \
| case "$1" in
-u ) dos2unix ;;
-d ) unix2dos ;;
* ) cat - ;;
esac
fi
}
# =========================================================================
clp "$@"
# =========================================================================
#EOF