py很強大,我承認。但在運維方面,py不但不強大,還有硬傷。正因為有下述硬傷,所以我們運維,還是用shell多,用py極少。
我看到用shell的人很多,你建議人用python,人說py是很好,但下一秒都跑光了。
我看到用shell的人很多,你建議人用python,人說py是很好,但下一秒都跑光了。
------只從shell角度,詳細對比powershell和python
------【1在linux安裝powershell,並不是難題】------
無需編譯,一鍵安裝powershell:
centos7及以上,安裝powershell:
curl -o /etc/yum.repos.d/microsoft.repo https://packages.microsoft.com/config/rhel/7/prod.repo
sudo yum remove -y powershell #刪除舊版
sudo yum install -y powershell
pwsh -c 'mkdir -p "$env:HOME/.config/powershell" '
pwsh -c 'Add-Content -Value "Set-PSReadlineOption -EditMode Windows" -LiteralPath $profile '
pwsh -c 'Add-Content -Value "`nSubsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile" -LiteralPath /etc/ssh/sshd_config '
ubuntu1604:
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
sudo curl -o /etc/apt/sources.list.d/microsoft.list https://packages.microsoft.com/config/ubuntu/16.04/prod.list
sudo apt-get update
sudo apt-get remove -y powershell #刪除舊版
sudo apt-get install -y powershell
pwsh -c 'mkdir -p "$env:HOME/.config/powershell" '
pwsh -c 'Add-Content -Value "Set-PSReadlineOption -EditMode Windows" -LiteralPath $profile '
pwsh -c 'Add-Content -Value "`nSubsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile" -LiteralPath /etc/ssh/sshd_config '
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
sudo curl -o /etc/apt/sources.list.d/microsoft.list https://packages.microsoft.com/config/ubuntu/16.04/prod.list
sudo apt-get update
sudo apt-get remove -y powershell #刪除舊版
sudo apt-get install -y powershell
pwsh -c 'mkdir -p "$env:HOME/.config/powershell" '
pwsh -c 'Add-Content -Value "Set-PSReadlineOption -EditMode Windows" -LiteralPath $profile '
pwsh -c 'Add-Content -Value "`nSubsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile" -LiteralPath /etc/ssh/sshd_config '
ubuntu1804:
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
sudo curl -o /etc/apt/sources.list.d/microsoft.list https://packages.microsoft.com/config/ubuntu/18.04/prod.list
sudo apt-get update
sudo apt-get remove -y powershell #刪除舊版
sudo apt-get install -y powershell
pwsh -c 'mkdir -p "$env:HOME/.config/powershell" '
pwsh -c 'Add-Content -Value "Set-PSReadlineOption -EditMode Windows" -LiteralPath $profile '
pwsh -c 'Add-Content -Value "`nSubsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile" -LiteralPath /etc/ssh/sshd_config '
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
sudo curl -o /etc/apt/sources.list.d/microsoft.list https://packages.microsoft.com/config/ubuntu/18.04/prod.list
sudo apt-get update
sudo apt-get remove -y powershell #刪除舊版
sudo apt-get install -y powershell
pwsh -c 'mkdir -p "$env:HOME/.config/powershell" '
pwsh -c 'Add-Content -Value "Set-PSReadlineOption -EditMode Windows" -LiteralPath $profile '
pwsh -c 'Add-Content -Value "`nSubsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile" -LiteralPath /etc/ssh/sshd_config '
debian9:
sudo apt-get update
sudo apt-get install curl gnupg apt-transport-https
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch main" > /etc/apt/sources.list.d/microsoft.list'
sudo apt-get update
sudo apt-get remove -y powershell #刪除舊版
sudo apt-get install -y powershell
pwsh -c 'mkdir -p "$env:HOME/.config/powershell" '
pwsh -c 'Add-Content -Value "Set-PSReadlineOption -EditMode Windows" -LiteralPath $profile '
pwsh -c 'Add-Content -Value "`nSubsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile" -LiteralPath /etc/ssh/sshd_config '
sudo apt-get update
sudo apt-get install curl gnupg apt-transport-https
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch main" > /etc/apt/sources.list.d/microsoft.list'
sudo apt-get update
sudo apt-get remove -y powershell #刪除舊版
sudo apt-get install -y powershell
pwsh -c 'mkdir -p "$env:HOME/.config/powershell" '
pwsh -c 'Add-Content -Value "Set-PSReadlineOption -EditMode Windows" -LiteralPath $profile '
pwsh -c 'Add-Content -Value "`nSubsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile" -LiteralPath /etc/ssh/sshd_config '
debian10:
sudo apt-get update
sudo apt-get install curl gnupg apt-transport-https
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-buster-prod buster main" > /etc/apt/sources.list.d/microsoft.list
sudo apt-get update
sudo apt-get remove -y powershell #刪除舊版
sudo apt-get install -y powershell
pwsh -c 'mkdir -p "$env:HOME/.config/powershell" '
pwsh -c 'Add-Content -Value "Set-PSReadlineOption -EditMode Windows" -LiteralPath $profile '
pwsh -c 'Add-Content -Value "`nSubsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile" -LiteralPath /etc/ssh/sshd_config '
sudo apt-get update
sudo apt-get install curl gnupg apt-transport-https
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-buster-prod buster main" > /etc/apt/sources.list.d/microsoft.list
sudo apt-get update
sudo apt-get remove -y powershell #刪除舊版
sudo apt-get install -y powershell
pwsh -c 'mkdir -p "$env:HOME/.config/powershell" '
pwsh -c 'Add-Content -Value "Set-PSReadlineOption -EditMode Windows" -LiteralPath $profile '
pwsh -c 'Add-Content -Value "`nSubsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile" -LiteralPath /etc/ssh/sshd_config '
安裝方法:
https://docs.microsoft.com/zh-cn/powershell/scripting/setup/installing-powershell-core-on-linux?view=powershell-6
------【2python定義變量,氣死劉玄德】------
話說我在認識powershell之前,就想學習一門簡單強大的腳本。我找到python開始學練起來。
有一天,我想定義個變量叫做【3顧茅廬】但不行,研究了下發現不支持數字打頭。
改了下定義成了【a3顧茅廬】,感覺不雅,一怒之下改成了【b3顧茅廬】。。。
阿三顧茅廬,癟三顧茅廬。。。姓劉的學個python真是倒霉啊, ;-(
第一步變量名都定義不了,還能平天下么?
有一天,我想定義個變量叫做【3顧茅廬】但不行,研究了下發現不支持數字打頭。
改了下定義成了【a3顧茅廬】,感覺不雅,一怒之下改成了【b3顧茅廬】。。。
阿三顧茅廬,癟三顧茅廬。。。姓劉的學個python真是倒霉啊, ;-(
第一步變量名都定義不了,還能平天下么?
同理,我想建立個變量叫做“52張撲克牌”python就不行。
$52張撲克牌 = 'J','Q','K' #在powershell中合法
shell人喜歡用$1,$2,作為變量名,在py中也不行。ps無這些問題。ps函數名也支持數字打頭,如1a。
$1 = 'xxx'
$2 = 'yyy'
$52張撲克牌 = 'J','Q','K' #在powershell中合法
shell人喜歡用$1,$2,作為變量名,在py中也不行。ps無這些問題。ps函數名也支持數字打頭,如1a。
$1 = 'xxx'
$2 = 'yyy'
在ps里,建議定義變量,引用變量,建議用大花括號。這一點和shell相同。
${1} = 'xxx' #定義
${1} #使用。
${1} = 'xxx' #定義
${1} #使用。
你在python中輸入,if=1,會彈出,“duang,這是非法姨夫,姨夫是內置關鍵字,被python內部給霸占了”
你在powershell中,$if=1,完全正常。if是姨夫,$if是“四姨夫”,絕對不會搞錯^_^
你在powershell中,$if=1,完全正常。if是姨夫,$if是“四姨夫”,絕對不會搞錯^_^
下列為合法ps變量名的定義:
${1+2} = 'a' #不加大花括號不行
${a.b} = 'a'
${a:b} = 'a'
${a'b} = 'a'
${1+2} = 'a' #不加大花括號不行
${a.b} = 'a'
${a:b} = 'a'
${a'b} = 'a'
我說py中,本質上,沒有布爾型變量。ps中的布爾型變量強點有限。
那么什么叫做有布爾型變量呢?有且只有:1,0,"true","false",$true,$flase, 或許還要算上db的true,false。很顯然py沒做到,ps也沒做到。
ps命令行,不光可以運行ps命令,還能執行shell外部命令。如find,grep等。
powershell有shell命令行,有參數,有tab補全,這是ps的一個大優勢,py的一個大弱項。
或者說從.py中運行shell命令,接收返回值麻煩。
ps有命令行顏色,命令行表格&對齊(format-table),命令行百分比進度條(Write-Progress)等,這些命令行相關的基本功能,py都沒有吧?
------【3python沒有shell命令行】------
ps命令行,不光可以運行ps命令,還能執行shell外部命令。如find,grep等。
powershell有shell命令行,有參數,有tab補全,這是ps的一個大優勢,py的一個大弱項。
或者說從.py中運行shell命令,接收返回值麻煩。
ps有命令行顏色,命令行表格&對齊(format-table),命令行百分比進度條(Write-Progress)等,這些命令行相關的基本功能,py都沒有吧?
------【4python沒有管道】------
powershell有管道,管道兩端可以傳強類型對象,和shell用法相同。這是ps的一個大優勢,py的一個大弱項。
比如寫py管道腳本,需要import,open,read,close。
py2有4個popen,
py3管道subprocess.Popen有各種坑,py為了解決這些坑,py有參數10多個,每個參數n個選項。導致在py中使用管道,變得很復雜。用起來很麻煩。
啥時候用true,啥時候用false參數?你得duang,duang實驗。
這個麻煩直接把80%用戶嚇跑了。所以我“說成”沒有管道。
而powershell,bash,使用管道數據,不需要這些步驟。
------【5shell命令,bash嵌入powershell容易,嵌入python難】------
基於上述3,4。shell,bash嵌入powershell容易,嵌入python難。
shell人學powershell難度平滑,shell人學python難度陡峭。
引號的絕對引用,powershell用【@''@】,【@""@】。python用3個單引號。哪個更容易嵌入shell呢?
------【6py語法vs ps語法】------
perl道:“來我告訴你,茴香豆的茴字,的4萬種寫法”
python道:“來我告訴你,茴香豆的茴字,的40種寫法”
powershell道:“來我告訴你,茴香豆的茴字,的2種寫法”
powershell語法簡單,沒有三目,沒有裝飾器,
比如$aaa ,變量不管定義,還是引用,都要用$ ,比python更容易看懂。比python的【無變量打頭符號,定義變量】好。
powershell這種做法,更容易區分內置關鍵字,和變量!
powershell這種做法,更容易區分內置關鍵字,和變量!
python【變量名】支持大小寫computer=1但Computer=2,很容易搞亂,讓我用着不爽。
py的線程模型也不行。同1個python進程,能並發執行這2個py么?
bash=1進程。內含1個線程。此線程可以執行1個,單線程bash。
python=1進程。內含1個線程。此線程可以執行1個py。一個py內可以有多線程,【單py內的多線程代碼】加了復雜度,卻沒法用cpu多核。
powershell=1進程。內含n個線程。每線程可以執行1個ps1。每線程內的ps1,和單線程執行ps1,差別極小。還能用多核。
bash=1進程。內含1個線程。此線程可以執行1個,單線程bash。
python=1進程。內含1個線程。此線程可以執行1個py。一個py內可以有多線程,【單py內的多線程代碼】加了復雜度,卻沒法用cpu多核。
powershell=1進程。內含n個線程。每線程可以執行1個ps1。每線程內的ps1,和單線程執行ps1,差別極小。還能用多核。
由上述,py沒有【ps的全局作用域】。我趕腳ps的變量作用域,比python清晰,簡單。py混亂。
結論=論點:
python功能強大,但語法復雜,門檻高於ps。
powershell和shell語法類似,更適合腳本人員,運維人員,非專業程序員。
人生苦短,我用powershell
------【7py癌症1,中文&編碼問題多】------
py的中文問題雖然都是可以解決的。但實際場合中,總是有中文問題。
常見的,處理中文文件名問題。url中帶有中文問題。編碼問題。
前兩天有人給我個神奇軟件,能把照片變果體。
我一看軟件,用了python,gt5的殼子。
打開一個"美眉.jpg",軟件崩潰。改成英文名字,好了。
我一看軟件,用了python,gt5的殼子。
打開一個"美眉.jpg",軟件崩潰。改成英文名字,好了。
---a.py開始---
# 因為有本行中文注釋。python2.7或python3.7 都不靈。除非加上【coding:】來聲明編碼,用【錯誤的編碼來聲明】也行。
print "Hello World!"
---a.py結束---
# 因為有本行中文注釋。python2.7或python3.7 都不靈。除非加上【coding:】來聲明編碼,用【錯誤的編碼來聲明】也行。
print "Hello World!"
---a.py結束---
或
---b.py開始---
# 因為有本行中文注釋。python2.7或python3.7 都不靈。除非加上【coding:】來聲明編碼,用【錯誤的編碼來聲明】也行。
print ("Hello World!")
---b.py結束---
powershell處理,lnux中的中文目錄名,中文文件名,中文腳本名,中文變量名,中文函數名,中文注釋,容易。
關於亂碼:
亂碼這個病,治法有很多,py大夫的治療方案,我看不是最佳的。
微軟家發明了bom頭,powershell對bom頭100%支持,
ps1腳本源碼用bom頭,txt用bom頭。有頭文件無亂碼。
亂碼這個病,治法有很多,py大夫的治療方案,我看不是最佳的。
微軟家發明了bom頭,powershell對bom頭100%支持,
ps1腳本源碼用bom頭,txt用bom頭。有頭文件無亂碼。
------【8py癌症2,無法鎖死變量類型】------
強制動態類型的,py版胯骨軸子,太靈活了,太容易閃到腰。
py無法鎖死變量類型,成為靜態變量,真的很不爽。
這一點這很重要,甚至有人說py不適合大型項目。
鎖死變量類型
https://www.v2ex.com/t/474197
https://www.v2ex.com/t/474197
而ps就不同了。
可以動態: $a = 'xxx'
可以靜態數據類型: [string]$a = 'xxx'
可以動態: $a = 'xxx'
可以靜態數據類型: [string]$a = 'xxx'
------【9py癌症3,1.6億美金估值的公司被py這個特性毀了】------
def f(l=[]):
l.append(1)
print(l)
f()
f()
f()
python2.7,python3.6.8測試結果為:
[1]
[1, 1]
[1, 1, 1]
f()
f()
python2.7,python3.6.8測試結果為:
[1]
[1, 1]
[1, 1, 1]
圍觀:
https://www.v2ex.com/t/467817
https://www.v2ex.com/t/467817
powershell沒有這個問題:
function f([System.Collections.ArrayList]$l=@())
{
$null = $l.add(1)
$l
}
f
f
f
輸出:
1
1
1
function f([System.Collections.ArrayList]$l=@())
{
$null = $l.add(1)
$l
}
f
f
f
輸出:
1
1
1
------【10py癌症4,怎能少了你?python線程全局鎖】------
python中的多線程,利用不了多核?這樣豈不等於瞎子的眼睛。
搜 GIL threading Thread
powershell沒這問題。
搜 GIL threading Thread
powershell沒這問題。
------【11py癌症5,路徑依賴】------
你在當前目錄下,存放一個【string.py】,【string.pyc】就把python打懵了。開始蹦吧報錯。
同樣道理,py腳本名還不能起名為ssl,http,request等。
python安裝麻煩,升級麻煩。第三方庫、包對路徑有依賴,部署困難,移動困難,簡直是癌症。
而ps較好,一個【$env:PSModulePath】搞定。達到java的class-path水平。當然最好的是go的靜態連接。
------【12py癌症6,py2和py3的兼容性】------
語法和模塊差異較大,當然以下列py2老版本為主,新版本這個為題逐漸消解了。
centos6,centos7,centos8---centos8沒有默認py3
ubuntu1404,ubuntu1804---ubuntu1604默認py3
debian8,debian9
------【13py調試器】------
pycharm調試器需要破解,還是java開發的,又慢又吃內存。
Sublime Text 3主要問題:
1界面英文,漢化的不太敢用,怕后門。
2要破解。
3英文原版st3,打開帶有【中文變量名】,【中文注釋】的.py,.ps1會亂碼。
1界面英文,漢化的不太敢用,怕后門。
2要破解。
3英文原版st3,打開帶有【中文變量名】,【中文注釋】的.py,.ps1會亂碼。
py開發、調試器大都是英文的,來了vscode救贖。
------【14手冊】------
docs.microsoft.com中的。net手冊,是多語種的,簡單說就是中文的,比python的只有英文的手冊強一萬倍!!!
------【15其他:】------
shell假如hang住,會給py帶來麻煩。
python人太貴,運維的工資只能招到py低手。py高手有更掙錢的方向,【高富帥不願入窮坑】寫運維腳本。即便寫出來。也很繁瑣。又不好用。
py的遠程ssh遠程一堆坑 : https://zhangge.net/5122.html
py的sftp一堆坑,不如ps+winscp模塊和命令 : https://zhangge.net/5121.html
目的,生成個文件a,並寫入內容b。遇到temp147的目錄癌症了。不是這樣的目錄,就完全沒問題。ps沒這問題。shell也沒這問題。
------【16powershell的優勢】------
ps中有,管道。管道兩端可以傳對象。
ps中有,基於sshd的,遠程命令行。支持客戶端,服務器之間,直接傳輸對象。py不行。或者說ps遠程的【序列化/反序列化】是透明的。
ps的遠程命令,可以自動上傳腳本,到遠程。
invoke-command -session $a -filepath /tmp/客戶機本地腳本1.ps1
ps有遠程變量作用域,$using:a = 1,本地引用遠程變量,遠程引用本地變量,容易。
ps用大花括號包圍腳本代碼,而不用單雙引號。經堡壘機,多層服務器,發送命令,更簡單,有優勢。
用vscode,加remote-ssh插件,可以遠程打開保存在linux中的ps1文件。
可以單步,斷點,鼠標選中n行,可以在win中遠程調試linux中的ps1腳本。
linux中可以多線程,多進程並發運行ps1。多線程並發可以跑滿多核cpu。
powershellwin中無敵。
很多虛擬機廠家,雲廠家,官方發布powershell模塊:
aws,vmware,dell,聯想,citrix
雖然powershell庫沒有python那么多,但運維方面的基本不缺。
powershell對json,yaml,csv支持的也很好。
------【17python在腳本范疇的優勢在哪?】------
有啥python行,而powershell不行的,你倒是說出來呀?歡迎來diss powershell。
注意,基本應該圍繞,腳本,運維,shell這一塊。