将两个shell命令组合到一个命令中

Ani*_*esh 2 bash powershell powershell-2.0

在Bash中,我们可以组合两个shell命令cd,ls如下所示:

function cd {
    builtin cd "$@" && ls
}
#this will get a list of file after changing into a directory
Run Code Online (Sandbox Code Playgroud)

还有这个

mkcd () { mkdir -p "$@" && cd "$@"; }
#this will create and directory and change into it at once
Run Code Online (Sandbox Code Playgroud)

我们可以在Powershell中做类似的事吗?如果是这样,我想做类似的功能,并把它放在我的$ profile中

谢谢你的帮助.
Steeluser

编辑:

我意识到这可以通过shell完成,如下所示:

$> pwd|ls

    Directory: D:\ps

Mode                LastWriteTime     Length Name                                                                      
----                -------------     ------ ----                                                                      
d----          5/7/2011   9:40 PM            config                                                                    
d----          5/7/2011   9:40 PM            output                                                                    
d----          5/8/2011   3:37 AM            static                                                                    
-a---          5/8/2011   3:36 AM        485 create-static-files.ps1                                                   
Run Code Online (Sandbox Code Playgroud)

这可以放在这样的配置文件中:

function pl
{
    pwd|ls
}
Run Code Online (Sandbox Code Playgroud)

并且可以从shell调用

ps$ pl

    Directory: D:\ps

Mode                LastWriteTime     Length Name                                                                      
----                -------------     ------ ----                                                                      
d----          5/7/2011   9:40 PM            config                                                                    
d----          5/7/2011   9:40 PM            output                                                                    
d----          5/8/2011   3:37 AM            static                                                                    
-a---          5/8/2011   3:36 AM        485 create-static-files.ps1                                                   
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚如何做mkcd功能.

Zac*_*son 7

这样的事情应该有效.

Function mkcd {
  mkdir $args[0]
  cd $args[0]
}
Run Code Online (Sandbox Code Playgroud)

这只是PowerShell中的一个普通函数.有关更多信息,请参见http://technet.microsoft.com/en-us/library/dd347712.aspx.


Emi*_*ggi 6

您可能还需要管理异常directory already exists并将目录对象返回给调用者:

Function mkcd {
  if(!(Test-Path -path $args[0])) {
   mkdir $args[0]
  }
  cd $args[0] -passthru
}