新手PowerShell问题:
我想在PowerShell中创建一个完全等同于此Bash别名的别名:
alias django-admin-jy="jython /path/to/jython-dev/dist/bin/django-admin.py"
Run Code Online (Sandbox Code Playgroud)
到目前为止,我一直在修补它,我发现这非常困难.
-PowerShell别名仅适用于PowerShell命令+函数调用
- 无法在PowerShell函数调用上允许无限数量的参数
-PowerShell似乎阻止了stdout
值得注意的是,我已经尝试过这里提出的解决方案:http://huddledmasses.org/powershell-power-user-tips-bash-style-alias-command/
并且在加载PowerShell时遇到以下与语法相关的错误:
The term 'which' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spell
ing of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\Dan\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:9 char:27
+ $cmd = @(which <<<< $_.Content)[0]
+ CategoryInfo : ObjectNotFound: (which:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)
Kei*_*ill 17
PowerShell别名可用于任何"命令",包括EXE:
function django-admin-jy {
jython.exe /path/to/jython-dev/dist/bin/django-admin.py @args
}
Run Code Online (Sandbox Code Playgroud)
更新:如评论中所述,PowerShell别名不允许参数.它们是命令名称的简单"别名",仅此而已.为了得到你想要的东西(我想因为我不熟悉Bash别名)试试这个:
function New-BashStyleAlias([string]$name, [string]$command)
{
$sb = [scriptblock]::Create($command)
New-Item "Function:\global:$name" -Value $sb | Out-Null
}
New-BashStyleAlias django-admin-jy 'jython.exe /path/to/jython-dev/dist/bin/django-admin.py @args'
Run Code Online (Sandbox Code Playgroud)
它使用PowerShell 2.0的一个功能,称为参数splatting.您可以应用于@
引用数组或哈希表的变量名称.在这种情况下,我们将它应用于名为args
包含所有未命名参数的变量.
如果您想要一种真正通用的方法来创建带参数的别名函数,请尝试以下方法:
function django-admin-jy {
jython.exe /path/to/jython-dev/dist/bin/django-admin.py @args
}
Run Code Online (Sandbox Code Playgroud)
关于乔尔的方法的问题,我怀疑他有@
别名args
.尝试更换@
用args
.
归档时间: |
|
查看次数: |
1931 次 |
最近记录: |