2011-05-07 63 views
1

在bash中,我们可以将两个shell命令cdls这样的:组合两个shell命令为一个命令

function cd { 
    builtin cd "[email protected]" && ls 
} 
#this will get a list of file after changing into a directory 

而且这个

mkcd() { mkdir -p "[email protected]" && cd "[email protected]"; } 
#this will create and directory and change into it at once 

我们可以做类似的PowerShell的东西吗?如果是这样,我想作出类似的功能,并把它放在我的$ profile

感谢您的任何帮助。
Steeluser

编辑:

我意识到这可能从外壳就像这样:

$> 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             

这可以放在一个配置文件是这样的:

function pl 
{ 
    pwd|ls 
} 

并且可以从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             

但我想不出如何做mkcd函数。

回答

4

像这样的东西应该工作。

Function mkcd { 
    mkdir $args[0] 
    cd $args[0] 
} 

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

+0

感谢扎克,这个工作! – Animesh 2011-05-08 00:01:49

+0

这很好,但不管理现有目录,并返回'$ null'。看到我的答案。 – 2011-05-08 06:33:06

4

您可能还需要管理异常directory already exists,并返回目录对象到您的来电者:

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