tags: Shell

Read a File and Redirect the Output to the Same File

To write outputs to a file in a shell, we can use redirection. However, when we read a file and modify the content and redirect to the same original file, we’ll get just an empty file. What’s going on here? $ cat file bravo delta charlie alpha charlie $ cat file | sort | uniq > file $ cat file # empty file $ Of course, if you redirect to a different file and rename it to the original name, it works as expected. This behavior is a little annoying when we want to modify a file like sorting and removing duplicates and do it without creating any temporary file. The reason redirecting output to the source file ends up an empty file is the redirection truncates the destination file earlier than the command actually starts processing.

Read more →

Variables with Shell-Style Default in docker-compose.yml

Today I learned that in docker-compose.yml, a variable interpolation with a default can be expressed as ${VAR1:-default value} like shell-style parameter expansion. With parameter expansion, you can do more than setting a default value such as modifying the parameter. However, the supported features in docker-compose.yml are limited to two of them: to set a default variable or to make the parameter mandatory. Let’s define VAR_ALPHA with a default value for the example service.

Read more →

Aggregate Multiple Outputs with Named Pipes

Sometimes I want to make multiple outputs from multiple processes into one single output. Let’s say we have to aggregate K8s pod logs from a deployment. We could do that by redirecting each output to a file and follow the file by tail -f like below. tempfile=$(mktemp /tmp/rip-example.XXXXXXXXXX) trap "rm \"$tempfile\"" EXIT pods=$(kubectl get pods | grep Running | grep -oe "app-[a-z0-9\-]\+") for pod in $pods do kubectl logs -f "$pod" >> "$tempfile" & done tail -f "$tempfile" However, that method consumes disk spaces. Even worse, all the output data remains in the local disk if the process were killed with 9 (SIGKILL). There must be better ways. That’s where named pipe comes to play.

Read more →

Gentaro "hibariya" Terada

Otakanomori, Nagareyama, Chiba, Japan
Email me

Likes Ruby, Internet, and Programming.