gpt4 book ai didi

linux - 从另一个 shell 脚本中只调用一个函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:30:40 24 4
gpt4 key购买 nike

cat global_features_script.sh

.child1_script.sh
.child2_script.sh

function svn_credentials
{
echo -n "Enter the svn username and press [ENTER]: " > /dev/tty
read svn_username
echo -n "Enter the svn commit message and press [ENTER]: " > /dev/tty
read svn_message
echo -n "Enter your svn password and press [ENTER]: " > /dev/tty
read -s svn_password
}
if [ a == b]
then
echo "a is equal to be b"
else
echo "a is not equal to b"
fi

function exit_error
{
echo " There is an error in the command, please check it"
exit 1
}

cat child_script.sh

. global_features_script.sh
svn_wc=temp_dir
svn_credentials # calling function from global_features_script.sh
svn commit $svn_wc -m "$svn_message" --username $svn_username --password $svn_password

当我执行时: .子脚本.sh

预期输出:我只需要从 global_features_script.sh 中运行一个函数 (svn_credentails)

我得到的输出是:它调用所有其他函数以及 global_features_script.sh 中列出的其他 shell 脚本

最佳答案

据我了解,. master_script.sh只会将主脚本插入到 child_script.sh 的执行中,因此您实际上将同时运行这两个脚本。在我看来,最简单的事情就是创建一个 common_functions.sh。包含所有常用函数的头文件,然后只需在主文件或子文件中获取该头文件。

快速语法说明,我建议使用 source master_script.sh而不是 . .它在功能上应该是相同的,但它稍微更简洁且更具可读性。

关于linux - 从另一个 shell 脚本中只调用一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11475671/

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