Last active
February 17, 2022 11:41
-
-
Save pdcastro/90cb9b72737d9d3dac598c4cab552d5b to your computer and use it in GitHub Desktop.
balenaOS shell script to get a full container name given the service name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Get a full container name given its short service name. | |
# The full container name can then be used as argument for | |
# the `balena-engine exec` command. | |
# | |
# Usage instructions: | |
# | |
# * Save this file somewhere, e.g.: | |
# $ vi /tmp/get_container_name.sh | |
# | |
# * Give it executable permissions (one off) with: | |
# $ chmod +x /tmp/get_container_name.sh | |
# | |
# * Run it with the service name as argument, and it will print | |
# the full container name as output: | |
# $ /tmp/get_container_name.sh main | |
# main_4578820_2075969_8746957653480dc984ab2dc8fd6587c7 | |
function get_container_name { | |
local service="$1" | |
local len="${#service}" | |
local -a names | |
readarray -t names <<< "$(balena-engine ps --format '{{.Names}}')" | |
local result='' | |
local name | |
for name in "${names[@]}"; do | |
if [ "${name:0:len+1}" = "${service}_" ]; then | |
result="${name}" | |
break | |
fi | |
done | |
echo "${result}" | |
} | |
get_container_name "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment