相当于PowerShell中的Bash别名

Rig*_*der 9 django powershell

新手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.

  • 使用函数的一个好处是,你可以自动完成参数名称(在`-`之后按tab键),你没有获得使用通用`New-BashStyleAlias`设置的别名.定义函数有点冗长,但它不应该是配置文件脚本中的问题. (2认同)