1

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

When i execute: . child_script.sh

expected output: I need to get run only one function (svn_credentails) from global_features_script.sh

output i am getting is: its calling all other functions and also other shell scripts that are listed in global_features_script.sh

kumar
  • 389
  • 1
  • 9
  • 28
  • Wait. `child_script.sh` calls `master_script.sh`, and `master_script.sh` calls `child_script.sh`? – chepner Jul 13 '12 at 18:00
  • child_script is calling master_script. I know its should be reverse side. but just to make the confusion out, I will rename the master file as global_features_script.sh – kumar Jul 13 '12 at 18:10
  • My point is, you seem to have mutual recursion between the two files. – chepner Jul 13 '12 at 18:21
  • chepner, can you elaborate your comment please. – kumar Jul 13 '12 at 18:33
  • Ugh, never mind. I misread "child1_script.sh" and "child2_script.sh" as two calls to "child_script.sh". – chepner Jul 13 '12 at 18:36
  • Is there a reason that you're redirecting your echos to the terminal? Are you redirecting the output of the script elsewhere? If you use `read -p "prompt text" instead of the echos, the prompts go to stderr automatically. – Dennis Williamson Jul 13 '12 at 18:36
  • Yes i am executing the script as .child_script.sh >> child_script.log – kumar Jul 13 '12 at 18:42
  • possible duplicate of [Can I call a function of a shell script from another shell script](http://stackoverflow.com/questions/10822790/can-i-call-a-function-of-a-shell-script-from-another-shell-script) – givanse Aug 10 '14 at 01:05

1 Answers1

5

From my understanding, the . 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. 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.

A quick syntax note, i'd recommend using source master_script.sh rather than the .. It should be functionally the same, but it's slightly cleaner and more readable.

Adeel
  • 66
  • 1