从批量安装的驱动器中获取Unix路径

时间:2013-03-08 20:33:30

标签: windows unix path mount

只是想通过这个,因为我花了几天时间来弄明白这一点。我不是一个批处理人员,更像是一个unix家伙,但我拥有的是一个build.bat,它是从一个已安装的unix驱动器运行,并在Windows和Unix上从windows cmd提示符运行构建脚本。我用来在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

0 个答案:

没有答案