代码之家  ›  专栏  ›  技术社区  ›  RightFullRudder

相当于PowerShell中的bash别名

  •  8
  • RightFullRudder  · 技术社区  · 15 年前

    新手PowerShell问题:

    我想让PowerShell中的别名与此bash别名完全等效:

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

    到目前为止,在修补它的过程中,我发现这是非常困难的。

    -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
    
    2 回复  |  直到 8 年前
        1
  •  9
  •   Keith Hill    9 年前

    PowerShell别名可用于任何“命令”,包括exes:

    new-alias n notepad.exe
    

    更新 :如注释中所述,PowerShell别名不允许使用参数。它们只是命令名的简单“别名”,仅此而已。要获得您想要的内容(我认为因为我不熟悉bash别名),请尝试以下操作:

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

    它使用了PowerShell2.0的一个称为参数展开的功能。你可以申请 @ 引用数组或哈希表的变量名。在本例中,我们将它应用于名为 args 其中包含所有未命名的参数。

    如果要使用真正通用的方法来创建接受参数的别名函数,请尝试以下操作:

    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'
    

    关于乔尔方法的问题,我怀疑他 which 别名为 Get-Command . 尝试替换 哪一个 具有 GET命令 .

        2
  •  1
  •   Joey Gumbo    15 年前

    函数可以有任意多个参数。你只需要使用 $args 访问它们。

    至于性传播疾病:你到底在经历什么?