声明:本文由焖哥(1328486072)投稿,感谢分享!
掩面痛哭,奔走相告!
powershell .ps1脚本 run in linux啦!
可以在linux的shell中调用.net类和方法,属性啦。
(mono+pash)er要开饭了!
powershell linux ps1 powershell 传教士 script
概述
- 简单来说linux,unix是非常依赖脚本的,而win不是。win中有很多图形程序+c库,效率不比脚本差。点几下鼠标照样能完成需求。当
然,图形和字符是两码事,不能完全替代。 - win中的bat和linux中的bash都是老脚本语言,纵横沙场几十年,功勋卓著。但由于太老,有很多限制和缺陷。用起来比python和powershell差很多,麻烦很多。一般来讲玩python和powershell的不乐意用。总之新的大有替代老的之势。
- 拿python和powershell比较。pash就是bash+powershell。python一个很大的缺点是【没有shell命令行,管道】。为了弥补这个缺点,又有人创造出了ipython。ipython有人用么?用着爽么?那么pash是类似的东西。
pash目前的命令
Add-Member Add-PSSnapin Add-Type Clear-Variable Convert-Path ConvertTo-Csv ConvertTo-Html Copy-Item Export-Csv ForEach-Object Format-Default Format-List Format-Table Get-ChildItem Get-Command Get-Date Get-Help Get-History Get-Host Get-Location Get-Member Get-Process Get-PSDrive Get-PSProvider Get-PSSnapin Get-Random Get-Service Get-Variable Import-Csv Join-Path Move-Item New-Alias New-Item New-Object New-Variable Out-Default Out-File Out-Null Remove-Item Remove-PSSnapin Remove-Variable Rename-Item Restart-Service Resume-Service Select-Object Set-Alias Set-Location Set-Variable Sort-Object Split-Path Start-Service Start-Sleep Stop-Process Stop-Service Suspend-Service Tee-Object Write-Debug Write-Error Write-Host Write-Output Write-Progress Write-Verbose Write-Warning
powershell的特色命令:
Select-Object,Where-Object,Group-Object中,只实现了第一个。
没有任何invoke-*命令。如:invoke-command。
有Get-Variable命令,但后面是否跟变量名,都不灵。
Import-Module这个重量级的命令还不支持。
测试脚本内容
略
pash试用结论
只测试过utf8编码保存文件。
好处
- 回车设置为cr,lf,crlf都能正确运行。
- 空格设置为空格,tab键,全角空格,都能正确运行。—-bash无法识别中文空格,即全角空格,但pash无此问题。
- 支持中文函数名,中文变量名。—–好
- 支持静态类,静态方法,和这种写法,如: [array]::Sort($mmm)
- 命令不区分大小写,get-process和Get-Process都好用。
- 中文支持比原生powershell好,参见麻将八万的测试代码。
- 支持变量作用域,并工作正常。
- 支持 -match,并结果正确。
- 支持 -f ,结果正确。
- 支持$a++ ,$a–等。
最新版已经修复下面这个问题。
不支持 未定义先引用变量,如:$temp2 = $temp2 + ‘aaa’
但支持这样:$temp2 = ‘aaa’ + $temp2
目前不支持
- 不支持do { } while ( ),但支持while () { }。—-这个问题不大。
- 不支持switch。
这个问题也不大,我更喜欢if嵌套,更直观。 - 数组:不支持包含参数,即不支持 -contains,和 $数组.contains(“a”) 。数组也不支持-join。
- 字符串:不支持 -replace 。
- 不支持@”@,@””@的引用。
前后我已经带了回车,并且在win中测试通过。 - 不支持变量前缀[string]$a ,[int32]$a。
这些个是什么?只是语法糖而已。
语法糖是什么?是给菜鸟程序员制造的,简单好用的功能。
没有这些个功能,我们照样可以用原生的。net语法代替。
如 string.replace:$aaaa = 'aaaa?cccc?ddd?kkkkk' $bbbb = $aaaa.replace('?','-') write-host $bbbb
bug
- else,elseif 不能放在行首。
- 函数输出,和变量输出,存在串行的bug。
- 这都是小问题,容易解决。
pash点评
get-content 不支持就真的不好了。但如此,我们就武功就全废了么?
还好创建alias的new-alias和$profile功能是好的,这样我们就可以用原生的linux命令建立别名了。
如:
还好创建alias的new-alias和$profile功能是好的,这样我们就可以用原生的linux命令建立别名了。
如:
echo "new-alias get-content-linux /usr/bin/cat" >> /root/.Pash_profile.ps1 #这里的root根据用户名改变。
这样我们就有了 get-content-linux 这个命令,我们的武功全都回来了。
pash脚本案例
#!/opt/mono/bin/mono /Pash/Source/PashConsole/bin/Debug/Pash.exe # test2.ps1 # 每行文字前插入 'aaa' $要处理的文件 = '/etc/sysconfig/network-scripts/ifcfg-lo' $每行文件 = get-content-linux $要处理的文件 foreach ($temp in $每行文件) { $temp2 = $temp2 + 'aaa' + $temp + "`r`n" } out-file $temp2 /root/ttt.txt -encoding utf8
pash安装
- 安装mono
- git 并编译pash
本文链接: https://www.pstips.net/use-mono-to-call-pash-on-linux.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!