『楼 主』:
转贴:DOS环境变量“全家福”
使用 LLM 解释/回答一下
DOS环境变量“全家福”
环境是DOS系统的变量存储区——一个高速暂存存储器,它保存于RAM中,是DOS系统保存重要信息以供批处理文件使用的场所。用户可以根据需要修正或改变它的信息。很多DOS参考书都忽略了它,但不管你知不知道,都在自觉或不自觉地使用着它。
如何造访环境
造访环境有两种方法,一种是通过SET命令来查看,另一种是使用DOS系统的DEBUG命令来查看计算机的RAM。SET命令直观、方便,大多数DOS用户都喜欢使用它来设置和查看DOS环境。
1.用SET命令
SET命令可用于设置、修改和查看环境变量,键入无参数的SET命令可显示当前环境设置。这些设置一般包括COMSPEC和PATH环境变量,MS-DOS用它们在磁盘上寻找程序。另外还有PROMPT、DIRCMD、COPYCMD等为DOS系统所使用的其它环境变量。这些环境变量大多在AUTOEXEC.BAT或CONFIG.SYS文件中用SET命令设置而生成。如下例:
C:\>SET
COMSPEC=C:\COMMAND.COM
PROMPT=$p$g
PATH=C:\WINDOWS;C:\;C:\DOS;C:\FOXPRO25;C:\UCDOS;C:\GYPC;C:\CCED;C:\HD;C:\SARP
TEMP=C:\DOS
2.通过DEBUG命令
DEBUG是DOS提供的调试程序的有力工具,利用它可以检验磁盘原始信息、查看内存单元、反汇编程序文件或生成新程序等等,然而DEBUG命令有一定的危险性,因此用户在使用过程中须注意。
DOS系统每运行一个程序都要生成一个256字节的内存块——程序段前缀PSP(Program Segment Prefix),它包含了一些比较重要的信息,如内存单元、矢量、命令行上键入的文件名、命令行本身(用户运行程序的命令行的拷贝)、各种保留缓冲器存储地点的信息。只要涉及环境,那些存储地点之一就会保存系统环境地址。
在DEBUG命令状态下查看环境,首先查看偏移量2C处的两个字节:
-D2CL2
1B41:0020 90 09
数据90 09是在某系统上得到的计算机环境的段地址值(用户在操作时应以实际显示数为准),把这两对数以相反的顺序,并在后面跟上偏移量0,就能在DEBUG命令上显示系统环境。如下例:
-D0990:0
0990:0000 43 4F 4D 53 50 45 43 3D-43 3A 5C 43 4F 4D 4D 41 COMSPEC=C:\COMMA
0990:0010 4E 44 2E 43 4F 4D 00 50-52 4F 4D 50 54 3D 24 70 ND.COM.PROMPT=$p
0090:0020 24 67 00 50 41 54 48 3D-43 3A 5C 57 49 4E 44 4F $g.PATH=C:\WINDO
0990:0030 57 53 3B 43 3A 5C 3B 43-3A 5C 44 4F 53 3B 43 3A WS;C:\;C:\DOS;C:
0990:0040 5C 46 4F 58 50 52 4F 32-35 3B 43 3A 5C 55 43 44 \FOXPRO25;C:\UCD
0990:0050 4F 53 3B 43 3A 5C 47 59-50 43 3B 43 3A 5C 43 43 OS;C:\GYPC;C:\CC
0990:0060 45 44 3B 43 3A 5C 48 44-3B 43 3A 5C 53 41 52 50 ED;C:\HD;C:\SARP
0990:0070 00 54 45 4D 50 3D 43 3A-5C 44 4F 53 00 00 01 00 .TEMP=C:\DOS...
从上面所显示的环境可以看到,环境中的每项都被“空”字节00隔开,并且环境中的最后一项后面是两个空字符。
上面两种方法所显示的环境,其结果是一致的。
环境变量“众生相”
1.COMSPEC变量
这个变量规定了COMMAND.COM文件的位置。通常,如果计算机由硬盘驱动器引导 COMSPEC变量表示为:
COMSPEC=C:\COMMAND.COM
如果是从软盘驱动器引导的,则COMSPEC变量表示为:
COMSPEC=A:\COMMAND.COM
使用SET命令能改变COMSPEC的位置,格式为:
SET COMSPEC=C:\DOS\COMMAND.COM
此命令在系统环境中把COMSPEC重新设置成C盘DOS目录中的COMMAND.COM文件,因为COMMAND.COM不需要在用户的根目录中。用户也可以把COMMAND.COM放进其它子目录中,采用CONFIG.SYS的SHELL命令来完成这项工作。在计算机的启动过程中,CONFIG.SYS是在COMMAND.COM装入之前运行的,用SHELL命令通知DOS系统COMMAND.COM在另一个目录中,则系统到那个目录去找它。这有一个明显的优点,它保持了根目录的清洁。
另外,为保证系统能正常运行,用户还需在CONFIGSYS文件中加上这样一句:
SHELL=C:\DOS\COMMAND.COM/P
此命令指示DOS在C:\DOS子目录中寻找并运行命令解释程序或外壳程序COMMAND.COM。如果把此命令加到CONFIG.SYS文件中,可提前把COMMAND.COM移到DOS子目录中。P选项指示COMMAND.COM在根目录中寻找AUTOEXEC.BAT文件并且运行它,如果没有此选项,则根目录下的AUTOEXEC.BAT文件不能运行。
当DOS系统没有更多的空间存储环境变量时,将会看到信息:
Out of environment space(环境空间溢出)
出现这种情况后,可利用SHELL命令加大COMMAND.COM的环境空间,可选项E:1024就是将环境空间扩大到1KB(1024字节)。
2.PROMPT变量
这个环境变量显示用户所设置的命令提示符。DOS系统提示符一般设置为显示当前驱动器和路径后接一“>”符号,当然也可以设置为其它类型的命令提示符,这个变量一般在AUTOEXECBAT文件中设置,命令使用的一般格式为:
PROMPT $p$g
当然,你也可以使用SET PROMPT=$p$g。
3.PATH变量
这个环境变量包括用PATH命令设置路径,使DOS系统能在指定的目录中搜索可执行的文件。
4.DIRCMD变量
这个环境变量让用户确定DIR命令缺省使用的开关和参数集。用户可在AUTOEXEC.BAT文件中或命令提示符下使用SET命令定义DIRCMD环境变量,预先设置DIR的参数和选项的合法组合(可包括路径和文件名)。
例如,要把DIRCMD环境变量设置为满屏时暂停(/P)为缺省格式,在AUTOEXEC.BAT批处理文件或DOS命令提示符下键入:
SET DIRCMD=/P
这时使用DIR命令,在满一屏时将暂停并提示“按任意键继续”。
若不想使用这个缺省格式,可在命令行上使用:
DIR/-P
如要清除此缺省设置,在命令提示符下键入:
SET DIRCMD=
5.COPYCMD变量
用户可以通过设置COPYCMD环境变量,指定COPY、MOVE、XCOPY命令是否先给出提示,经确认后再覆盖文件。
若强制要求COPY、MOVE、XCOPY命令在所有情况下均先给出提示:
Overwrite Filename(YES/NO/ALL)?
就把COPYCMD环境变量设置成/-Y(SET COPYCMD=/-Y),用户可以根据需要来选择是否覆盖。也可以强制要求COPY、MOVE、XCOPY命令在所有情况下都不提示就进行覆盖 , 则把COPYCMD环境变量设置成/Y(SET COPYCMD=/Y)。这里所设置的COPYCMD环境变量,优先于COPYCMD环境变量的所有缺省值和当前值。
6.TEMP变量
TEMP是一个常用的环境变量,它告诉程序在何处建立临时文件,而有一些程序需要使用环境变量来识别它们要使用的目录。如下例:
SET TEMP=C:\DOS
在此例中,环境变量TEMP被DOS环境和一些其它程序使用,以确定当前文件子目录的位置。上面的目录C:\DOS被放入环境中,现在DOS系统知道把它们当前的文件放在哪里。在AUTOEXEC.BAT文件中,用户能够也应该设置环境的整个转换。
环境变量与批处理文件的亲密关系
使用环境变量可以控制某些批处理文件和使程序可按照用户的意愿进行,可以控制MS-DOS显示与工作的方式。一般在AUTOEXECBAT或CONFIGSYS文件中用SET命令设定用户环境,以便每次启动计算机时系统都能根据用户需要自动设置环境变量。
1.在批处理文件中调用环境变量
从批处理文件中调用环境变量值时,必须用百分符(%)将变量值括起来。如设置名为WIN32的变量,使它等于字符串C:\Windows\SYSTEM(C盘中的目录),可键入以下命令:
SET WIN32=C:\WINDOWS\SYSTEM
在批处理文件中可用%WIN32%代替C:\WINDOWS\SYSTEM。在批处理文件中包括以下命令,显示C:\WINDOWS\SYSTEM环境变量的目录内容:
DIR %WIN32%
当MS-DOS处理该命令时,会用字符串C:\WINDOWS\SYSTEM代替%WIN32%。
2.在批处理文件中保存和恢复原有环境下的路径
对每一个批处理文件,用户都可以修改环境变量,而各个批处理文件需要不同的PATH指明路径,以执行批处理下的程序,这就需要用户保存原有环境下的路径。用户可在批处理文件中使用一个环境变量暂时存储用户原来的路径,以便在需要时可以恢复,而不需要重新用PATH命令来设置:
SET OLDPATH=%PATH%
例如某计算机的设置如下:
SET OLDPATH=C:\WINDOWS;C:\; C:\DOS;C:\FOXPRO25; C:\UCDOS; C:\GYPC; C:\CCED;C:\HD; C:\SARP
批处理文件解释程序把%PATH%变量扩展成用户的当前路径,故OLDPATH变量等于此路径。如果此时系统因使用其它批处理文件而打乱了原系统路径,可简单地在批处理文件中使用下列语句恢复路径的原貌,以满足用户对不同环境的需要。如下例:
PATH %OLDPATH%
计算机显示它的执行情况:
PATH C:\WINDOWS;C:\;C:\DOS;C:\FOXPRO25;C:\UCDOS;C:\GYPC;C:\CCED;C:\HD;C:\SARP
其实,很多用户都可能在各自的AUTOEXECBAT文件中存有“OLDPATH”变量,由于此环境变量总包含原有路径的备份,因此可以很容易地恢复原有路径。
小结
环境是DOS系统很重要的内容之一,它是DOS系统保存重要信息供批处理文件使用的变量存储区——一个高速暂存存储器。它在此保存重要的信息,且用户能把批处理文件的环境变量放入其中。
SET命令对环境而言是相当重要的,它被用来给变量赋值、清除变量,并显示环境内容。
除了用户生成环境变量(COMSPEC)给出COMMANDCOM的位置外,PATH设置系统的搜索路径;PROMPT设置系统提示符;COPYCMD环境变量指定COPY、MOVE、XCOPY命令是否对要覆盖的文件进行提示;DIRCMD环境变量可以预置DIR参数和开关项。
由于环境表相当小,SHELL命令可用于扩大其容量,这虽然避免了“环境空间溢出(Out of environment space)”这个错误,但同时也多占了系统的RAM。
DOS Environment Variables "Family Portrait"
The environment is the variable storage area of the DOS system - a high-speed temporary memory, which is stored in RAM and is the place where the DOS system stores important information for batch files. Users can modify or change its information as needed. Many DOS reference books ignore it, but whether you know it or not, you are using it consciously or unconsciously.
### How to Visit the Environment
There are two ways to visit the environment. One is to view it through the SET command, and the other is to use the DOS system's DEBUG command to view the computer's RAM. The SET command is intuitive and convenient, and most DOS users like to use it to set and view the DOS environment.
#### 1. Using the SET Command
The SET command can be used to set, modify, and view environment variables. Typing the SET command without parameters can display the current environment settings. These settings generally include the COMSPEC and PATH environment variables, which MS-DOS uses to find programs on the disk. There are also other environment variables used by the DOS system, such as PROMPT, DIRCMD, COPYCMD, etc. These environment variables are mostly set and generated using the SET command in the AUTOEXEC.BAT or CONFIG.SYS file. For example:
```
C:\>SET
COMSPEC=C:\COMMAND.COM
PROMPT=$p$g
PATH=C:\WINDOWS;C:\;C:\DOS;C:\FOXPRO25;C:\UCDOS;C:\GYPC;C:\CCED;C:\HD;C:\SARP
TEMP=C:\DOS
```
#### 2. Through the DEBUG Command
DEBUG is a powerful tool of the DOS-provided debugging program. It can be used to test disk raw information, view memory units, disassemble program files, or generate new programs, etc. However, the DEBUG command has certain risks, so users need to be careful during use.
Every time the DOS system runs a program, it generates a 256-byte memory block - the Program Segment Prefix (PSP), which contains some important information, such as memory units, vectors, the file name typed on the command line, the command line itself (a copy of the user's command line to run the program), and information on the storage locations of various reserved buffers. As long as it involves the environment, one of those storage locations will store the system environment address.
To view the environment under the DEBUG command state, first view the two bytes at offset 2C:
```
-D2CL2
1B41:0020 90 09
```
The data 90 09 is the segment address value of the computer environment obtained on a certain system (users should use the actual displayed number when operating). Reversing the order of these two pairs of numbers and following them with offset 0 can display the system environment under the DEBUG command. For example:
```
-D0990:0
0990:0000 43 4F 4D 53 50 45 43 3D-43 3A 5C 43 4F 4D 4D 41 COMSPEC=C:\COMMA
0990:0010 4E 44 2E 43 4F 4D 00 50-52 4F 4D 50 54 3D 24 70 ND.COM.PROMPT=$p
0090:0020 24 67 00 50 41 54 48 3D-43 3A 5C 57 49 4E 44 4F $g.PATH=C:\WINDO
0990:0030 57 53 3B 43 3A 5C 3B 43-3A 5C 44 4F 53 3B 43 3A WS;C:\;C:\DOS;C:\
0990:0040 5C 46 4F 58 50 52 4F 32-35 3B 43 3A 5C 55 43 44 \FOXPRO25;C:\UCD
0990:0050 4F 53 3B 43 3A 5C 47 59-50 43 3B 43 3A 5C 43 43 OS;C:\GYPC;C:\CC
0990:0060 45 44 3B 43 3A 5C 48 44-3B 43 3A 5C 53 41 52 50 ED;C:\HD;C:\SARP
0990:0070 00 54 45 4D 50 3D 43 3A-5C 44 4F 53 00 00 01 00 .TEMP=C:\DOS...
```
From the displayed environment above, each item in the environment is separated by the "empty" byte 00, and there are two null characters after the last item in the environment.
The results of the environment displayed by the above two methods are consistent.
### Environment Variables "Portrait"
#### 1. COMSPEC Variable
This variable specifies the location of the COMMAND.COM file. Usually, if the computer is booted from a hard disk drive, the COMSPEC variable is expressed as:
```
COMSPEC=C:\COMMAND.COM
```
If it is booted from a floppy disk drive, the COMSPEC variable is expressed as:
```
COMSPEC=A:\COMMAND.COM
```
The SET command can be used to change the location of COMSPEC. The format is:
```
SET COMSPEC=C:\DOS\COMMAND.COM
```
This command resets COMSPEC in the system environment to the COMMAND.COM file in the C drive DOS directory. Because COMMAND.COM does not need to be in the user's root directory. Users can also put COMMAND.COM into other subdirectories, which can be done using the SHELL command in CONFIG.SYS. During the computer startup process, CONFIG.SYS runs before COMMAND.COM is loaded. Using the SHELL command to notify the DOS system that COMMAND.COM is in another directory, the system will look for it in that directory. This has an obvious advantage, which keeps the root directory clean.
In addition, to ensure the normal operation of the system, users also need to add the following sentence in the CONFIG.SYS file:
```
SHELL=C:\DOS\COMMAND.COM /P
```
This command instructs DOS to find and run the command interpreter or shell program COMMAND.COM in the C:\DOS subdirectory. If this command is added to the CONFIG.SYS file, COMMAND.COM can be moved to the DOS subdirectory in advance. The P option instructs COMMAND.COM to find the AUTOEXEC.BAT file in the root directory and run it. Without this option, the AUTOEXEC.BAT file in the root directory cannot be run.
When the DOS system has no more space to store environment variables, the information "Out of environment space" will be seen. After this situation occurs, the SHELL command can be used to increase the environment space of COMMAND.COM. The optional E:1024 is to expand the environment space to 1KB (1024 bytes).
#### 2. PROMPT Variable
This environment variable displays the command prompt set by the user. The DOS system prompt is generally set to display the current drive and path followed by a ">" symbol. Of course, it can also be set to other types of command prompts. This variable is generally set in the AUTOEXEC.BAT file. The general format of the command is:
```
PROMPT $p$g
```
Of course, you can also use SET PROMPT=$p$g.
#### 3. PATH Variable
This environment variable includes setting the path using the PATH command, so that the DOS system can search for executable files in the specified directory.
#### 4. DIRCMD Variable
This environment variable allows the user to determine the default switches and parameter sets used by the DIR command. The user can use the SET command in the AUTOEXEC.BAT file or at the command prompt to define the DIRCMD environment variable, and pre-set the legal combination of DIR parameters and options (which can include paths and file names).
For example, to set the DIRCMD environment variable to the default format of pausing when full screen (/P), type the following in the AUTOEXEC.BAT batch file or the DOS command prompt:
```
SET DIRCMD=/P
```
At this time, when using the DIR command, it will pause when full screen and prompt "Press any key to continue".
If you don't want to use this default format, you can use it on the command line:
```
DIR /-P
```
If you want to clear this default setting, type the following at the command prompt:
```
SET DIRCMD=
```
#### 5. COPYCMD Variable
The user can set the COPYCMD environment variable to specify whether the COPY, MOVE, and XCOPY commands give a prompt first and then overwrite the file after confirmation.
If you force the COPY, MOVE, and XCOPY commands to give a prompt in all cases:
```
Overwrite Filename(YES/NO/ALL)?
```
Set the COPYCMD environment variable to /-Y (SET COPYCMD=/-Y). The user can choose whether to overwrite according to needs. You can also force the COPY, MOVE, and XCOPY commands to overwrite without prompting in all cases, then set the COPYCMD environment variable to /Y (SET COPYCMD=/Y). The COPYCMD environment variable set here takes precedence over all default values and current values of the COPYCMD environment variable.
#### 6. TEMP Variable
TEMP is a commonly used environment variable. It tells the program where to create temporary files, and some programs need to use the environment variable to identify the directory they want to use. For example:
```
SET TEMP=C:\DOS
```
In this example, the environment variable TEMP is used by the DOS environment and some other programs to determine the location of the current file subdirectory. The above directory C:\DOS is put into the environment, and now the DOS system knows where to put its current files. In the AUTOEXEC.BAT file, the user can and should set the entire conversion of the environment.
### The Close Relationship Between Environment Variables and Batch Files
Using environment variables can control certain batch files and make programs work according to the user's wishes, and can control the way MS-DOS displays and works. Generally, the user environment is set using the SET command in the AUTOEXEC.BAT or CONFIG.SYS file, so that the system can automatically set the environment variables according to the user's needs every time the computer is started.
#### 1. Calling Environment Variables in Batch Files
When calling the value of an environment variable from a batch file, the variable value must be enclosed in percent signs (%). For example, set a variable named WIN32 to be equal to the string C:\Windows\SYSTEM (the directory on the C drive), you can type the following command:
```
SET WIN32=C:\WINDOWS\SYSTEM
```
In the batch file, %WIN32% can be used instead of C:\WINDOWS\SYSTEM. Including the following command in the batch file displays the directory content of the C:\WINDOWS\SYSTEM environment variable:
```
DIR %WIN32%
```
When MS-DOS processes this command, it will replace %WIN32% with the string C:\WINDOWS\SYSTEM.
#### 2. Saving and Restoring the Original Path in the Environment in Batch Files
For each batch file, the user can modify the environment variable, and each batch file needs a different PATH to indicate the path to execute the program under the batch file. This requires the user to save the original path in the environment. The user can use an environment variable in the batch file to temporarily store the user's original path, so that it can be restored when needed without re-setting with the PATH command:
```
SET OLDPATH=%PATH%
```
For example, the settings of a certain computer are as follows:
```
SET OLDPATH=C:\WINDOWS;C:\; C:\DOS;C:\FOXPRO25; C:\UCDOS; C:\GYPC; C:\CCED;C:\HD; C:\SARP
```
The batch file interpreter expands the %PATH% variable into the user's current path, so the OLDPATH variable is equal to this path. If the system disrupts the original system path due to using other batch files, you can simply use the following statement in the batch file to restore the original path to meet the user's needs for different environments. For example:
```
PATH %OLDPATH%
```
The computer displays its execution situation:
```
PATH C:\WINDOWS;C:\;C:\DOS;C:\FOXPRO25;C:\UCDOS;C:\GYPC;C:\CCED;C:\HD;C:\SARP
```
In fact, many users may have the "OLDPATH" variable in their respective AUTOEXEC.BAT files. Since this environment variable always contains a backup of the original path, the original path can be easily restored.
### Summary
The environment is one of the very important contents of the DOS system. It is the variable storage area - a high-speed temporary memory - where the DOS system stores important information for batch files to use. It stores important information here, and users can put the environment variables of batch files into it.
The SET command is quite important for the environment. It is used to assign values to variables, clear variables, and display the environment content.
In addition to the user-generated environment variable (COMSPEC) giving the location of COMMAND.COM, PATH sets the system's search path; PROMPT sets the system prompt; the COPYCMD environment variable specifies whether the COPY, MOVE, and XCOPY commands prompt for files to be overwritten; the DIRCMD environment variable can pre-set DIR parameters and switches.
Since the environment table is quite small, the SHELL command can be used to expand its capacity. Although this avoids the error "Out of environment space", it also occupies more system RAM.
|