Can I alias a subcommand? (shortening the output of `docker ps`)

匿名 (未验证) 提交于 2019-12-03 03:06:01

问题:

The docker command has a ps sub-command that emits very long lines:

$ docker ps -a CONTAINER ID        IMAGE                             COMMAND                  CREATED             STATUS                      PORTS                                                                                                                                                                                                                                                                                                        NAMES 6e8ec8a16da4        waisbrot/wait:latest              "/wait"                  4 minutes ago       Exited (0) 4 minutes ago                                                                                                                                                                                                                                                                                                                 wait-for-janus-test 9dbf0739561f        whoop/downsampler:master          "./run.bash"             4 minutes ago       Up 4 minutes                0.0.0.0:32855->4369/tcp, 0.0.0.0:32854->9100/tcp, 0.0.0.0:32853->9101/tcp, 0.0.0.0:32852->9102/tcp, 0.0.0.0:32851->9103/tcp, 0.0.0.0:32850->9104/tcp, 0.0.0.0:32849->9105/tcp, 0.0.0.0:32848->9106/tcp, 0.0.0.0:32847->9107/tcp, 0.0.0.0:32846->9108/tcp, 0.0.0.0:32845->9109/tcp, 0.0.0.0:32844->9110/tcp   metrics-downsampler-test 6cf56623bb48        whoop/janus:master                "./start.bash"           4 minutes ago       Up 4 minutes                0.0.0.0:32843->80/tcp                                                                                                                                                                                                                                                                                        janus-test 882b50303d54        whoop/recalculator:master         "./run.bash"             4 minutes ago       Exited (1) 4 minutes ago                                                                                                                                                                                                                                                                                                                 internum-test 

It can be instructed to output only specific columns:

docker ps --format "table {{.Image}}\t{{.Names}}\t{{.Ports}}\t{{.Status}}" 

I'd like to be able to say docker ps and get the --format "table..." argument added on for me. Is there a nice way to do this?

I know I could say

alias dp='docker ps --format ...' 

but I'd prefer to keep the sub-command.

I'm using zsh as my shell.

回答1:

You can wrap docker in a function that checks for the specific subcommand and passes everything else through. (The below will actually work with not just zsh, but any POSIX-compliant shell -- a category to which zsh doesn't quite belong).

docker() {   case $1 in     ps)       shift       command docker ps --format 'table {{.Image}}\t{{.Names}}\t{{.Ports}}\t{{.Status}}' "$@"       ;;     *)       command docker "$@";;   esac } 

If you wanted a more generic wrapper function (that doesn't need to know about your specific desired ps logic), that could be done as follows (note that this version is not compatible with baseline POSIX sh due to its use of local; however, this is an extension implemented even by ash and its derivatives):

docker() {   local cmd=$1; shift   if command -v "docker_$cmd" >/dev/null 2>/dev/null; then     "docker_$cmd" "$@"   else     command docker "$cmd" "$@"   fi } 

...after which any subcommand can have its own functions defined, without the wrapper needing to be modified to know about them (you could also create a script in the PATH named docker_ps, or provide the command in any other manner you choose):

docker_ps() {   command docker ps --format 'table {{.Image}}\t{{.Names}}\t{{.Ports}}\t{{.Status}}' "$@" } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!