2011-05-07 59 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 
}