如何在 Windows 批处理作业中访问环境变量?

发布于 2025-01-04 07:10:40 字数 1104 浏览 0 评论 0原文

我正在尝试从命令提示符读取登录凭据,以便执行一系列 SQL*Loader 脚本。
我可以看到变量是使用 set v_ 设置的。但是,我无法访问这些值。

测试文件:

@echo off

set /P v_user = Username: 
set /P v_pass = Password: 
set /P v_db   = Database: 

ECHO Username: "%V_USER%"
ECHO Password: "%V_PASS%"
ECHO Database: "%V_DB%"

set v_

pause

实际结果 结果:

Username: 1    <-- Input
Password: 2    <-- Input
Database: 3    <-- Input
Username: ""  
Password: ""  
Database: ""  
v_db   =3  
v_pass =2  
v_user =1  

预期结果 结果:

Username: 1    <-- Input
Password: 2    <-- Input
Database: 3    <-- Input
Username: "1"  
Password: "2"  
Database: "3"  
v_db   =3  
v_pass =2  
v_user =1  

预期用途:

@echo off

set /P v_user = Username: 
set /P v_pass = Password: 
set /P v_db   = Database: 

sqlldr %v_user%/%v_pass%@%v_db% CONTROL='TABLE_T1.ctl' log='logs/TABLE_T1.log' bad='bad/TABLE_T1.bad' skip=1
sqlldr %v_user%/%v_pass%@%v_db% CONTROL='TABLE_T2.ctl' log='logs/TABLE_T2.log' bad='bad/TABLE_T2.bad' skip=1

pause

I'm trying to read login cridentials from the command prompt in order to execute a series of SQL*Loader Scripts.
I am able to see that the variable got set using set v_. However, I can't access the values.

Test File:

@echo off

set /P v_user = Username: 
set /P v_pass = Password: 
set /P v_db   = Database: 

ECHO Username: "%V_USER%"
ECHO Password: "%V_PASS%"
ECHO Database: "%V_DB%"

set v_

pause

Actual Result Result:

Username: 1    <-- Input
Password: 2    <-- Input
Database: 3    <-- Input
Username: ""  
Password: ""  
Database: ""  
v_db   =3  
v_pass =2  
v_user =1  

Expected Result Result:

Username: 1    <-- Input
Password: 2    <-- Input
Database: 3    <-- Input
Username: "1"  
Password: "2"  
Database: "3"  
v_db   =3  
v_pass =2  
v_user =1  

Intended Use:

@echo off

set /P v_user = Username: 
set /P v_pass = Password: 
set /P v_db   = Database: 

sqlldr %v_user%/%v_pass%@%v_db% CONTROL='TABLE_T1.ctl' log='logs/TABLE_T1.log' bad='bad/TABLE_T1.bad' skip=1
sqlldr %v_user%/%v_pass%@%v_db% CONTROL='TABLE_T2.ctl' log='logs/TABLE_T2.log' bad='bad/TABLE_T2.bad' skip=1

pause

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后来的我们 2025-01-11 07:10:41

删除 set 语句中 = 周围的空格:

echo off

set /P v_user=Username: 
set /P v_pass=Password: 
set /P v_db=Database: 

ECHO Username: "%V_USER%"
ECHO Password: "%V_PASS%"
ECHO Database: "%V_DB%"

set v_

pause

另一种方法是更改​​ ECHO 语句,使其包含与变量名和 =< 之间使用的 SET 语句相同数量的尾随空格。 /代码> 标志:

@echo off

set /P v_user = Username: 
set /P v_pass = Password: 
set /P v_db   = Database: 

ECHO Username: "%V_USER %"
ECHO Password: "%V_PASS %"
ECHO Database: "%V_DB   %"

set v_

pause

Remove the spaces around the = in the set statements:

echo off

set /P v_user=Username: 
set /P v_pass=Password: 
set /P v_db=Database: 

ECHO Username: "%V_USER%"
ECHO Password: "%V_PASS%"
ECHO Database: "%V_DB%"

set v_

pause

The other alternative is to change the ECHO statements to contain the same number of trailing spaces as the SET statement used between the variable name and = sign:

@echo off

set /P v_user = Username: 
set /P v_pass = Password: 
set /P v_db   = Database: 

ECHO Username: "%V_USER %"
ECHO Password: "%V_PASS %"
ECHO Database: "%V_DB   %"

set v_

pause
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文