Bash 将两个shell命令组合成一个命令
在Bash中,我们可以组合两个shell命令Bash 将两个shell命令组合成一个命令,bash,powershell,powershell-2.0,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 $> pwd|ls Directory: D:\ps Mode LastWriteTime Length Name
cd
和ls
,如下所示:
function cd {
builtin cd "$@" && ls
}
#this will get a list of file after changing into a directory
$> 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
}
还有这个
mkcd () { mkdir -p "$@" && cd "$@"; }
#this will create and directory and change into it at once
我们可以在Powershell中做类似的事情吗?如果是这样的话,我想做类似的函数,并把它放在我的$profile中
谢谢你的帮助。钢铁用户 编辑: 我意识到这可以从壳牌公司这样做:
function cd {
builtin cd "$@" && ls
}
#this will get a list of file after changing into a directory
$> 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
}
这可以放在如下配置文件中:
function cd {
builtin cd "$@" && ls
}
#this will get a list of file after changing into a directory
$> 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功能。类似的功能应该可以工作
Function mkcd {
mkdir $args[0]
cd $args[0]
}
这只是powershell中的一个普通函数。有关详细信息,请参阅。您可能还希望管理异常
目录已存在
,并将目录对象返回给调用者:
Function mkcd {
if(!(Test-Path -path $args[0])) {
mkdir $args[0]
}
cd $args[0] -passthru
}
函数mkcd{
if(!(测试路径-路径$args[0])){
mkdir$args[0]
}
cd$args[0]-通过
}
可以,但不管理现有目录,并返回
$null
。看看我的答案。