不同平台终端中执行的命令方式一般有两种:自身终端封装的指令(内置)、终端下调用其它目录下的可执行程序(外部)。
Linux
Linux下终端一般为/bin/bash、/bin/sh、/bin/zsh等,这里以bash终端为例测试。
以Linux为例【Windows等平台原理同Linux类似】,Linux下终端内建(内置)的指令类型为:shell built-in command。
所谓shell built-in command,就是那些内建在linux shell里面的command指令。
通常情况下,在linux shell下面执行一个command指令,shell会查找command是否为built-in command类型,对于built-in command指令类型,shell会自己解释执行,而无需fork一个child process子进程来执行该command指令;对于,不是built-in command指令类型,shell会从环境变量中按顺序搜索该command指令,如果能查到则会fork一个child process子进程来执行该command指令;然而,对于找不到的command指令,一般为:执行的指令不存在、指令未加入到环境变量中。
那么如何进行终端内建指令的判断与查看呢,对于内建指令可以使用type指令去判断
┌──(roottoor)-[~/桌面] └─# type echo echo is a shell builtin ┌──(roottoor)-[~/桌面] └─# type whoami whoami is /usr/bin/whoami ┌──(roottoor)-[~/桌面] └─#
或着也可以使用其它指令进行查找判断:which、where
┌──(roottoor)-[~/桌面] └─# which echo echo: shell built-in command ┌──(roottoor)-[~/桌面] └─# which whoami /usr/bin/whoami ┌──(roottoor)-[~/桌面] └─# ┌──(roottoor)-[~/桌面] └─# where echo echo: shell built-in command echo /bin/echo ┌──(roottoor)-[~/桌面] └─# where whoami /usr/bin/whoami /bin/whoami ┌──(roottoor)-[~/桌面] └─#
注意:在Linux平台,有些命令虽然为内建命令,但是系统关键目录也存在其可执行文件。
这里也可以使用enable或help指令,查看终端内建的所有指令
第一种:enable指令
┌──(roottoor)-[~/桌面] └─# enable - . : [ alias autoload bg bindkey break builtin bye cd chdir command compadd comparguments compcall compctl compdescribe compfiles compgroups compquote compset comptags comptry compvalues continue declare dirs disable disown echo echotc echoti emulate enable eval exec exit export false fc fg float functions getln getopts hash history integer jobs kill let limit local log logout noglob popd print printf private pushd pushln pwd r read readonly rehash return sched set setopt shift source suspend test times trap true ttyctl type typeset ulimit umask unalias unfunction unhash unlimit unset unsetopt vared wait whence where which zcompile zformat zle zmodload zparseopts zregexparse zstat zstyle ┌──(roottoor)-[~/桌面] └─#
第二种:help指令
[attach]56094[/attach]
接着对终端(内置|外置)命令进行测试,测试终端/bin/zsh:
测试:whoami指令
先对whoami指令进行类型探测与指令定位查询
# 指令探测:非内置指令 ┌──(roottoor)-[~/桌面] └─# type whoami whoami is /usr/bin/whoami ┌──(roottoor)-[~/桌面] └─# # 指令定位查询:搜索发现系统特殊目录存在`whoami`可执行程序 ┌──(roottoor)-[~/桌面] └─# where whoami /usr/bin/whoami /bin/whoami ┌──(roottoor)-[~/桌面] └─#
注意:/bin目录为/usr/bin目录的链接
然后,在zsh终端写入For循环执行whoami指令查看是否为内部执行或外部调用
for n in {0..10000000}; do whoami ; done
另一侧,使用htop动态进程监控程序对该终端进行监控,可发现whoami指令并非zsh终端内置封装的指令
[attach]56095[/attach]
测试:echo指令
同样,对echo指令进行类型探测与指令定位查询
# 指令探测:内置指令 ┌──(roottoor)-[~/桌面] └─# type echo echo is a shell builtin ┌──(roottoor)-[~/桌面] └─# # 指令定位查询:搜索发现系统特殊目录存在`echo`可执行程序,同时还发现存在`echo: shell built-in command`【终端内置指令】 ┌──(roottoor)-[~/桌面] └─# where echo echo: shell built-in command /usr/bin/echo /bin/echo ┌──(roottoor)-[~/桌面] └─#
然后,在zsh终端写入For循环执行echo指令查看是否为内部执行或外部调用
for n in {0..10000000}; do echo 1 ; done
另一侧,使用htop动态进程监控程序对该终端进行监控,可以发现echo指令为zsh终端内置封装的指令,并未出现外部调用
[attach]56096[/attach]
从系统环境变量PATH里面定位查询(注意人为增添的环境变量的影响),如果能查到一般来说可以判定为外部调用指令(排除非系统特殊目录),否则为内部调用指令(排除不存在指令)
# 外部调用指令 C:\Users\Qftm>where whoami C:\Windows\System32\whoami.exe C:\Users\Qftm> # 内部调用指令 C:\Users\Qftm>where cd INFO: Could not find files for the given pattern(s). C:\Users\Qftm> # 不存在指令 C:\Users\Qftm>where qftm INFO: Could not find files for the given pattern(s). C:\Users\Qftm> # 内部调用指令(排除人为增添的环境变量的影响)(排除非系统特殊目录) C:\Users\Qftm>where echo D:\QSoftware\W3Server\phpstudy2019\Extensions\MySQL5.7.26\bin\echo.exe C:\Users\Qftm>
第二种:set path指令【友好】
将系统环境变量临时设置为null,然后对指令进行帮助查询,如果能查到则判定为内置指令,否则为外部调用。
# path置空 C:\Users\Qftm>set path= C:\Users\Qftm>path PATH=(null) C:\Users\Qftm> # 内部调用指令 C:\Users\Qftm>cd /? Displays the name of or changes the current directory. CHDIR [/D] [drive:][path] CHDIR [..] CD [/D] [drive:][path] CD [..] .. Specifies that you want to change to the parent directory. Type CD drive: to display the current directory in the specified drive. Type CD without parameters to display the current drive and directory. Use the /D switch to change current drive in addition to changing current directory for a drive. If Command Extensions are enabled CHDIR changes as follows: The current directory string is converted to use the same case as the on disk names. So CD C:\TEMP would actually set the current directory to C:\Temp if that is the case on disk. CHDIR command does not treat spaces as delimiters, so it is possible to CD into a subdirectory name that contains a space without surrounding the name with quotes. For example: cd \winnt\profiles\username\programs\start menu is the same as: cd "\winnt\profiles\username\programs\start menu" which is what you would have to type if extensions were disabled. C:\Users\Qftm> # 外部调用指令 C:\Users\Qftm>whoami /? 'whoami' is not recognized as an internal or external command, operable program or batch file. C:\Users\Qftm> # 不存在指令 C:\Users\Qftm>qftm /? 'qftm' is not recognized as an internal or external command, operable program or batch file. C:\Users\Qftm>
注意:Windows下终端help指令并不能够查询终端内建指令:首先help指令为外部调用指令,然后help指令查询出的所有指令=(内建指令+外部指令)
# help:属于外部指令 C:\Users\Qftm>where help C:\Windows\System32\help.exe C:\Users\Qftm> # help:内建指令+外部指令(不同于Linux下bash等终端) C:\Users\Qftm>help For more information on a specific command, type HELP command-name ASSOC Displays or modifies file extension associations. ATTRIB Displays or changes file attributes. BREAK Sets or clears extended CTRL+C checking. BCDEDIT Sets properties in boot database to control boot loading. CACLS Displays or modifies access control lists (ACLs) of files. CALL Calls one batch program from another. CD Displays the name of or changes the current directory. CHCP Displays or sets the active code page number. CHDIR Displays the name of or changes the current directory. CHKDSK Checks a disk and displays a status report. CHKNTFS Displays or modifies the checking of disk at boot time. CLS Clears the screen. CMD Starts a new instance of the Windows command interpreter. COLOR Sets the default console foreground and background colors. COMP Compares the contents of two files or sets of files. COMPACT Displays or alters the compression of files on NTFS partitions. CONVERT Converts FAT volumes to NTFS. You cannot convert the current drive. COPY Copies one or more files to another location. DATE Displays or sets the date. DEL Deletes one or more files. DIR Displays a list of files and subdirectories in a directory. DISKPART Displays or configures Disk Partition properties. DOSKEY Edits command lines, recalls Windows commands, and creates macros. DRIVERQUERY Displays current device driver status and properties. ECHO Displays messages, or turns command echoing on or off. ENDLOCAL Ends localization of environment changes in a batch file. ERASE Deletes one or more files. EXIT Quits the CMD.EXE program (command interpreter). FC Compares two files or sets of files, and displays the differences between them. FIND Searches for a text string in a file or files. FINDSTR Searches for strings in files. FOR Runs a specified command for each file in a set of files. FORMAT Formats a disk for use with Windows. FSUTIL Displays or configures the file system properties. FTYPE Displays or modifies file types used in file extension associations. GOTO Directs the Windows command interpreter to a labeled line in a batch program. GPRESULT Displays Group Policy information for machine or user. GRAFTABL Enables Windows to display an extended character set in graphics mode. HELP Provides Help information for Windows commands. ICACLS Display, modify, backup, or restore ACLs for files and directories. IF Performs conditional processing in batch programs. LABEL Creates, changes, or deletes the volume label of a disk. MD Creates a directory. MKDIR Creates a directory. MKLINK Creates Symbolic Links and Hard Links MODE Configures a system device. MORE Displays output one screen at a time. MOVE Moves one or more files from one directory to another directory. OPENFILES Displays files opened by remote users for a file share. PATH Displays or sets a search path for executable files. PAUSE Suspends processing of a batch file and displays a message. POPD Restores the previous value of the current directory saved by PUSHD. PRINT Prints a text file. PROMPT Changes the Windows command prompt. PUSHD Saves the current directory then changes it. RD Removes a directory. RECOVER Recovers readable information from a bad or defective disk. REM Records comments (remarks) in batch files or CONFIG.SYS. REN Renames a file or files. RENAME Renames a file or files. REPLACE Replaces files. RMDIR Removes a directory. ROBOCOPY Advanced utility to copy files and directory trees SET Displays, sets, or removes Windows environment variables. SETLOCAL Begins localization of environment changes in a batch file. SC Displays or configures services (background processes). SCHTASKS Schedules commands and programs to run on a computer. SHIFT Shifts the position of replaceable parameters in batch files. SHUTDOWN Allows proper local or remote shutdown of machine. SORT Sorts input. START Starts a separate window to run a specified program or command. SUBST Associates a path with a drive letter. SYSTEMINFO Displays machine specific properties and configuration. TASKLIST Displays all currently running tasks including services. TASKKILL Kill or stop a running process or application. TIME Displays or sets the system time. TITLE Sets the window title for a CMD.EXE session. TREE Graphically displays the directory structure of a drive or path. TYPE Displays the contents of a text file. VER Displays the Windows version. VERIFY Tells Windows whether to verify that your files are written correctly to a disk. VOL Displays a disk volume label and serial number. XCOPY Copies files and directory trees. WMIC Displays WMI information inside interactive command shell. For more information on tools see the command-line reference in the online help. C:\Users\Qftm>
接着对终端(内置|外置)命令进行测试,测试终端cmd.exe:
测试:whoami指令
先对whoami指令进行类型探测与指令定位查询
# 类型探测:外部调用指令 # 定位查询:系统可执行程序 C:\Users\Qftm>where whoami C:\Windows\System32\whoami.exe C:\Users\Qftm>
然后,在cmd终端写入For循环执行whoami指令查看是否为内部执行或外部调用
C:\Users\Qftm>for /l %i in (1,1,1000000) do whoami
另一侧,打开任务管理进行cmd终端的监控,可发现whoami指令并非cmd.exe终端内置封装的指令
[attach]56097[/attach]
测试:echo指令
同样,对echo指令进行类型探测与指令定位查询
# 类型探测:内部调用指令 # 定位查询:非系统可执行程序 C:\Users\Qftm>where echo D:\QSoftware\W3Server\phpstudy2019\Extensions\MySQL5.7.26\bin\echo.exe C:\Users\Qftm>
然后,在cmd终端写入For循环执行echo指令查看是否为内部执行或外部调用
for /l %i in (1,1,1000000) do echo 1
另一侧,打开任务管理进行cmd终端的监控,可以发现echo指令为终端内置封装的指令,并未出现外部调用
[attach]56098[/attach]