Bash - Remote Library

白昼怎懂夜的黑 提交于 2019-12-25 03:24:20

问题


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


来源:https://stackoverflow.com/questions/28717672/bash-remote-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!