我可以为子命令设置别名吗?(缩短`docker ps`的输出) [英] Can I alias a subcommand? (shortening the output of `docker ps`)

查看:22
本文介绍了我可以为子命令设置别名吗?(缩短`docker ps`的输出)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

docker 命令有一个发出很长行的 ps 子命令:

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}}	{{.Names}}	{{.Ports}}	{{.Status}}"

我希望能够说 docker ps 并为我添加 --format "table..." 参数.有没有很好的方法来做到这一点?

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?

我知道我可以说

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

但我更愿意保留子命令.

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

我使用 zsh 作为我的外壳.

I'm using zsh as my shell.

推荐答案

您可以将 docker 包装在一个函数中,该函数检查特定的子命令并传递其他所有内容.(以下内容实际上不仅适用于 zsh,还适用于任何符合 POSIX 的 shell——zsh 并不完全属于这一类别).

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}}	{{.Names}}	{{.Ports}}	{{.Status}}' "$@"
      ;;
    *)
      command docker "$@";;
  esac
}

<小时>

如果你想要一个更通用的包装函数(不需要知道你想要的特定 ps 逻辑),可以按如下方式完成(注意这个版本不是 与基线 POSIX sh 兼容,因为它使用了 local;然而,这是一个扩展,甚至由 ash 及其衍生工具实现):


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
}

...之后,任何子命令都可以定义自己的函数,而无需修改包装器即可了解它们(您也可以在名为 docker_ps 的 PATH 中创建一个脚本,或提供您选择的任何其他方式的命令):

...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}}	{{.Names}}	{{.Ports}}	{{.Status}}' "$@"
}

这篇关于我可以为子命令设置别名吗?(缩短`docker ps`的输出)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆