-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathshell-source
59 lines (52 loc) · 1.37 KB
/
shell-source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/sh -efu
### This file is covered by the GNU General Public License,
### which should be included with libshell as the file LICENSE.
### All copyright information are listed in the COPYING.
if [ -z "${__included_shell_source-}" ]; then
__included_shell_source=1
__shell_source_find()
{
local r f IFS=:
r="$1"; shift
f="$1"; shift
if [ -n "${f##*/*}" ]; then
set -- ${PATH-}
while [ "$#" -gt 0 ]; do
if [ -e "$1/$f" ]; then
f="$1/$f"
break
fi
shift
done
fi
[ -e "$f" ] || return 1
eval "$r=\"\$f\""
}
### Execute commands from file in the current environment if file exists.
### Usage: source_if_exists /file arg1 arg2
### or: source_if_exists file arg1 arg2
source_if_exists()
{
local v f
f="$1"; shift
! __shell_source_find v "$f" || [ ! -f "$v" ] || . "$v"
}
### Execute commands from file in the current environment if file is not empty.
### Usage: source_if_notempty /file arg1 arg2
### or: source_if_notempty file arg1 arg2
source_if_notempty()
{
local v f
f="$1"; shift
! __shell_source_find v "$f" || [ ! -s "$v" ] || . "$v"
}
### Execute commands from file in the current environment if file is executable.
### Usage: source_if_executable /file arg1 arg2
### or: source_if_executable file arg1 arg2
source_if_executable()
{
local v f
f="$1"; shift
! __shell_source_find v "$f" || [ ! -x "$v" ] || . "$v"
}
fi #__included_shell_source