Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
apply1() (
body=$2
block() {
eval "$body"
}
block "$1"
)
apply2() (
body=$3
block() {
eval "$body"
}
block "$1" "$2"
)
touch file_a
apply1 file_a "$(cat <<-EOF
echo "Copying \$1 to file_copy"
cp \$1 file_copy
EOF
)"
apply2 file_copy file_b "$(cat <<-EOF
echo "Copying \$1 to \$2"
cp \$1 \$2
EOF
)"
lambda="$(cat <<-EOF
echo "Copying \$1 to \$2"
cp \$1 \$2
EOF
)"
apply2 file_b file_c "$lambda"
rm file_*
#!/bin/bash
# Functions with lambda/closure passed into as second argument
run_task () {
echo " - $1"
eval "$2"
if [ $? -eq 0 ]; then
echo " [done]"
else
echo " [failed]"
exit -1
fi
}
run_grouped () {
echo " - $1"
eval "$2"
echo " [done]"
}
run_subtask () {
echo " - $1"
eval "$2"
if [ $? -eq 0 ]; then
echo " [done]"
else
echo " [failed]"
exit -1
fi
}
# Passing lambdas into functions
run_task "create file_a" "$(cat <<-TASK
touch file_a
TASK
)"
run_grouped "create file_b and file_c" "$(cat <<-GROUPED
run_subtask "create file_b" "$(cat <<-TASK
touch file_b
TASK
)"
run_subtask "create file_c" "$(cat <<-TASK
if [ ! -d dir_a/dir_b ]; then
mkdir dir_a/dir_b -p
fi
touch dir_a/dir_b/file_c
TASK
)"
GROUPED
)"
# Unfortunatelly <<- requires tabs :/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.