命令操作符
1.
元字符
在命令行中有特殊含义的一些字符,通过这些字符表示一些特殊含义,称为元字符。它们包括:
(1)
分隔符:包括空白、分号
(;)
、逗号
(,)
和双引号
分号和逗号分割参数和命令,如果命令和参数之间的用它们连接则默认第一个参数为空。但是他们都会包含在
%*
中
(2)
环境变量的引用符号:
百分号
(%)
(3)
管道,重定向符,命令分隔符:
| & ^
(
escape
)
< >
2.
如何重定向命令输入和输出?
在
MS-DOS
下每个进程可以打开
10
个文件,其中有一些是默认的,这些文件用句柄来操作:
文件句柄是
0~9
的整数。可以通过输出和输入重定向改变对文件的读
/
写操作。文件句柄可以看作操纵文件的指针。重定向则是对这些指针重新赋值,使其志祥所指定的文件。
重定向操作符一共有五种:
|
输出重定向,可以是文件名,空设备
nul,
打印机
prn,
该操作符左端默认的文件句柄是
1
|
|
|
|
|
|
输出重定向到另外一个文件句柄,该操作符左端默认的文件句柄是
1
|
|
输入重定向到另外一个文件句柄,该操作符左端默认的文件句柄是
0
|
|
|
重定向命令按照在命令行中的顺序进行,例如:
<command> >> <log-file> 2>&1
上述命令首先将标准输出重定向到
<log-file>
,然后将其复制到标准错误输出。上述命令中的顺序不能改变。
另外,重定向的操作是单向的,即对读写操作是分开的:
<command> 3><output-file>
上述命令仅将对文件句柄
3
的输出定向到
<output-file>
并不影响从文件句柄读取原来的输入。
3.
如何理解管道?
管道命令的格式如下:
一句话,该命令将
<command1>
的标准输出重定向至管道,将
<command2>
的标准输入重定向至管道。
4.
如何定义复合语句?
复合语句由一些命令操作符来连接:
|
|
|
|
|
只有当第一个命令执行成功后才执行第二个命令(由命令的错误代码决定)
|
|
|
只有当第一个命令执行失败后才执行第二个命令(由命令的错误代码大于
0
决定)
|
|
( <
Command1>
& <Command2>)
|
|
|
<Command1>
<Parameter1>;<Parameter2>
|
|
5.
命令通配符
(一些命令中某些区域)
在有些命令的某些区域,可以使用通配符
(wildcard),
通配符有两个:
?
–
匹配任意一个字符
*
–
匹配任意多个字符
实用命令
(in updating)
Sort
Find
more
环境变量
1.
环境变量如何初始化?
命令解释器某一运行时刻的环境变量的来源和初始化依次如下建立:
-
命令解释器内置变量
-
系统变量(在
HKEY_LOCAL_MACHINE
下定义)系统管理员设定
-
本地变量(在
HKEY_CURRENT_USER
下定义)用户指定
-
Autoexec.bat
定义的变量
-
登陆脚本定义的变量
-
用户交互命令定义的变量
2.
常用环境变量
|
|
Returns the location of the All Users Profile.
|
|
|
Returns the location where applications store data by default.
|
|
|
Returns the current directory string.
|
|
|
Returns the exact command line used to start the current Cmd.exe.
|
|
|
Returns the version number of the current Command Processor Extensions.
|
|
|
Returns the name of the computer.
|
|
|
Returns the exact path to the command shell executable.
|
|
|
Returns the current date. Uses the same format as the date /t command.
|
|
|
Returns the error code of the most recently used command. A non zero value usually indicates an error.
|
|
|
Returns which local workstation drive letter is connected to the user's home directory. Set based on the value of the home directory. The user's home directory is specified in Local Users and Groups.
|
|
|
Returns the full path of the user's home directory. Set based on the value of the home directory. The user's home directory is specified in Local Users and Groups.
|
|
|
Returns the network path to the user's shared home directory. Set based on the value of the home directory. The user's home directory is specified in Local Users and Groups.
|
|
|
Returns the name of the domain controller that validated the current logon session.
|
|
|
Specifies the number of processors installed on the computer.
|
|
|
Returns the operating system name. Windows 2000 displays the operating system as Windows_NT.
|
|
|
Specifies the search path for executable files.
|
|
|
Returns a list of the file extensions that the operating system considers to be executable.
|
|
|
Returns the chip architecture of the processor. Values:
x86
or IA64 (
Itanium
–based).
|
|
|
Returns a description of the processor.
|
|
|
Returns the model number of the processor installed on the computer.
|
|
|
Returns the revision number of the processor.
|
|
|
Returns the command prompt settings for the current interpreter. Generated by Cmd.exe.
|
|
|
Returns a random decimal number between 0 and 32767. Generated by Cmd.exe.
|
|
|
Returns the drive containing the Windows server operating system root directory (that is, the system root).
|
|
|
Returns the location of the Windows server operating system root directory.
|
|
|
Returns the default temporary directories that are used by applications available to users who are currently logged on. Some applications require TEMP and others require TMP.
|
|
|
Returns the current time. Uses the same format as the time /t command.
|
|
|
Returns the name of the domain that contains the user's account.
|
|
|
Returns the name of the user who is currently logged on.
|
|
|
Returns the location of the profile for the current user.
|
|
|
Returns the location of the operating system directory.
|
3.
如何设置包括特殊字符的环境变量?
如果使用包含特殊字符
<, >, |, &, or ^,
的环境变量,则需要在前面加上转义字符
^
,该字符即使在引号中也有效果。
如果定义含有引号的环境变量,则引号也是变量值的一部分。
环境变量的定义到行尾,不包括行尾空白。