我想找到Windows批处理副本,它与Bash的$@
对应,其中包含传递给脚本的全部参数的列表。 git
仍是我不得不去shift
? npm
检索脚本中全部参数的方法以下: 数组
@ECHO off ECHO The %~nx0 script args are... for %%I IN (%*) DO ECHO %%I pause
这是获取args并将其设置为env vars的一种至关简单的方法。 在此示例中,我仅将它们称为键和值。 app
将如下代码示例另存为“ args.bat”。 而后从命令行调用您保存的批处理文件。 例如:arg.bat --x 90 --y 120 ide
我提供了一些echo命令来逐步引导您完成此过程。 可是最终结果是--x的值为90,而--y的值为120(也就是说,若是您按照上面指定的示例运行;-))。 oop
而后,您能够使用“若是已定义”条件语句来肯定是否运行代码块。 所以,能够说运行:“ arg.bat --x hello-world”,而后能够使用语句“ IF DEFINED –x echo%-x%”,结果将为“ hello-world”。 若是运行批处理,应该更有意义。 this
@setlocal enableextensions enabledelayedexpansion @ECHO off ECHO. ECHO :::::::::::::::::::::::::: arg.bat example ::::::::::::::::::::::::::::::: ECHO :: By: User2631477, 2013-07-29 :: ECHO :: Version: 1.0 :: ECHO :: Purpose: Checks the args passed to the batch. :: ECHO :: :: ECHO :: Start by gathering all the args with the %%* in a for loop. :: ECHO :: :: ECHO :: Now we use a 'for' loop to search for our keys which are identified :: ECHO :: by the text '--'. The function then sets the --arg ^= to the next :: ECHO :: arg. "CALL:Function_GetValue" ^<search for --^> ^<each arg^> :: ECHO :: :: ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ECHO. ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ECHO :: From the command line you could pass... arg.bat --x 90 --y 220 :: ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ECHO. ECHO.Checking Args:"%*" FOR %%a IN (%*) do ( CALL:Function_GetValue "--","%%a" ) ECHO. ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ECHO :: Now lets check which args were set to variables... :: ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ECHO. ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ECHO :: For this we are using the CALL:Function_Show_Defined "--x,--y,--z" :: ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ECHO. CALL:Function_Show_Defined "--x,--y,--z" endlocal goto done :Function_GetValue REM First we use find string to locate and search for the text. echo.%~2 | findstr /C:"%~1" 1>nul REM Next we check the errorlevel return to see if it contains a key or a value REM and set the appropriate action. if not errorlevel 1 ( SET KEY=%~2 ) ELSE ( SET VALUE=%~2 ) IF DEFINED VALUE ( SET %KEY%=%~2 ECHO. ECHO ::::::::::::::::::::::::: %~0 :::::::::::::::::::::::::::::: ECHO :: The KEY:'%KEY%' is now set to the VALUE:'%VALUE%' :: ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ECHO. ECHO %KEY%=%~2 ECHO. REM It's important to clear the definitions for the key and value in order to REM search for the next key value set. SET KEY= SET VALUE= ) GOTO:EOF :Function_Show_Defined ECHO. ECHO ::::::::::::::::::: %~0 :::::::::::::::::::::::::::::::: ECHO :: Checks which args were defined i.e. %~2 ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ECHO. SET ARGS=%~1 for %%s in (%ARGS%) DO ( ECHO. ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ECHO :: For the ARG: '%%s' IF DEFINED %%s ( ECHO :: Defined as: '%%s=!%%s!' ) else ( ECHO :: Not Defined '%%s' and thus has no value. ) ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ECHO. ) goto:EOF :done
@echo off :start :: Insert your code here echo.%%1 is now:%~1 :: End insert your code here if "%~2" NEQ "" ( shift goto :start )
Windows版本(虽然须要socat) spa
C:\Program Files (x86)\Git\bin>type gitproxy.cmd socat STDIO PROXY:proxy.mycompany.de:%1:%2,proxyport=3128
设置: 命令行
C:\Users\exhau\AppData\Roaming\npm>git config --global core.gitproxy gitproxy.cmd
如下代码模拟一个数组(' params
')-接受脚本接收的参数,并将它们存储在变量params_1
.. params_n
,其中n
= params_0
=数组的元素数: code
@echo off rem Storing the program parameters into the array 'params': rem Delayed expansion is left disabled in order not to interpret "!" in program parameters' values; rem however, if a parameter is not quoted, special characters in it (like "^", "&", "|") get interpreted at program launch set /a count=0 :repeat set /a count+=1 set "params_%count%=%~1" shift if defined params_%count% ( goto :repeat ) else ( set /a count-=1 ) set /a params_0=count rem Printing the program parameters stored in the array 'params': rem After the variables params_1 .. params_n are set with the program parameters' values, delayed expansion can rem be enabled and "!" are not interpreted in the variables params_1 .. params_n values setlocal enabledelayedexpansion for /l %%i in (1,1,!params_0!) do ( echo params_%%i: "!params_%%i!" ) endlocal pause goto :eof