jeudi 29 janvier 2015

Locate specific file from script


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?





Aucun commentaire:

Enregistrer un commentaire