Bash - Remote Library

2019-09-07 02:32发布

问题:

We're two student working on a Bash Tools Box, and we come across a problem :

Our local script use functions, stocked in two local libraries.

Our script uses functions inside the first library, and this last uses functions inside the second one library.

Script <-- Library1 <-- Library2

We can't fusion the two libraries in one.

We actually use this command to run our script on a remote computer :

ssh login@remoteIP bash < ~/script.sh

The question is : How to set in the script the library location

回答1:

You need to use the . command in order to include external libraries. Yes, the command is simply called . - a literal dot. In library1.sh add at the top:

. /path/to/library2.sh

In script.sh add at the top:

. /path/to/library1.sh

I would recommend to use absolute paths since relative paths. If you use relative paths, you need to make sure that they are relative to one of your $PATH entries otherwise they would be relative to the current folder where script.sh gets executed.


Btw, there is also the source command which is doing excatly the same as the dot command. Both of them are bash builtins.

Type help . or help source to get help.



回答2:

Embed your Library# code in your script using bashpp.

Replace your . calls with #include and then run it through bashpp:

bashpp ~/script.sh | ssh login@remoteIP bash -s