0

I have two bash files, in different folders. I want to call abc() method from bash file a from another bash file b. For that I can do an import. But what if this code is being used in production? I cannot simply provide my locally created files.

Example:

  • main bash file that I am running "third"

    #!/bin/bash
    .  /Users/r/r1-tests/r/fourth
    
    
    abc
    
  • bash file called "fourth" which has method declared

    #!/bin/bash
    
    function abc()
    {
        echo "hello"
    }
    

Can I do "without" using Import in "third"?

Amessihel
  • 5,891
  • 3
  • 16
  • 40
  • Hello, do you mean you want to call `abc()` from `third` without including `fourth`? – Amessihel Feb 26 '21 at 00:04
  • I mean this is going to be checked in Git. I can do in my local with Import. But how can I take care if this is a remote location? – Java_stack1 Feb 26 '21 at 00:06
  • Do you mean you cannot modify `third` file? – Amessihel Feb 26 '21 at 00:08
  • 2
    How do you _want_ it to work without the file with the code you want to execute being local? Give us a concrete idea of how you want it to find and load the code so we can tell you how to make it do that; right now, it's not clear what you want -- instead, you've only described what you _don't_ want. – Charles Duffy Feb 26 '21 at 00:09
  • Yes,I mean I cannot simply add the path of my locally created file "fourth" in "third". This is perhaps an issue I will encounter if my code is pushed to Git, where the code is expecting a file that has been taken from Git location and not from my local file. Do I make sense? In other words, how can I modify this file if I check in my code in Git? – Java_stack1 Feb 26 '21 at 00:12
  • Are _both_ files checked into the same git repo, so you want to search for one file in a path relative to the other one? Or do you want your script to go out over the network to the git repository to find the other file? Or is it something else you're asking for? – Charles Duffy Feb 26 '21 at 00:14
  • If it's just about searching relative to your current source file, that's a duplicate of existing questions already in the knowledge base. – Charles Duffy Feb 26 '21 at 00:15
  • The first part is right. Both files are checked in git repo. So, If that is the case, I cannot simply mention my local path on one file, right? This code will be used by other developers. – Java_stack1 Feb 26 '21 at 00:16
  • Please guide me to the right link. – Java_stack1 Feb 26 '21 at 00:17
  • https://mywiki.wooledge.org/BashFAQ/028 – Charles Duffy Feb 26 '21 at 00:18
  • https://stackoverflow.com/questions/59895/how-can-i-get-the-source-directory-of-a-bash-script-from-within-the-script-itsel/57666344#57666344 – Charles Duffy Feb 26 '21 at 00:18
  • https://stackoverflow.com/questions/43902054/how-can-i-make-source-location-independent-within-my-applications-directory-tre – Charles Duffy Feb 26 '21 at 00:19

1 Answers1

0

But what if this code is being used in production?

Then include it too.

Your problems seems no to be including files, but including files not using absolute paths. So include it using a relative path. Relative to.. for example the current file:

. "${BASH_SOURCE%/*}"/dir/with/fourth

Or relative to the git repository:

. "$(git rev-parse --show-toplevel)"/some/dir/dir/with/fourth
wjandrea
  • 28,235
  • 9
  • 60
  • 81
KamilCuk
  • 120,984
  • 8
  • 59
  • 111