寻找一种方法来包装一个命令,并给自己的shell

我正在使用相同的模式手动执行相同的命令多次手动命令行。 现在我正在寻找一种方法来简化它只需input一次命令。

让我们看看一个普通的docker例子:

docker ps docker ps -a docker ps -l docker stop x docker start x docker start y docker logs y docker logs -fz 

这个例子也适用于更多的命令,如git,brew,gulp,gcloud。

现在我正在寻找某种命令封装器的shell,它允许我with docker来编写,它将把任何命令封装在嵌套/子shell中。

然后,我不需要预先执行docker命令,只需调用:

 >ps # does docker ps and displays result >stop x # prepends docker so docker stop x is actually executed CTRL+C # to exit the command wrapper 

这样的事情已经存在了吗? 我正在Google上search,但无法正确描述,因此我没有find任何东西。

您也可以自己定义一个函数,并将其包含在.bash_profile或类似文件中:

 function with { echo -n "$1> " while read input do if [[ $input == "exit" ]] then break fi eval "$1 $input" echo -n "$1> " done echo } 

用法示例:

 user@host $ with git git> status # On branch master nothing to commit (working directory clean) git> exit user@host $ 

编辑:这个function不会做input消毒或任何东西,所以使用您自己的风险等…

你可以写一个bash或其他shell脚本来做到这一点。 例如,一个简单的替代方法几乎同样好,只是定义短的别名并将它们预先设置好

 alias d=docker alias g=gcloud 

等等。 然后运行

 d ps d ps -a 

等等,这比input命令更难。