I had a script called server.sh which had both a body (executing things) and a rich set of functions. I now have a script called client.sh which has itself a body, and need some functions which are already defined in server.sh.
I would like to to re-use the functions that are in server.sh when running client.sh, but I don't want to run the body of server.sh.
I have tried the following:
server.sh
#!/bin/bash
beautiful_function()
{
PARAM1=$1
PARAM2=$2
echo "I am the server and I received param1 = ${PARAM1} and param2 = ${PARAM2}"
}
echo "I am the body of server and I shouldn't be executed"
client.sh
#!/bin/bash
. ./server.sh
ugly_function()
{
beautiful_function $1 "hardcoded things"
}
ugly_function "hey, hi, I'm matteo"
However, when I run client.sh
, I get first the echo "I am the body of server and I shouldn't be executed"
and then I get correctly the function beautiful_function
to be executed as I wish.
I would like to get rid of the run of the body of server.sh
, as if the beautiful_function
was a static method that I could invoke from outside.
Is this possible in Bash? How?
P.s. I have also thought of creating a my_functions.sh
script with no body, that would be used by both server.sh
and client.sh
so I wouldn't have the issue of executing the body at all.
However, I would like to know if there's another way to avoid making a third script just to provide the functions. But if the right answer is "you should indeed extract all your functions in another script with no body, that's the proper way of doing this in Bash", that would still be a valid answer - I'm a beginner in Bash, so pretty open to learn :)
Pre-answering the possible question:
Have you already tried to search? Yes, I did :) All I found (like this or this) will make me call the body of
server.sh
, which is in fact what I want to avoid.