相当于Powershell中的Bash别名 [英] Equivalent to Bash Alias in Powershell

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

问题描述

一个新手的权力问题:



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



别名django-admin-jy =jython /path/to/jython-dev/dist/bin/django-admin.py



到目前为止,我发现这是非常困难的。



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

- 没有明确的方式允许在PowerShell函数调用上无限数量的参数



-Powershell似乎阻止stdout






更新:为powerhell中bash别名模拟的工作实现添加了150个奖励。



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



并在加载powershell时收到以下与语法有关的错误:






 术语哪个不被识别为cmdlet,函数,脚本文件或可操作程序的名称。检查名称的法术
,或者如果包含路径,请验证路径是否正确,然后重试。
在C:\Users\Dan\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:9 char:27

+ $ cmd = @(< < $ _。Content)[0]
+ CategoryInfo:ObjectNotFound:(其中:String)[],CommandNotFoundException
+ FullyQualifiedErrorId:CommandNotFoundException
/ pre>




我会接受一个修正,作为一个答案。

解决方案

PowerShell别名可用于包含EXE的任何命令:

  new-alias n notepad.exe 

更新:as在评论中提到,PowerShell别名不允许参数。它们是命令名称的简单别名,而不再是命令名称。要获得你以后的东西(我认为自从我不熟悉Bash别名)尝试这样:

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

它使用PowerShell 2.0的功能,称为参数分割。您可以将 @ 应用于引用数组或散列表的变量名称。在这种情况下,我们将它应用于名为 args 的变量,其中包含所有未命名的参数。



如果您想要一个真正通用的方法来创建参数的别名函数,请尝试:

 函数New-BashStyleAlias([string] $ name,[string] $ command)
{
$ sb = [scriptblock] :: Create($ command)
新项目功能:\global:$ name-Value $ sb | Out-Null
}

新建BashStyleAlias django-admin-jy'jython.exe /path/to/jython-dev/dist/bin/django-admin.py @args'

关于Joel的做法,我怀疑他有哪个别名到 Get-Command 。尝试用 Get-Command 替换 。


a newbie powershell question:

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 Aliases only work with Powershell commands + function calls

-No clear way to allow for an unlimited number of args on a powershell function call

-Powershell seems to block stdout


UPDATE: Bounty of 150 pts added for a working implementation of bash alias simulation in powershell.

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

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:\Users\Dan\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:9 char:27

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


I'll accept a fix to this as an answer as well.

解决方案

PowerShell aliases can be used for any "command" including EXEs:

new-alias n notepad.exe

Update: as mentioned in the comments, PowerShell aliases do not allow for arguments. They are simple "aliases" for a command name and nothing more. To get what you are after (I think since I'm not familiar with Bash aliases) try this:

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

It uses a feature of 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 variable named args which contains all the unnamed parameters.

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'

Regarding the issue with Joel's approach, I suspect he has which aliased to Get-Command. Try replacing which with Get-Command.

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

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