summaryrefslogtreecommitdiff
path: root/inpath
blob: f625b173accf987ed02e2f6fdd186a7637fe30cf (plain)
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
#!/bin/sh

in_path () {
    local cmd=$1 path=$2 res=1
    local IFS=":"

    for dir in $path;
    do
        if [ -x "$dir/$cmd" ]; then
            res=0
            break
        fi
    done

    return $res
}

cmd_in_path () {
    var=$1

    if [ -n "$var" ]; then
        if [ $(echo "$var" | cut -c 1) = "/" ]; then
            if [ ! -x $var ]; then
                return 1
            fi
        elif ! in_path $var "$PATH"; then
            return 2
        fi
    fi
}

if [ $# -ne 1 ]; then
    echo "Usage: $0 <command>" >&2
    exit 1
fi

cmd_in_path "$1"
case $? in
    0 ) echo "$1 found in PATH"               ;;
    1 ) echo "$1 not found or not executable" ;;
    2 ) echo "$1 not found in PATH"           ;;
esac

exit 0