你應(yīng)該選擇 Powershell 的10個理由(拋棄 cmd)
Powershell 從 Windows7 時代開始內(nèi)置于 Windows 系統(tǒng)當(dāng)中,可以看作是微軟對 cmd 的大升級,目前兩者并存于 Windows 系統(tǒng)中。
首先強(qiáng)烈推薦一個 powershell 學(xué)習(xí)網(wǎng)站:
powershell在線教程
1. powershell 更加 “powerful”
微軟起“powershell”這個名字雖然有吹牛逼的嫌疑(我以前也這樣想),但從事實來看并非如此。powershell 的強(qiáng)大程度分分鐘秒殺 Linux 上的 bash,更不要說飽受詬病的 cmd 了。(無意打廣告)
2. powershell 全面支持面向?qū)ο?/strong>
powershell 背后依靠的是一套完整的 .NET 編程體系,其腳本更容易編寫且穩(wěn)健性大大提升。反過來看 cmd,那些完全由各種命令堆砌而成的一條條指令簡直就是“小打小鬧”。
3. 從 cmd 遷移到 powershell 成本幾乎為零
如果你對老朋友 cmd 充滿情懷,無法舍棄,完全無妨!因為 powershell 可以看作 cmd 的超集,所有的常用命令諸如dir, cd, ipconfig等在 powershell 中都能直接使用。但背后的實現(xiàn)方式是完全不同的,powershell 基于完全的面向?qū)ο?,它通過給函數(shù)和對象“起別名”的方式來支持這些舊的命令。
4. 誘人的管道操作
管道操作的靈感來自 Linux 的 shell,但由于 powershell 將一切都包裝成為對象,而不是直接處理字符串,因此其管道操作的靈活程度遠(yuǎn)在 Linux 的 shell 之上。
例如:
PS C:\Users\Haley> ls | sort -Descending Name | Format-Table Name,Mode Name Mode ---- ---- VirtualBox VMsd----- Videos d-r--- Searchesd-r--- Saved Games d-r--- Picturesd-r--- OneDrived-r--- Music d-r--- Links d-r---
這條命令列出當(dāng)前路徑下的所有文件,按照名稱降序排序,并以表格的形式輸出,且只顯示Name和Mode兩個字段。
5. 絕對完備的幫助文檔
任何函數(shù)與對象都能夠通過help *命令來查看其幫助文檔(準(zhǔn)確來說應(yīng)該是Get-Help函數(shù),這是更加“面向?qū)ο蟆被拿绞剑鴋elp是它的別名),如果看不明白,加上-examples參數(shù)會有應(yīng)用實例,如果仍看不明白,加上-online參數(shù)會打開完整的在線幫助文檔,不得不佩服,微軟的一條龍服務(wù)做的很到位。
例如,關(guān)于ls的幫助文檔如下:
PS C:\Users\Haley> help ls 名稱 Get-ChildItem 摘要 Gets the items and child items in one or more specified locations. 語法 Get-ChildItem [[-Filter] <String>] [-Attributes {ReadOnly | Hidden | System | Directory | Archive | Device | Normal | Temporary | SparseFile | ReparsePoint | Compressed | Offline | NotContentIndexed | Encrypted | IntegrityStream | NoScrubData}] [-Depth <UInt32>] [-Directory] [-Exclude <String[]>] [-File] [-Force] [-Hidden] [-Include <String[]> ] -LiteralPath <String[]> [-Name] [-ReadOnly] [-Recurse] [-System] [-UseTransaction] [<CommonParameters>] Get-ChildItem [[-Path] <String[]>] [[-Filter] <String>] [-Attributes {ReadOnly | Hidden | System | Directory | Arch ive | Device | Normal | Temporary | SparseFile | ReparsePoint | Compressed | Offline | NotContentIndexed | Encrypte d | IntegrityStream | NoScrubData}] [-Depth <UInt32>] [-Directory] [-Exclude <String[]>] [-File] [-Force] [-Hidden] [-Include <String[]>] [-Name] [-ReadOnly] [-Recurse] [-System] [-UseTransaction] [<CommonParameters>] 說明 The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child cont ainers. A location can be a file system location, such as a directory, or a location exposed by a different Windows PowerSh ell provider, such as a registry hive or a certificate store. 相關(guān)鏈接 Online Version: http://go.microsoft.com/fwlink/?LinkId=821580 Get-Item Get-Location Get-Process Get-PSProvider 備注 若要查看示例,請鍵入: "get-help Get-ChildItem -examples". 有關(guān)詳細(xì)信息,請鍵入: "get-help Get-ChildItem -detailed". 若要獲取技術(shù)信息,請鍵入: "get-help Get-ChildItem -full". 有關(guān)在線幫助,請鍵入: "get-help Get-ChildItem -online"
6. 支持基本的數(shù)學(xué)運(yùn)算和數(shù)組操作(加減乘除模),打開就能當(dāng)計算器用
PS C:\Users\Haley> 1 + 1 2 PS C:\Users\Haley> 3 * 5 15 PS C:\Users\Haley> 0xab 171 PS C:\Users\Haley> 1kb 1024 PS C:\Users\Haley> 1mb / 1kb 1024
7. 良好的腳本編程體驗
powershell 腳本的語法與高級編程語言非常相近,例如其分支語句if(...){} else{}、循環(huán)語句for(...){}與 C 語言別無二致,因而大大提高了編程體驗。
8. 擁有豐富的字符串操作對象和函數(shù),全面支持正則表達(dá)式
字符串處理是 shell 腳本的主要任務(wù),正則表達(dá)式的重要性也無需多說。
9. 支持重定向,讀寫文件易如反掌
powershell 原生支持將結(jié)果導(dǎo)出到html, csv, xml等文件,也可以通過重定向從文件中讀取內(nèi)容。
例如:
PS C:\Users\Haley> ls | Select-Object Mode,Name | Export-Csv ~/desktop/test.csv
這樣就把當(dāng)前目錄下的所有 文件名-讀寫權(quán)限 保存到了一個 csv 文件中。
10. 支持 Debug
雖然這個功能很少用到,但支持 debug 是走向完備編程語言不可或缺的一項功能。
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。