How to embed a foreign script: We can comment out the "foreign" script part and then "embeded" it in a bash script; then use sed or some way to "extract" that part and execute it.
sed -n ' ## do not automatically print
/^### / { ## for all lines that begins with "### "
s:^### ::; ## delete that "### "
s:FUNC:$1:g; ## replace all "FUNC" with "$1"
p ## print that line
}'
An example to "embed" a python script into a bash script:
#!/bin/bash
ME="$0"
callEmbeded() {
sed -n "/^### /{s:^### ::;s:FUNC:$1:g;p}" <"$ME" | python
}
while read -n1 KEY; do
case "x$KEY" in
x1)
callEmbeded func1
;;
x2)
callEmbeded func2
;;
xq)
exit
;;
*)
echo "What do you want?"
esac
done
### # ----
### # python part
###
### def func1():
### print ("hello world!")
###
### def func2():
### print ("hi there!")
###
### if __name__ == '__main__':
### FUNC()
A similar example to "embed" a bash script into another one:
# cat <<EOF >/dev/null
ME="$0"
callEmbeded() {
cut -c3- <"$ME" | bash -s "$1"
}
while read -n1 KEY; do
case "x$KEY" in
x1)
callEmbeded func1
;;
x2)
callEmbeded func2
;;
xq)
exit
;;
*)
echo "What do you want?"
esac
done
# EOF
# ## ----
# ## embeded part
#
# func1() {
# echo "hello world."
# }
#
# func2() {
# echo "hi there."
# }
#
# eval "$@"