batch-file - trying to set a value to a variable in batch
问题描述
i'm trying to write a simple script to do git add, commit and push.
@echo off
SET operation=%1
SET opParam=%2
SET mainBranch=dev/1.1
if %operation%==fire (
for /f %%i in ('git rev-parse --abbrev-ref HEAD') do set branchName=%%i
if %opParam%.==. (
SET opParam="autogeneratedmessage"
@echo on
echo %opParam%
@echo off
) else (
SET opParam="%opParam%"
@echo on
echo %opParam%
@echo off
)
git add -A
git commit -m %opParam%
git push origin %branchName%
)
So the problem is when the second parameter is empty, it fails to set the value of opParam
to autogeneratedmessage
at line 9.
Here is what i have tried so far to set the opParam:
SET opParam=""autogeneratedmessage""
SET opParam="autogeneratedmessage"
SET "opParam=autogeneratedmessage"
I really can't see what im doing wrong here. It seems like a simple issue but i've been on this for half an hour now.
解决方案
I had enable the delayed expansion in that block and reference my variable using !
instead of %
.
if %operation%==fire (
setlocal enabledelayedexpansion
for /f %%i in ('git rev-parse --abbrev-ref HEAD') do set branchName=%%i
if %opParam%.==. (
SET opParam="autogenerated"
@echo on
echo !opParam!
@echo off
) else (
SET opParam="%opParam%"
@echo on
echo %opParam%
@echo off
)
git add -A
git commit -m !opParam!
git push origin %branchName%
)
推荐阅读
- ruby-on-rails - Rails 找不到 webpacked 文件
- javascript - React Native Expo 音频播放器错误 - 播放器不存在
- mysql - MySQL 按月、年计数,如果没有则显示 0
- grails - grails - 找不到命令安装模板
- mysql - 如何在mysql中减去2个值集来填充数据
- spring-boot - 如何从集群中正在运行的 Spring Boot 应用程序中动态创建 Kubernetes 资源
- quartz-scheduler - 无法在 javax 电子邮件中创建 Mimemesage
- jquery - 在 jQuery 中同时使用淡入淡出和淡入淡出作为 CSS 中的过渡
- azure-devops - Azure DevOps 管道代理在 CNTLM 代理后面不起作用?
- xcode - 在 Xcode 日志中,'[number1:number2]' 是什么意思?