1

只是想把它传递下去,因为我花了几天时间才弄清楚这一点。我不是批处理人,更像是 unix 人,但我拥有的是一个 build.bat,它从已安装的 unix 驱动器运行,并从 windows cmd 提示符在 Windows 和 Unix 上运行构建脚本。我曾经将路径硬编码在 bat 文件中,但我希望能够将脚本放在 Unix 上的任何位置,并允许用户将源签出到他们自己的沙箱,安装驱动器打开 cmd 提示符,cd 到脚本目录,然后就可以了运行构建。有许多进程从此脚本运行。所以我不得不从挂载的驱动器中获取路径。再次 - 我确信可能有更简单或更短的方法,但这对我有用:

@echo off

set USERNAME=%1
set PASSWORD=%2

setlocal enabledelayedexpansion

:: All scripts are run from the same script directory on the mounted unix drive
:: 
:: Get the unix path to the scripts directory by using the following command
::
::    net use %~d0
:: 
:: %~d0  - drive letter where the script is running from
:: %~dp0 - complete path to this script
::
set WIN_PATH=%~dp0
set DRIVE_LETTER=%~d0

:: get "Remote Name" from net use command
FOR /F "tokens=*" %%i in ('net use %DRIVE_LETTER% ^| FINDSTR /I "^Remote"') DO SET NET_USE_OUTPUT=%%i

:: Get mounted unix drive from net use drive_letter, returns //ServerName/mount
FOR /F "tokens=3" %%A IN ("%NET_USE_OUTPUT%") DO SET MOUNTED_DRIVE=%%A

::  substitute drive_Letter for mounted_drive \\serverName\mount\path\to\scripts 
set WIN_MOUNTED_PATH=!WIN_PATH:%DRIVE_LETTER%=%MOUNTED_DRIVE%!

::  Change to win to Unix slashes //serverName/mount/path/to/scripts
set UNX_MOUNTED_PATH=%WIN_MOUNTED_PATH:\=/%

:: get SERVER_NAME 
FOR /F "tokens=1 delims=/" %%A IN ("%UNX_MOUNTED_PATH%") DO SET "SERVER_NAME=%%A"

:: Remove //serverName from full path - not needed on unix server
SET "SERVER_NAME_REMOVE=*//%SERVER_NAME%"

:: Remove serverName to get unix path to scripts directory
::  SCRIPTS = /path/to/scripts
CALL SET SCRIPTS=%%UNX_MOUNTED_PATH:!SERVER_NAME_REMOVE!=%%

:: Run script on Unix using plink
pink -pw %PASSWORD% %USERNAME%@%SERVER_NAME% "%SCRIPTS%/run.sh"

:: Run script on Windows
%WIN_PATH%run.bat
4

0 回答 0