How can I make an alias for this command piping docker-compose logs -f into less?

Alden W. :

I have a working command which I would like to make an alias for, but can't seem to figure out how to do. The working command is

docker-compose logs -f | less -S -R +F

This works fine, essentially removing word wrap from the logs.

The caveat is that when using this I also want to be able to optionally add arguments to the middle, to specify the particular service(s) to tail. Making something that end up looking like this

docker-compose logs -f service1 service2 etc... | less -S -R +F

To be able to do this I was thinking I could use xargs -I to pass in a variable number of arguments and inject them into the middle of the command. But my skill with bash is very limited, and so no matter how much I fiddle with it I can't seem to get it to work, and am sure there is something/some concept I'm missing.

The last iteration of the alias I tried before posting this is

alias logsx="xargs -I{} bash -c 'docker-compose logs -f \"{}\" | less -S -R +F'"

which when you run it seems to start less but without the docker logs being piped there.

Any help would be greatly appreciated. Thanks!

Quasímodo :

All you need is a function instead.

logsx() {
    docker-compose logs -f "$@" | less -S -R +F
}

"$@" will expand the arguments given. So you write logsx service1 service2.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=174898&siteId=1