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

时间:2012-07-13 17:48:39

标签: linux bash function shell inheritance

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

当我执行时:。 child_script.sh

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

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

1 个答案:

答案 0 :(得分:5)

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

快速语法提示,我建议使用source master_script.sh而不是.。它应该在功能上相同,但它更清晰,更易读。