LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Unable to run another command from within shell script, when run from home directory (https://www.linuxquestions.org/questions/linux-newbie-8/unable-to-run-another-command-from-within-shell-script-when-run-from-home-directory-4175467972/)

blackray1 07-01-2013 03:52 AM

Unable to run another command from within shell script, when run from home directory
 
Hi All,

I am tryin to install an HP tool, HP OV from a script, to automate installation.

I am using the below command in shell script called ov_exec.sh

"source /tmp/LIN/oainstall.sh -i -a -precheckonly"

The install package is located in /tmp/LIN/oainstall.sh
oainstall.sh is the main script which calls upon subscripts based on OS and architechture.

The issue I am facing is one of the subscript does not execute as it is not found, if oainstall.sh is executed form another script, located in directory other than where the oainstall.sh package is located.

I tried giving absolute path to oainstall.sh in my script, still the subscript cannet be called upon, when this script is run from my home directory.
But when the script is run from /tmp/LIN where oainstall packages are located, it executes fine.

jpollard 07-01-2013 05:19 AM

Very likely the script uses relative paths to files it requires.

It would help to know what the error message is.

eahmedshendy 07-01-2013 06:42 AM

Hi blackray1,

Try replacing this line

Code:

source /tmp/LIN/oainstall.sh -i -a -precheckonly
with

Code:

oldPath=$PWD
linPath="/tmp/LIN/"
cd $linPath
source oainstall.sh -i -a -precheckonly
// Also execute whatever you want to do and related to this directory
cd $oldPath
// Then continue your progress


eahmedshendy 07-01-2013 06:47 AM

Code:

oldPath=$PWD  Here you save your current directory address in a variable
linPath="/tmp/LIN/"
cd $linPath  Change current working directory to /tmp/LIN/
source oainstall.sh -i -a -precheckonly  Now read/execute your script safely 
cd $oldPath  Return back to your current working directory


jpollard 07-01-2013 09:23 AM

You wouldn't us a variable to hold the cd command though. Drop the "currentPath=" part (along with the quotes).

One problem using "source" is that it executes in the same context as the current shell, so you have to know what variables that script defines - because they will remain after the script is completed. That is the advantage of doing:

Code:

...
(cd /tmp/LIN/; oainstall.sh -i -a -precheckonly
...

This way a new subprocess is spawned with its own "working directory".

eahmedshendy 07-01-2013 09:37 AM

Quote:

Originally Posted by jpollard (Post 4981737)
You wouldn't us a variable to hold the cd command though. Drop the "currentPath=" part (along with the quotes).

Code:

...
(cd /tmp/LIN/; oainstall.sh -i -a -precheckonly
...


Thank you for your advice.
I usually store all frequently used Linux command in variables.

jpollard 07-01-2013 09:39 AM

Storing them there is fine. Its just that they don't get executed.

eahmedshendy 07-01-2013 09:42 AM

Quote:

Originally Posted by jpollard (Post 4981749)
Storing them there is fine. Its just that they don't get executed.

OH hhhh, I'm sorry
I wrote the code wrong

currentPath="cd /tmp/LIN/"
$currentPath

This will execute it, sorry :)


All times are GMT -5. The time now is 11:32 PM.