0

I've written a script file in which, one function searches for another script and if found, executes that script.

Script extract

#!/bin/bash
...
service_status() {
local my_script=`which my-services-check.sh 2>/dev/null`
[[ -z "$my_script" ]] && { echo -n "functionality not available" ; failure ; echo ; return ; }
source $my_script    
}
...

When I do which my-services-check.sh 2>/dev/null from the terminal, it returns the correct path to that file.

When I run that function service_status() (when calling the script), it doesn't find the file ($my_script is empty).

Instead of using which I've tried with type and command but I end up with the same result.

Path issue

Then I printed out $PATH from the terminal and from the script, and sure enough they aren't the same! When executed from my script, the $PATH is set to be the secure PATH, as defined in /etc/sudoers:

Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin

From the terminal

echo $PATH /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin: ...

So,

  1. how can I reliably find the file I'm looking for, from my script? (without using find as the file I need must be in the PATH)?

  2. and how come the PATH is set differently when executed from a script?

  • @slm, any idea on this? – fduff Jan 30 '15 at 14:12
0

The interactive shell loads .bashrc, the script doesn't. If your $PATH is set in .bashrc and you don't export it, the script doesn't inherit your modifications to $PATH. If you call export PATH, the script should see it. Check export -p to see what variables get exported ($PATH should be among them). Also check $BASH_ENV which could potentially override your $PATH.

Ideally, the $PATH should be set and exported in your .profile, so that it gets loaded and set when you login, and is visible to everything you run from then on forward.

It's a good practice to check not only if the filename exists, but if it is a proper file, [[ -f "$my_script" ]] or (in case you wanted to call it as a script instead of sourcing it), the switch -x.

  • "If your $PATH is set in .bashrc and you don't export it, the script doesn't inherit your modifications to $PATH." That is wrong. For the simple reason that PATH is (under normal conditions) in every process's environment. Writing to an existing environment variable does modify it for all child processes. You don't have to export it as it is already exported. The problem is that PATH is overwritten during script shell initialization. – Hauke Laging Jan 29 '15 at 13:42
  • Well that's why I'm saying to check if it is exported - it should be, but you never know. A noninteractive shell shouldn't have any additional initialization that would override the $PATH. Unless $BASH_ENV is set to something. That's also something to check. – orion Jan 29 '15 at 13:47
  • @orion, export -p report the PATH being exported with all relevant paths as when run from the terminal. – fduff Jan 29 '15 at 13:48
0

There isn't really any such thing as a guaranteed set of directories to include in PATH. You'll find /bin but everything else is just probable or possible. Users on my systems will have something like $HOME/bin:/usr/local/bin:/bin:/usr/bin. Root users would have /usr/local/sbin:/sbin:/usr/sbin in there too.

If your file might be in one of a number of places you could search for it in those possible places. Just remove the entire dependency on PATH out of this search, or extend PATH to include the minimum set of places your script might be found.

local OPATH="$PATH"
PATH="$PATH:/extra/place:/another/extra/place"
local my_script=$( which my-services-check.sh 2>/dev/null )
PATH="$OPATH" # Restore the original PATH (optional)

if test -z "$my_script"; then ...; fi
. "$my_script"

Alternatively just set PATH to the required value at the top of the script. There's no reason, once your initial script is running, why you can't set PATH. I tend to do this in many of my scripts simply so I know the environment is plausible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.