0

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.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89
  • This might help: [Importing functions from a shell script](https://stackoverflow.com/q/12815774/3776858) – Cyrus Mar 26 '19 at 10:21
  • 2
    Unfortunately there is no support for module like imports like `python` does. The `bash` interpreter run top-down, meaning though the `beautiful_function` is sourced in `client.sh`, you have no way to prevent the `echo` statement on the file from being executed with your existing approach. Ways to implement this would involve some kind of hack around the existing features only. – Inian Mar 26 '19 at 10:26
  • 2
    You should put the functions you are interested in to an external "library" file. Then source that file from client and server. – Poshi Mar 26 '19 at 10:35
  • @Cyrus, thanks, the question you reported is pretty much what I'm asking here. I've voted to close my own question as duplicate - could have deleted, but I didn't find that question when searching so letting it to be able to search by other keywords, but still redirecting to the original question. – Matteo NNZ Mar 26 '19 at 10:47
  • @Poshi thanks, that's what I thought too. Will do that most probably. – Matteo NNZ Mar 26 '19 at 10:48
  • @Inian thanks for the explanation :) – Matteo NNZ Mar 26 '19 at 10:48

0 Answers0