关于shell:命令行及调用命令的语法格式

6次阅读

共计 3420 个字符,预计需要花费 9 分钟才能阅读完成。

概念

shell1

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

在大多数 Linux 零碎中,Bash 是默认的 shell 程序。

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

终端模拟器

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

命令的根本语法

以 PowerShell 为例,探讨调用 shell 命令的根本语法格局。
在 Powershell 中,在命令前面输出 -? 参数并回车,会失去这个命令的根本用法。比方,咱们输出 copy -? 时,失去如下的提醒:

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,咱们用了其别名 copy

诸如 [-xxx]<xxx[]> 之类的语法,其含意如下:

  • [-xxx]

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

    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… ↩
正文完
 0