From 2707adaa056fb804a386dd6c30c84c1c3935c5a2 Mon Sep 17 00:00:00 2001 From: Joseph Dalrymple Date: Fri, 31 Mar 2023 05:52:13 -0500 Subject: [PATCH] Updated function calls to pass in current element when looping --- demo/function-args-loops | 48 +++++++++++++++++++++++++++++++ mo | 15 ++++++++-- tests/function-args-loop.env | 9 ++++++ tests/function-args-loop.expected | 8 ++++++ tests/function-args-loop.template | 6 ++++ 5 files changed, 83 insertions(+), 3 deletions(-) create mode 100755 demo/function-args-loops create mode 100644 tests/function-args-loop.env create mode 100644 tests/function-args-loop.expected create mode 100644 tests/function-args-loop.template diff --git a/demo/function-args-loops b/demo/function-args-loops new file mode 100755 index 0000000..4308966 --- /dev/null +++ b/demo/function-args-loops @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# Example for how #54 can get implemented. + +cd "$(dirname "$0")" # Go to the script's directory + +# The links are associative arrays +declare -a links +declare -A names urls + +links+=(resque) +names[resque]=Resque +urls[resque]=http://example.com/resque + +links+=(hub) +names[hub]=Hub +urls[hub]=http://example.com/hub + +links+=(rip) +names[rip]=Rip +urls[rip]=http://example.com/rip + +# helper functions +link_name() { + # Trying to use unique names + local key + key=$(cat) + echo ${names[$key]} +} + +link_url() { + # Trying to use unique names + local key + key=$(cat) + echo ${urls[$key]} +} + + +# Source mo in order to work with arrays +. ../mo + +# Process the template +cat <