0

I have a master script which has few function definition. Using a child script im trying to call the master script along with passing the arguments to the function. Problem here is my master script is getting executed twice. 1. Everytime when i call my child script, Firstly all the function in master script is getting executed without any argument (i.e. Empty arguments) 2. Then second time Argument value is getting passed and executing the function successfully as expected.

Kindly let me how to avoid the execution of step-1 specified above.

I have tried with source (or) . followed by specifying the master script inside the child script and then followed by specifying the function name along with arguments. But it is not working.

Child Script :


source <directory_path>/master.sh 
add '123' 'get' 'R'

Master Script :


#! /bin/bash

add()
{
exec &> $1_$(date "+%Y%m%d%H%M").log
Change=$1
Command=$2
if [[ $Command == "get" ]];
then Command_1="getfacl"
elif [[ $Command == "set" ]];
then Command_1="setfacl"
elif [[ $Command == "ch" ]];
then Command_1="chown"
else Command_1="ls"
fi
Tag=$3
if [[ -z "$Tag" ]];
then Tag=""
else Tag="-$Tag"
fi
}
add;

Child Script :


source <directory_path>/master.sh 
add '123' 'get' 'R'

Only one file should get generated as per the script. But actual result generates 2 files.

Expected Result


123_201909201012.log 

Actual Result


_201909201012.log 
123_201909201012.log
0

1 Answer 1

1

just after the declaration of your function, you are executing it immediately:

add()
{
...
}
add; # you execute it here... remove this line
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.