2

I have an application named puppet installed on my Linux box. It is installed at location /usr/test/bin/puppet

This is how .bash_profile looks

export PATH=/usr/test/bin

if I run command puppet apply from console, it works fine but when I call puppet command from inside bash script, it says command not found

#!/bin/bash
puppet apply x.pp

Any ideas on what is wrong ?

9
  • 3
    This is probably better a question for Unix & Linux Stack Exchange. Commented Jan 8, 2015 at 0:11
  • Are you sure the path is correct, run whereis puppet. Commented Jan 8, 2015 at 0:14
  • On the same console where this script is run, export the path again and check. Perhaps this shell has is in the path but the path was not exported initially. Commented Jan 8, 2015 at 0:16
  • whereis puppet puppet: /etc/puppet Commented Jan 8, 2015 at 0:21
  • @Gary: then you should include /etc in the path... And you better append to the path: ...=$PATH:/etc, so that old path references aren't lost in the process. Commented Jan 8, 2015 at 0:23

3 Answers 3

5

.bash_profile is loaded only if bash is invoked as login shell (bash -l or from a real tty), at least in Debian based distributions bash in a virtual tty (for example when using xterm, gnome-terminal, etc...) is invoked as interactive shell.

Interactive shells loads the configuration from ~/.bashrc.

bash manpage:

~/.bash_profile
   The personal initialization file, executed for login shells
~/.bashrc
   The individual per-interactive-shell startup file

Shellscripts don't load any of these.

You can check which files are opened by any program with strace:

strace ./s.sh 2>&1 | grep -e stat -e open

Possible solutions:

  1. You can export the variable at the beginning of every script:

    #!/bin/bash
    export PATH=$PATH:...
    
  2. Or you can have another file with the desired variables and source it from any script that need those:

    /etc/special_vars.sh:

    export PATH=$PATH:...
    

    script:

    #!/bin/bash
    . /etc/special_vars.sh
    puppet ...
    
  3. Configure the PATH in in ~/.bashrc, ~/.bash_profile and ~/.profile for the user running the script (sub-processes will inherit the environment variables) to have some warranty that the user can run the script from different environments and shells (some bourne compatible shells others than bash do load ~/.profile)

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

6 Comments

Hi, can you please elaborate little more, should I make changes to bashrc ?
i have no experience with strace, can you tell how to use it in context of my problem ?
Thanks Fernando, it worked. If puppet is installed in default /usr/bin directory, and I still have export /usr/test/bin in my script along with /usr/bin. will it try to find the command in both /usr/bin and /usr/test/bin ? At one point in time, either application will be in /usr/bin or in /usr/test/bin. Thanks again
@Gary the best solution for that is installing puppet on /usr/local/bin if you do that won't need to modify the PATH environment variable.
I tested that script on both the systems where puppet is installed in different folders, its working on both the systems. Fingers Crossed, thanks
|
3

Maybe the export of PATH is wrong?

export PATH=$PATH:/usr/test/bin/puppet

1 Comment

No, seeing as puppet is the file that should be found, not a directory in which to look for binaries.
1

You could try using an alias, like so

in your .bash_profile:

alias puppet='bash puppet.fileextension'

you can also do

alias puppet='bash path/to/puppet.fileextension'

which will let you run the script from anywhere in Terminal.

EDIT:

OP has stated in the comments that there will be two different systems running, and he asked how to check the file path to the bash file.

If you do

#!/bin/bash
runPuppet(){
 if [ -e path/to/system1/puppet.fileextension]
 then
 bash path/to/system1/puppet.fileextension $1 $2
 elif [ -e path/to/system2/puppet.fileextension]
 then
 bash path/to/system2/puppet.fileextension $1 $2
 fi
}
runPuppet apply x.pp

and change the runPuppet input to whatever you'd like.

To clarify/explain:

-e is to check if the file exists

$1 & $2 are the first two input parameters, respectively.

4 Comments

Quill:wq, Thanks for the reply. There are two systems on which people can run this script. On one type of system, application is installed at /usr/bin and puppet command is identified - Everything is smooth BUT if people run this script on second system where application is installed on /usr/test/bin, then we see this problem, that's why i haven't mentioned any path inside the script because I do not know that on which system they will run this script. Any idea ?
What kind of systems are we talking, Operating Systems, or just general user preference?
just 1 parameter, apply is not a parameter, that's a part of puppet command, patameter is just one x.pp, this sounds good, thanks Quill
No worries, but the parameters are for what you do with puppet, if you're only going to ever call apply then fine, but otherwise it's basically like replacing $1 $2 with what you put after puppet.

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.