嵌套环境变量 [英] Nested environment variables
问题描述
我正在尝试创建一个注册要求您输入用户名和密码的文件.注册时,密码保存在变量中.
I am trying to make a file that asks for your username and password, with a registration. When registering, the passwords are saved in variables.
问题是我必须为这些变量设置名称,这些名称将在您写下用户名时输入.例如,我需要用户"john"的密码.用户写下用户名john时,会将其存储在变量%user1%
中.由于我需要创建一个变量%johnpass%
和"john" = %user1%
,因此我必须创建一个名为 %% user1%pass%的变量
(变量内含一个变量.疯狂!)
The problem is i have to set names for those variables, names that would be input when you write your username down. for example, i need a password for the user "john". When the username john is written down by a user, it is stocked in a variable, %user1%
. As i need to make a variable %johnpass%
and "john"= %user1%
, i have to therefore make a variable called %%user1%pass%
(Variable INSIDE a variable.CRAZY!)
还有另一种方法吗?
简单版本:
- 用户输入用户名"john"
- "john"保存在变量
%user1%
中 - 他写了一个密码"1q2p"
- 如何将密码保存在变量中:
%user1%
的密码 -
%% user1%pass%
不起作用
- User writes username "john"
- "john" is saved in a variable,
%user1%
- he writes a password "1q2p"
- how can i save this password in a variable: which would be:
%user1%
's password %%user1%pass%
is not working
我真的很想找出答案!如果您有用于注册和登录的脚本,请告诉!!!预先感谢.
I really want to find out! If you have a script for registration and login, please tell!!! Thanks in advance.
推荐答案
您的方法有一个重要的缺点:您如何知道要管理多少个不同的变量?您如何知道下一个用户将使用 user2
变量和下一个 user3
?您必须同意,如果只有一个用户,则password变量不需要用户名,并且可以被命名为 pass
,对吗?
You method have an important drawback: How do you know how many different variables are you managing? How do you know that the next user will use user2
variable and the next one user3
? You must agree that if there is just one user then password variable don't require the user name and could be named just pass
, right?
处理这种情况的标准方法是通过 array :您使用具有相同名称(数组)的变量,并通过<下标.在您的情况下,您可能有两个数组: user
和 pass
,它们都具有从1开始的相同数字下标.例如:
The standard method to deal with this situation is via an array: you use a variable with same name (the array) and select different values from it via a subscript. In your case, you may have two arrays: user
and pass
, both with the same numeric subscript starting from 1. For example:
@echo off
setlocal EnableDelayedExpansion
set index=0
:nextUser
set /A index+=1
set /P "user[%index%]=Username: "
set /P "pass[%index%]=Password: "
rem Display data of user[1]:
echo %user[1]%'s password is %pass[1]%
rem Display data of all users, from 1 to %index%
for /L %%i in (1,1,%index%) do (
echo !user[%%i]!'s password is !pass[%%i]!
)
您可以在这篇文章中阅读批处理文件中有关阵列管理的详细说明:数组,链接列表和cmd.exe(批处理)脚本中的其他数据结构
You may read a detailed description about array management in Batch files at this post: Arrays, linked lists and other data structures in cmd.exe (batch) script
这篇关于嵌套环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!