用于导入 Virtual Box 设备的批处理脚本在共享文件夹路径末尾添加引号
Batch script to import Virtual Box appliance adding quotation marks to end of shared folder path
我创建了一个批处理脚本,用于导入虚拟盒设备并为该机器配置它。部分脚本删除共享(在来宾和主机之间)并使用新的主机路径再次创建它,如下所示:
"%vbPath%VBoxManage" sharedfolder remove "%devBoxName%" --name wwwshare
"%vbPath%VBoxManage" sharedfolder add "%devBoxName%" --name wwwshare --hostpath "%shareName%"
当我将这些代码行放入一个单独的文件并直接在上面设置 shareName 时,它工作正常(例如 shareName=c:/test)。但是,当我 运行 它作为脚本的一部分时,创建的共享将以 c:/test" 作为共享路径,这当然意味着它失败了。
这是用于此变量的所有代码:
setlocal enabledelayedexpansion
:: Check they want the default share folder name
SET shareName=C:\ld-sd%devNum%-rh7-www\
:getShareName
SET /P shareNameOK="Your shared folder will be %shareName%. Do you want to keep this value (enter y to keep and n to change): "
cls
if /I "%shareNameOK%"=="n" (
SET /P shareName="Please enter the path and name you would like for your shared folder: "
GOTO :getShareName
) else (
if /I not "%shareNameOK%"=="y" (
echo WARNING: You did not enter y or n
GOTO :getShareName
)
)
:getShareCheck
:: Check if this folder exists and if they plan to keep or overwrite it if it does
if /I exist "%shareName%" (
echo WARNING: Selecting yes will result in all website files being erased!
SET /P shareNameOverwrite="This folder already exists, would you like to overwrite the files in it with a clean copy (yes/no)? "
if /I not "!shareNameOverwrite!"=="no" (
if /I "!shareNameOverwrite!"=="yes" (
@RD /S /Q "!shareName!"
timeout /t 1 /nobreak > NUL
mkdir "!shareName!"
) else (
echo WARNING: You did not enter yes or no - NOTE full name
GOTO :getShareCheck
)
)
) else (
SET shareNameOverwrite=yes
mkdir "!shareName!"
)
每当我回显变量(文件中的任何位置)时,它都没有显示引号。这是它引起问题的唯一一点。我什至回应了整行问题:
echo "%vbPath%VBoxManage" sharedfolder add "%devBoxName%" --name wwwshare --hostpath "%shareName%"
它显示正常。引号是哪里来的?
最后的问题是:
SET shareName=C:\ld-sd%devNum%-rh7-www\
应该是:
SET shareName=C:\ld-sd%devNum%-rh7-www
我忘了最后有一个\。
这样做的结果是,对于 \" 它将把 " 作为字符串的一部分而不是字符串的结尾。
因此,如果您在变量末尾有一个 rouge ",请确保您的变量不以反斜杠结尾!
我创建了一个批处理脚本,用于导入虚拟盒设备并为该机器配置它。部分脚本删除共享(在来宾和主机之间)并使用新的主机路径再次创建它,如下所示:
"%vbPath%VBoxManage" sharedfolder remove "%devBoxName%" --name wwwshare
"%vbPath%VBoxManage" sharedfolder add "%devBoxName%" --name wwwshare --hostpath "%shareName%"
当我将这些代码行放入一个单独的文件并直接在上面设置 shareName 时,它工作正常(例如 shareName=c:/test)。但是,当我 运行 它作为脚本的一部分时,创建的共享将以 c:/test" 作为共享路径,这当然意味着它失败了。 这是用于此变量的所有代码:
setlocal enabledelayedexpansion
:: Check they want the default share folder name
SET shareName=C:\ld-sd%devNum%-rh7-www\
:getShareName
SET /P shareNameOK="Your shared folder will be %shareName%. Do you want to keep this value (enter y to keep and n to change): "
cls
if /I "%shareNameOK%"=="n" (
SET /P shareName="Please enter the path and name you would like for your shared folder: "
GOTO :getShareName
) else (
if /I not "%shareNameOK%"=="y" (
echo WARNING: You did not enter y or n
GOTO :getShareName
)
)
:getShareCheck
:: Check if this folder exists and if they plan to keep or overwrite it if it does
if /I exist "%shareName%" (
echo WARNING: Selecting yes will result in all website files being erased!
SET /P shareNameOverwrite="This folder already exists, would you like to overwrite the files in it with a clean copy (yes/no)? "
if /I not "!shareNameOverwrite!"=="no" (
if /I "!shareNameOverwrite!"=="yes" (
@RD /S /Q "!shareName!"
timeout /t 1 /nobreak > NUL
mkdir "!shareName!"
) else (
echo WARNING: You did not enter yes or no - NOTE full name
GOTO :getShareCheck
)
)
) else (
SET shareNameOverwrite=yes
mkdir "!shareName!"
)
每当我回显变量(文件中的任何位置)时,它都没有显示引号。这是它引起问题的唯一一点。我什至回应了整行问题:
echo "%vbPath%VBoxManage" sharedfolder add "%devBoxName%" --name wwwshare --hostpath "%shareName%"
它显示正常。引号是哪里来的?
最后的问题是:
SET shareName=C:\ld-sd%devNum%-rh7-www\
应该是:
SET shareName=C:\ld-sd%devNum%-rh7-www
我忘了最后有一个\。
这样做的结果是,对于 \" 它将把 " 作为字符串的一部分而不是字符串的结尾。
因此,如果您在变量末尾有一个 rouge ",请确保您的变量不以反斜杠结尾!