aboutsummaryrefslogtreecommitdiffstats
path: root/ves-list.sh
blob: 61e6bcace6b09e87c134543b176998a0627acb95 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/sh
#
# ves_list: list the entries of a path-like (:-delimited) variable from the
# live shell, one per line, in order of precedence. With --index, each entry
# is prefixed by its numeric (0-based) index.
#
# ves_envs: list all created sh-ves environments.
#
ves_list() {
    index=0
    case $1 in
        --index)
            index=1
            shift
            ;;
    esac

    if [ "$#" -lt 1 ]; then
        printf "ERROR: No variable specified. usage: ves list [--index] <variable>\n" >&2
        return 2
    fi

    if ! _shves_check_var_name "$1"; then
        return 1
    fi

    # Read the value of the variable named in $1 from the live shell.
    # For $1 = PATH, this expands to: var_value=${PATH}
    eval "var_value=\${$1}"

    if [ -z "$var_value" ]; then
        return 0
    fi

    i=0
    _shves_split_begin
    for entry in $var_value; do
        if [ "$index" -eq 1 ]; then
            printf "%d %s\n" "$i" "$entry"
        else
            printf "%s\n" "$entry"
        fi
        i=$((i + 1))
    done
    _shves_split_end

    return 0
}

ves_envs() {
    for f in "$SHVES_ENV_DIR"/*; do
        [ -f "$f" ] || continue
        basename "$f"
    done

    return 0
}