簡體   English   中英

帶有包含 & 的變量的批處理腳本

[英]Batch script with variables containing &

我正在嘗試創建一個批處理文件,該文件根據批處理文件中的某些參數使用自定義參數執行命令 ( mvn tomcat7:run ):

@ECHO off
SETLOCAL EnableDelayedExpansion
set args=

:loop
IF NOT "%1"=="" (
    IF "%1"=="local-mysql" (
        set args=!args! -Dds.user=user
        set args=!args! -Dds.pwd=password
        set args=!args! -Dhibernate.hbm2ddl.auto=update
        set args=!args! -Dds.url=jdbc:mysql://localhost:3306/database_name?useUnicode=true&characterEncoding=UTF-8&charSet=UTF-8&useSSL=false
    )
    IF "%1"=="local-other-service" (
        set args=!args! -Dother.service.url=localhost:8085/other-service
    )
    SHIFT
    goto :loop
)
mvn tomcat7:run%args%
:theend

這不起作用,因為行set args=!args! -Dds.url=jdbc:mysql://localhost:3306/database_name?useUnicode=true&characterEncoding=UTF-8&charSet=UTF-8&useSSL=false set args=!args! -Dds.url=jdbc:mysql://localhost:3306/database_name?useUnicode=true&characterEncoding=UTF-8&charSet=UTF-8&useSSL=false ,因為&符號換行。 我試過使用轉義字符,但這似乎只是轉義一次到變量中,然后下次我嘗試讀取變量時出現同樣的問題。

有人可以幫忙嗎? 我是批處理菜鳥。

&符號不會換行,它只是分隔命令。 但是在雙引號內,大多數特殊字符都失去了它們的能力,所以只需引用你的set命令

set "args=!args! -Dds.user=user"
set "args=!args! -Dds.pwd=password"
set "args=!args! -Dhibernate.hbm2ddl.auto=update"
set "args=!args! -Dds.url=jdbc:mysql://localhost:3306/database_name?useUnicode=true&characterEncoding=UTF-8&charSet=UTF-8&useSSL=false"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM