简体   繁体   中英

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

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM