0

How can I find (if possible) the location of the script that is being executed?

So I have a script that I'll use to do some magic, and I'll need to call it from random locations on a system (this system can be mine, my servers, my friend's PC, my mom's PC, etc).

Now, I need to execute some binaries present along with the script, How can I do that?

Say my script folder has 3 files:

  • binary1
  • myScript.sh
  • binary2

Now, this whole folder can be located in "/tmp/scripts", on one system, and in "/home/user/Downloads" on other system(random locations).
And I'll need to run this script from, lets say "/home/user/Desktop". So my question is, how can I execute the binary1 and binary2 from my script without knowing their actual path before hand?

agc
  • 7,002
  • 2
  • 25
  • 44
Sahil Soni
  • 41
  • 6
  • 1
    See: [Getting the source directory of a Bash script from within](https://stackoverflow.com/q/59895/3776858) – Cyrus Sep 30 '18 at 15:00

2 Answers2

0

If foo is an alias of the script, try:

n="$(type -f  foo)" ; n="${n/*\`}"; n="${n%?}"
dirname "$(realpath "$(which "$n")")"
agc
  • 7,002
  • 2
  • 25
  • 44
  • is there any way if I don't know the script file name? – Sahil Soni Sep 30 '18 at 15:54
  • @SahilSoni, Oh... it seemed as though the script name was known. There's usually a way, but we need to know more. This script, is it a desktop icon, a background script, or what? Do *you* run it, or click on it, or does it run automatically? – agc Sep 30 '18 at 16:13
  • Basically, I'm writing this script, it will run from terminal, and will quickly(or in a little while) finish up and exit. I cant know the file name because it can be changed. the user will run this script by "bash path/to/script" – Sahil Soni Sep 30 '18 at 18:14
  • @SahilSoni, This might be a bit of a side issue, but it's still unclear as to *why* you'd expect that script's file name would be changed by the user. Sure users can do that, but it's *unusual*. – agc Sep 30 '18 at 19:51
  • basically, this script will be generated by a program, so we can't quite have a static name, as the program that'll generate this, might run multiple times(after updates) and i'd like to keep copies of all the existing files(for debugging purposes), then the program will create alias for running my script which the user can use. It may seem weird that in last comment, I said user will run the script directly, but now I'm, saying something else, thats because all this is in development, so I'm not sure how the final result will be. – Sahil Soni Oct 02 '18 at 13:49
  • @SahilSoni, So another program generates a non-static name, as well as an alias. Does the alias have a static name? – agc Oct 02 '18 at 13:54
  • Yes, that alias name is static – Sahil Soni Oct 05 '18 at 06:03
0

The only executables that you can execute are either present in your PATH variable or initialization files (like aliases and functions).

So, couple of options. Try to determine the location where your scripts are and at the beginning of your script, add the path like.

# Your script beginning
PATH="$PATH:/<location>"

Other option, create a folder like say $HOME/bin and copy your script in this location on all your machines or better yet you can add the scripts /usr/bin or /usr/local* folder in all machines

apatniv
  • 1,393
  • 7
  • 11