gpt4 book ai didi

linux - 将一个shell函数从当前shell脚本写入另一个shell脚本

转载 作者:行者123 更新时间:2023-12-04 19:25:11 25 4
gpt4 key购买 nike

我有一个 zsh shell 脚本,如下所示:

# ! /usr/bin/env zsh
my_function () {
echo "my function is running"
python3 my_python_script.py
}
我想调用这个脚本并复制函数 my_function<file_location>/test.sh .
我目前的尝试如下所示:
# ! /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
是否可以复制此函数,理想情况下无需先将整个函数转换为字符串?
编辑:添加了带有转义的丢失的美元符号,导致调用 python 脚本而不是复制为字符串

最佳答案

虽然我不建议将此方法用于测试以外的任何目的,但您可以通过 type 来实现此目标。命令。

# 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"
}

关于linux - 将一个shell函数从当前shell脚本写入另一个shell脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71798597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com