简体   繁体   English

从另一个Shell脚本仅调用一个函数

[英]call only one function from another shell script

cat global_features_script.sh 猫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 猫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

When i execute: . 当我执行时: child_script.sh child_script.sh

expected output: I need to get run only one function (svn_credentails) from global_features_script.sh 预期的输出:我只需要从global_features_script.sh运行一个函数(svn_credentails)

output i am getting is: its calling all other functions and also other shell scripts that are listed in global_features_script.sh 我得到的输出是:它调用所有其他函数以及global_features_script.sh中列出的其他shell脚本

From my understanding, the . master_script.sh 据我了解, . master_script.sh . master_script.sh will just insert the master script into the execution of the child_script.sh , so you'll actually be running both scripts. . master_script.sh只会将主脚本插入到child_script.sh的执行中,因此您实际上将在运行这两个脚本。 The simplest thing, in my opinion, would be to just create a common_functions.sh header file that has all the common functions in it, and then just source that header file in either master or child. 在我看来,最简单的事情是创建一个具有所有常用功能的common_functions.sh头文件,然后仅在master或child头中提供该头文件。

A quick syntax note, i'd recommend using source master_script.sh rather than the . 快速的语法注释,我建议使用source master_script.sh而不是. . It should be functionally the same, but it's slightly cleaner and more readable. 它在功能上应该是相同的,但是它稍微更整洁并且更具可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM