2

I have created a script which runs a certain function. After running that certain function, it calls another script which runs another function. It's working perfectly. HOWEVER, I have called the second script with this line of code:

( exec "/home/sh0/folder/folder2/folder3/secondScript" )

What if I were to run my script on a different machine with totally different file paths? Is there a way to call a script on any machine without worrying about the file path?

Also, all my scripts are located in the same directory which would be 'folder3'

Thanks in advance!

1
  • Do you really mean totally different, i.e. that everything but folder3/secondScript could be different on different machines? Or do you instead mean that your user's home (perhaps /home/sh0/folder) directory is different on every machine, but the relative path remains the same? Commented Aug 27, 2016 at 23:33

2 Answers 2

3

Ultimately, you have to know where your scripts are installed, and make sure that directory is on $PATH. You can then use secondScript without specifying any path to the script.

But if the locations can vary between machines, you have to decide how to cut out the variance. One common option is to keep the script(s) in a known location such as $HOME/bin or /usr/local/bin, where you keep that known location on your path — or in /opt/program/bin (using an appropriate name for program based on what you call the system of programs you're using).

Another option is to have an environment variable that specifies where scripts are found (PROGRAMLOCATION=/home/sh0/folder/folder2/folder3 and export PATH=$PATH:$PROGRAMLOCATION). You then have to make sure the environment variables (PROGRAMLOCATION and PATH) are set appropriately — that can be tricky.

If you think the folder name is unique enough, you could (but probably shouldn't) use find to locate the directory. What do you do when you find 6 different directories of the same name? Plus an unconstrained find is slow — especially if you have NFS-mounted file systems.

Sign up to request clarification or add additional context in comments.

Comments

2

You can simply store the path like shown below

#!/bin/bash

MY_PATH="$PWD"
FOLDER3="folder3"

echo "$MY_PATH/$FOLDER3"

Let us know if this what you are looking for or not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.