我试图通过批处理脚本自动加载带有一些SSH键的Putty Pageant,但因为我想绕过Pageant的错误消息,即它已经在运行,所以我将其放在一个IF语句中。然而,由于某些原因,它并不起作用:
tasklist /FI "IMAGENAME eq pageant.exe" 2>NUL | find /I /N "pageant.exe">NUL
if %ERRORLEVEL%==1 ( :: checks whether pageant.exe is running or not
:: set the SSH-keys
if exist "C:\SSH keys\key1.ppk" (set KEYS="C:\SSH keys\key1.ppk")
if exist "C:\SSH keys\key2.ppk" (set KEYS=%KEYS% "C:\SSH keys\key2.ppk")
if not defined KEYS (
msg * A SSH-key is propably missing.
)
:: Start pageant with the defined SSH-keys
start /d"C:\Program Files (x86)\PuTTY" pageant.exe %KEYS%
)当它们分开工作时:(1)
tasklist /FI "IMAGENAME eq pageant.exe" 2>NUL | find /I /N "pageant.exe">NUL
if %ERRORLEVEL%==1 ( :: checks whether pageant.exe is running or not
:: This works!
start /d"C:\Program Files (x86)\PuTTY" pageant.exe
)(2)
:: set the SSH-keys
if exist "C:\SSH keys\key1.ppk" (set KEYS="C:\SSH keys\key1.ppk")
if exist "C:\SSH keys\key2.ppk" (set KEYS=%KEYS% "C:\SSH keys\key2.ppk")
if not defined KEYS (
msg * A SSH-key is propably missing.
)
This works as well!
start /d"C:\Program Files (x86)\PuTTY" pageant.exe %KEYS%这是一个语法问题吗?
发布于 2013-01-06 05:56:10
如果没有错误消息,我只能猜测。
您是否启用了延迟扩展?请参见 .
将此代码添加到脚本setlocal EnableExtensions EnableDelayedExpansion中,并将endlocal添加到脚本的末尾。
这允许KEYS变量计算实际值。延迟扩展允许立即将值设置为变量,而不仅仅是在if语句的作用域结束时设置。另外,不要忘记对if语句中设置的变量使用!而不是%。
示例:(在.bat中使用EnableDepayedExpansion运行一次,没有运行一次,您就会看到区别。)
setlocal EnableExtensions EnableDelayedExpansion
set "Value=Hello World"
echo %Value%
if 1==1 (
set "Value=Goodbye World"
echo %Value%
echo !Value!
)
echo %Value%
endlocalhttps://stackoverflow.com/questions/14179640
复制相似问题