I have a zsh shell script that looks as following:
# ! /usr/bin/env zsh
my_function () {
echo "my function is running"
python3 my_python_script.py
}
I would like to call this script and copy the function my_function
to <file_location>/test.sh
.
My current attempt looks as following:
# ! /usr/bin/env bash
BASEDIR=$(dirname "$0")
cat <<EOF > $BASEDIR/test.sh
my_function () {
echo "my function is running"
output_string=\$(python3 my_python_script.py);
}
EOF
Is it possible copy this function, ideally without turning the entire function into a string first?
EDIT: Added missing dollarsign with escape that caused python script to be called intstead of copied as a string
While I wouldn't recommend using this method for anything other than testing purposes, you can achieve this goal via the type
command.
# cat test.sh
testfunc() {
echo "Running testfunc"
}
# Append the function definition to tmp.sh, using tail to skip the 'testfunc is a function' line
type testfunc | tail -n +2 >> tmp.sh
# ./test.sh
# cat tmp.sh
testfunc ()
{
echo "Running testfunc"
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.