相当于 PowerShell 中的 Bash 别名 [英] Equivalent to Bash alias in PowerShell

查看:31
本文介绍了相当于 PowerShell 中的 Bash 别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个 PowerShell 新手问题:

A newbie PowerShell question:

我想在 PowerShell 中创建一个与这个 Bash 别名完全相同的别名:

I'd like to make an alias in PowerShell exactly equivalent to this Bash alias:

alias django-admin-jy="jython /path/to/jython-dev/dist/bin/django-admin.py"

到目前为止,我发现这非常困难.

In tinkering with it so far, I've found this to be very difficult.

-PowerShell 别名仅适用于 PowerShell 命令 + 函数调用

-PowerShell aliases only work with PowerShell commands + function calls

-没有明确的方法允许在 PowerShell 函数调用中使用无限数量的参数

-No clear way to allow for an unlimited number of arguments on a PowerShell function call

-PowerShell 似乎阻止了标准输出

-PowerShell seems to block stdout

值得注意的是,我已经尝试过这里提出的解决方案:http://huddledmasses.org/powershell-power-user-tips-bash-style-alias-command/

It's worth noting that I've tried the solution put forth here: http://huddledmasses.org/powershell-power-user-tips-bash-style-alias-command/

并且在加载 PowerShell 时出现以下与语法相关的错误:

And have gotten the following syntax-related error on loading up 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:UsersDanDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1:9 char:27

+             $cmd = @(which <<<<  $_.Content)[0]
    + CategoryInfo          : ObjectNotFound: (which:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

推荐答案

PowerShell 别名不允许参数.
它们只能引用一个命令名称,它可以是一个 cmdlet 或函数的名称,也可以是一个脚本或可执行文件的名称/路径.

PowerShell aliases do not allow for arguments.
They can only refer to a command name, which can be the name of a cmdlet or a function, or the name / path of a script or executable.

为了得到你想要的东西,你需要定义一个函数:

To get what you are after, you need to define a function:

function django-admin-jy {
    jython.exe /path/to/jython-dev/dist/bin/django-admin.py @args
}

这使用了自 PowerShell 2.0 以来可用的功能,称为参数 splatting:您可以将 @ 应用于引用数组或哈希表的变量名称.
在这种情况下,我们将其应用于 自动变量 名为 args,其中包含所有参数(未绑定到显式声明的参数 - 请参阅 about_Functions).

This uses a feature available since PowerShell 2.0 called argument splatting: you can apply @ to a variable name that references either an array or a hashtable.
In this case, we apply it to the automatic variable named args, which contains all arguments (that weren't bound to explicitly declared parameters - see about_Functions).

如果你想要一种真正通用的方法来创建带参数的别名函数,试试这个:

If you want a truly generic way to create aliases functions that take parameters, try this:

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'

这篇关于相当于 PowerShell 中的 Bash 别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆