Updates for how to source "mo" and then use it

When you source mo, it adds the "mo" function to the environment.
This commit is contained in:
Tyler Akins 2015-08-27 09:44:04 -05:00
parent e623b16bb8
commit b16d73b5a7
8 changed files with 18 additions and 9 deletions

View File

@ -47,14 +47,15 @@ Using arrays adds a slight level of complexity. *You must source `mo`.* Look a
#!/bin/bash #!/bin/bash
cd "$(dirname "$0")" # Go to the script's directory cd "$(dirname "$0")" # Go to the script's directory
export ARRAY=( one two "three three three" four five ) export ARRAY=( one two "three three three" four five )
cat << EOF | . ../mo . ../mo # This loads the "mo" function
cat << EOF | mo
Here are the items in the array: Here are the items in the array:
{{#ARRAY}} {{#ARRAY}}
* {{.}} * {{.}}
{{/ARRAY}} {{/ARRAY}}
EOF EOF
The result? You get a list of the five elements in the array. Take a look at the line that executes `mo`. You'll see that we went from `../mo` to using `. ../mo`. That is very important when you want arrays to work, since you can not execute a command and have arrays passed to that command's environment. Instead, we source the file. `. ../mo` is identical to `source ../mo` and you can find more about that in bash's man page. The result? You get a list of the five elements in the array. It is vital that you source `mo` and run the function when you want arrays to work because you can not execute a command and have arrays passed to that command's environment. Instead, we first source the file to load the function and then run the function directly.
There are more scripts available in the [demos directory](demo/) that could help illustrate how you would use this program. There are more scripts available in the [demos directory](demo/) that could help illustrate how you would use this program.

View File

@ -4,8 +4,9 @@ cd "$(dirname "$0")" # Go to the script's directory
declare -A DATA declare -A DATA
DATA=([one]=111 [two]=222) DATA=([one]=111 [two]=222)
. ../mo
cat <<EOF | . ../mo cat <<EOF | mo
Accessing data directly: Accessing data directly:
DATA: {{DATA}} DATA: {{DATA}}
One: {{DATA.one}} One: {{DATA.one}}

View File

@ -11,7 +11,8 @@ export OPTIONS=(
"Call a service representative at 1-800-000-0000 to discuss payment options" "Call a service representative at 1-800-000-0000 to discuss payment options"
"Return the vehicle immediately and pay a fine of 1 million dollars" "Return the vehicle immediately and pay a fine of 1 million dollars"
) )
sed '0,/^# END/ d' "$(basename "$0")" | . ../mo . ../mo
sed '0,/^# END/ d' "$(basename "$0")" | mo
exit exit
# END # END

View File

@ -28,8 +28,8 @@ declare -A resque hub rip
resque=([name]=Resque [url]=http://example.com/resque) resque=([name]=Resque [url]=http://example.com/resque)
hub=([name]=Hub [url]=http://example.com/hub) hub=([name]=Hub [url]=http://example.com/hub)
rip=([name]=Rip [url]=http://example.com/rip) rip=([name]=Rip [url]=http://example.com/rip)
. ../mo
cat <<EOF | . ../mo cat <<EOF | mo
{{#EVERY_REPO}} {{#EVERY_REPO}}
The repo is __REPO__ The repo is __REPO__
Name: {{__REPO__.name}} Name: {{__REPO__.name}}

View File

@ -8,7 +8,8 @@ wrapper() { echo -n "*** $1 ***"; }
export IP=127.0.0.1 export IP=127.0.0.1
export ALLOWED_HOSTS=( 192.168.0.1 192.168.0.2 192.168.0.3 ) export ALLOWED_HOSTS=( 192.168.0.1 192.168.0.2 192.168.0.3 )
cat <<EOF | . mo . mo # Keep in mind this script is executing in the parent directory
cat <<EOF | mo
# {{#wrapper}}OH SO IMPORTANT{{/wrapper}} # {{#wrapper}}OH SO IMPORTANT{{/wrapper}}
# This file automatically generated at {{date-string}} # This file automatically generated at {{date-string}}
home_ip={{IP}} home_ip={{IP}}

View File

@ -1,7 +1,8 @@
#!/bin/bash #!/bin/bash
cd "$(dirname "$0")" # Go to the script's directory cd "$(dirname "$0")" # Go to the script's directory
export ARRAY=( one two "three three three" four five ) export ARRAY=( one two "three three three" four five )
cat << EOF | . ../mo . ../mo
cat << EOF | mo
Here are the items in the array: Here are the items in the array:
{{#ARRAY}} {{#ARRAY}}
* {{.}} * {{.}}

View File

@ -1,4 +1,7 @@
#!/bin/bash #!/bin/bash
#
# This example does not source `mo` and is intentionally restricted to
# variables that are not arrays.
cd "$(dirname "$0")" # Go to the script's directory cd "$(dirname "$0")" # Go to the script's directory
export TEST="This is a test" export TEST="This is a test"
echo "Your message: {{TEST}}" | ../mo echo "Your message: {{TEST}}" | ../mo

View File

@ -2,6 +2,7 @@
cd "$(dirname $0)" cd "$(dirname $0)"
. ./mo
PASS=0 PASS=0
FAIL=0 FAIL=0
@ -11,7 +12,7 @@ for TEST in tests/*.expected; do
echo -n "$BASE ... " echo -n "$BASE ... "
echo "Do not read this input" | ( echo "Do not read this input" | (
. "${BASE}.env" . "${BASE}.env"
. ./mo "${BASE}.template" mo "${BASE}.template"
) | diff -wU5 - "${TEST}" > "${BASE}.diff" ) | diff -wU5 - "${TEST}" > "${BASE}.diff"
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then