Test shebangs

This commit is contained in:
Alexandre
2025-07-30 06:23:58 +02:00
committed by GitHub
parent e277df585c
commit 9416c80b14

View File

@@ -27,18 +27,36 @@ run_script() {
sed -i -E 's|s6-setuidgid[[:space:]]+([a-zA-Z0-9._-]+)[[:space:]]+(.*)$|su -s /bin/bash \1 -c "\2"|g' "$runfile"
fi
# Get current shebang, if not available use another
currentshebang="$(sed -n '1{s/^#![[:blank:]]*//p;q}' "$SCRIPTS")"
if [ ! -f "${currentshebang%% *}" ]; then
for shebang in "/usr/bin/env bashio" "/usr/bin/with-contenv bashio" "/command/with-contenv bashio" "/usr/bin/bashio" "/usr/bin/bash" "/usr/bin/sh" "/bin/bash" "/bin/sh"; do
command_path="${shebang%% *}"
if [ -x "$command_path" ] && "$command_path" echo "yes" > /dev/null 2>&1; then
echo "Valid shebang: $shebang"
# Prepare candidate shebangs: contenv first if PID1
candidate_shebangs=()
if $PID1; then
candidate_shebangs+=("/command/with-contenv bashio" "/usr/bin/with-contenv bashio")
fi
candidate_shebangs+=(
"/usr/bin/env bashio"
"/usr/bin/bashio"
"/usr/bin/bash"
"/usr/bin/sh"
"/bin/bash"
"/bin/sh"
)
for shebang in "${candidate_shebangs[@]}"; do
command_path="${shebang%% *}"
if [ -x "$command_path" ]; then
tmpfile="$(mktemp)"
echo "#!$shebang" > "$tmpfile"
echo "bashio::addon.version" >> "$tmpfile"
chmod +x "$tmpfile"
output="$("$tmpfile" 2>/dev/null)"
rm -f "$tmpfile"
if [[ -n "$output" ]]; then
echo "Valid shebang: $shebang (output: $output)"
sed -i "1s|^#![^\n]*|#!$shebang|" "$SCRIPTS"
break
fi
done
sed -i "s|$currentshebang|$shebang|g" "$SCRIPTS"
fi
fi
done
# Use source to share env variables when requested
if [[ "$script_kind" == service ]]; then
@@ -62,11 +80,18 @@ run_script() {
}
# Loop through /etc/cont-init.d/*
[[ -d /etc/cont-init.d ]] && \
for SCRIPTS in /etc/cont-init.d/*; do
[ -e "$SCRIPTS" ] || continue
run_script "$SCRIPTS" script
done
[[ -d /etc/s6-overlay/s6-rc.d ]] && \
for SCRIPTS in /etc/s6-overlay/s6-rc.d/*/run; do
[ -e "$SCRIPTS" ] || continue
run_script "$SCRIPTS" script
done || true
# Start services.d
if [ "$$" -eq 1 ]; then
for service_dir in /etc/services.d/*; do