命令行及调用命令的语法格式

概念

shell1

shell(壳)是一种能够接收键盘输入的命令并将它们传递给操做系统执行的命令行界面(command line interface,CLI)程序。现在,除了命令行界面(例如 shell)以外,咱们还有图形用户界面(graphical user interface,GUI)。php

在大多数 Linux 系统中,Bash 是默认的 shell 程序。linux

在旧版 Windows 系统中,CMD.EXE 是默认的 shell 程序,在最新版的 Windows 10 系统中,PowerShell 成为了其默认的 shell 程序,但 CMD.EXE 也保留在其中。shell

终端模拟器

终端模拟器(terminal emulator)在大部分场合下能够简称为终端,这是一种可让你与 shell 进行交互的窗口程序。微软开发并开源的 Windows Terminal 即是其中的一款。windows

Windows Terminal

命令的基本语法

以 PowerShell 为例,讨论调用 shell 命令的基本语法格式。
在 Powershell 中,在命令后面输入 -? 参数并回车,会获得这个命令的基本用法。好比,咱们输入 copy -? 时,获得以下的提示:api

PS C:\Users\hookin> copy -?

名称
    Copy-Item

语法
    Copy-Item [-Path] <string[]> [[-Destination] <string>]  [<Com
    monParameters>]

    Copy-Item [[-Destination] <string>]  [<CommonParameters>]


别名
    cpi
    cp
    copy


备注
    Get-Help 在此计算机上找不到该 cmdlet 的帮助文件。它仅显示部分
    帮助。
        -- 若要下载并安装包含此 cmdlet 的模块的帮助文件,请使用 U
    pdate-Help。
        -- 若要联机查看此 cmdlet 的帮助主题,请键入: "Get-Help Co
    py-Item -Online" 或
           转到 https://go.microsoft.com/fwlink/?LinkID=113292。

转到 https://go.microsoft.com/fwli... 查看联机帮助。数组

能够看到这个命令全名叫 Copy-Item,咱们用了其别名 copyspa

诸如 [-xxx]<xxx[]> 之类的语法,其含义以下:操作系统

  • [-xxx]命令行

    可选参数,能够省略不写,-xxx 为参数,用左右方括号 [] 包裹表示可选,好比:code

    copy *.txt child
    copy -Path *.txt -Destination child

执行这两条命令的效果是同样的。

若是显式声明了-Path 或/和 -Destination 参数,则显式声明的参数其位置能够改变:

copy -Destination child -Path *.txt
copy -Destination child *.txt
copy child -Path *.txt

PowerShell 会识别值的位置并判断其对应的是哪一个参数(下文会讲是怎么判断的)。

  • <xxx>

    必选值,如 [-Path] <string[]> 表示 -Path 参数是可写可不写的,可是其后面的值 string[] (表示字符串型数组,下文会讲)是必需要写的。

    再如 [[-Destination] <string>] 表示 -Destination 及其值 string 组成的总体是可选的,可是若是你想使用这个参数,则能够直接写 string 字符串。

数据类型

PowerShell 支持众多的数据类型,经常使用的有 string int64 SwitchParameter 等,你能够在遇到时阅读联机帮助。

  • string

    字符串类型,双引号能够省略不写。

    copy -Path *.txt -Destination child
  • string[]

    字符串数组,以上的代码能够这样写:

    copy -Path 1.txt,2.txt -Destination child

也就是说,数组中的元素用逗号 , 分隔。

  • SwitchParameter

    开关参数,至关于 Boolean 布尔型,键入时为 true,不键入时为默认值。

    PS E:\Temp\dosTest> copy -Path 1.txt,2.txt -Destination child -Confirm
    
    确认
    是否确实要执行此操做?
    正在目标“项: E:\Temp\dosTest\1.txt 目标: E:\Temp\dosTest\child\1.txt”上执行操做“复制文件”。
    [Y] 是(Y)  [A] 全是(A)  [N] 否(N)  [L] 全否(L)  [S] 暂停(S)  [?] 帮助 (默认值为“Y”): y
    
    确认
    是否确实要执行此操做?
    正在目标“项: E:\Temp\dosTest\2.txt 目标: E:\Temp\dosTest\child\2.txt”上执行操做“复制文件”。
    [Y] 是(Y)  [A] 全是(A)  [N] 否(N)  [L] 全否(L)  [S] 暂停(S)  [?] 帮助 (默认值为“Y”): y

以上命令中键入了一个在本地帮助中没有给出的 -Confirm 参数,按照 Copy-Item 联机帮助 的说明:

-Confirm

Prompts you for confirmation before running the cmdlet.

在运行 cmdlet 以前提示您进行确认。

属性
Type (类型): SwitchParameter 切换参数
Aliases (别名): cf,即 -Confirm 等同于 -cf
Position (位置): Named 须要主动声明
Default value (默认值): False 错
Accept pipeline input (接受管道输入): False 错

| Accept wildcard characters (接受通配符): | False 错 |

能够看到其默认值为 false,键入后为 true,但也能够主动声明为 falsetrue

copy -Path 1.txt,2.txt -Destination child -Confirm:$false
copy -Path 1.txt,2.txt -Destination child -Confirm:$true

Position (位置)

咱们看一下 copy 的几个参数介绍:

-Path

Specifies, as a string array, the path to the items to copy. Wildcard characters are permitted.

指定要复制的项的路径,做为字符串数组。容许使用通配符。

属性
Type: String[]
Position: 0
Default value: None
Accept pipeline input: True
Accept wildcard characters: True

-Destination

Specifies the path to the new location. The default is the current directory.

指定新位置的路径,默认为工做目录。

To rename the item being copied, specify a new name in the value of the Destination parameter.

若要重命名正在复制的项,请在 Destination 参数的值中指定一个新名称。

属性
Type: String
Position: 1
Default value: Current directory
Accept pipeline input: True
Accept wildcard characters: False

对这几个参数作对比:

参数 Position 值
-Confirm Named
-Path 0
-Destination 1

-Confirm 的 Position 为 Named,表示要声明才能修改其值;而其余的参数的 Position 值则为 01,表示读取的前后顺序。

PowerShell 就是经过预配置的 Position 来判断参数值对应的是哪一个可选参数的。

copy -Destination child *.txt
# *.txt 是 -Path 的值

copy child -Path *.txt
# child 是 -Destination 的值

copy *.txt child
# *.txt 是 -Path 的值,child 是 -Destination 的值

copy -Path 1.txt,2.txt -Destination child
# -Confirm 为默认值 false

copy -Path 1.txt,2.txt -Destination child -Confirm
# -Confirm 为 true

  1. What is "the Shell"?(http://linuxcommand.org/lc3_l...
相关文章
相关标签/搜索