From a05761b9a0cafa01fc837798e030cf01f46026c3 Mon Sep 17 00:00:00 2001 From: Alexandru Avadanii Date: Thu, 19 Oct 2017 23:58:05 +0200 Subject: lib.sh: Fix locals override in nested invocations 'wait_for' bash function is nested in another 'wait_for' call in some places, which leads to inner calls interfering with outer calls by overriding the locally scoped variables, including the 'attempt' internal counter. In some cases, the outer 'wait_for' would exit after a single attempt. Fix that by running all contents of `wait_for` inside a subshell, which inherits outer calls variables, but does not override them when the inner call is finished. Change-Id: I450eda3d023af2380c61ee930071fbfc393a5645 Signed-off-by: Alexandru Avadanii (cherry picked from commit 2776ab9d850e764a92c30c858befc48e474f6dd4) --- mcp/scripts/lib.sh | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'mcp/scripts') diff --git a/mcp/scripts/lib.sh b/mcp/scripts/lib.sh index da129a7da..a165bb367 100644 --- a/mcp/scripts/lib.sh +++ b/mcp/scripts/lib.sh @@ -191,15 +191,18 @@ function parse_yaml { } function wait_for { - local total_attempts=$1; shift - local cmdstr=$* - local sleep_time=10 - echo "[NOTE] Waiting for cmd to return success: ${cmdstr}" - # shellcheck disable=SC2034 - for attempt in $(seq "${total_attempts}"); do - # shellcheck disable=SC2015 - eval "${cmdstr}" && return 0 || true - echo -n '.'; sleep "${sleep_time}" - done - return 1 + # Execute in a subshell to prevent local variable override during recursion + ( + local total_attempts=$1; shift + local cmdstr=$* + local sleep_time=10 + echo "[NOTE] Waiting for cmd to return success: ${cmdstr}" + # shellcheck disable=SC2034 + for attempt in $(seq "${total_attempts}"); do + # shellcheck disable=SC2015 + eval "${cmdstr}" && return 0 || true + echo -n '.'; sleep "${sleep_time}" + done + return 1 + ) } -- cgit 1.2.3-korg