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
以 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
,咱們用了其別名 copy
。spa
諸如 [-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
,但也能夠主動聲明爲 false
或 true
:
copy -Path 1.txt,2.txt -Destination child -Confirm:$false copy -Path 1.txt,2.txt -Destination child -Confirm:$true
咱們看一下 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 值則爲 0
、1
,表示讀取的前後順序。
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