From 82fdb76c3696ff5d797cb8b0b674217fdc13ec5b Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Sat, 9 Sep 2023 12:29:08 +0200 Subject: [PATCH] Improve shebang detection --- .templates/ha_entrypoint.sh | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/.templates/ha_entrypoint.sh b/.templates/ha_entrypoint.sh index 5eb75f19b..9fadb6004 100755 --- a/.templates/ha_entrypoint.sh +++ b/.templates/ha_entrypoint.sh @@ -9,11 +9,22 @@ echo "Starting..." for SCRIPTS in /etc/cont-init.d/*; do [ -e "$SCRIPTS" ] || continue echo "$SCRIPTS: executing" + + # Ensure permissions chown "$(id -u)":"$(id -g)" "$SCRIPTS" chmod a+x "$SCRIPTS" - # Change shebang if no s6 supervision - sed -i 's|/usr/bin/with-contenv bashio|/usr/bin/env bashio|g' "$SCRIPTS" - /usr/bin/env bashio "$SCRIPTS" || echo -e "\033[0;31mError\033[0m : $SCRIPTS exiting $?" + + # Get current shebang, if not available use another + currentshebang="$(sed -n '1{s/^#![[:blank:]]*//p;q}' "$SCRIPTS")" + if [ ! -f "${currentshebang%% *}" ]; then + for shebang in "/command/with-contenv bashio" "/usr/bin/env bashio" "/usr/bin/bashio" "/bin/bash" "/bin/sh"; do if [ -f "${shebang%% *}" ]; then break; fi; done + sed -i "s|$currentshebang|$shebang|g" "$SCRIPTS" + fi + + # Start the script + /./ "$SCRIPTS" || echo -e "\033[0;31mError\033[0m : $SCRIPTS exiting $?" + + # Cleanup rm "$SCRIPTS" done