Project

General

Profile

1 183 aaronmk
#!/bin/bash
2 131 aaronmk
# Sets a password environment variable
3
4 291 aaronmk
self="$(readlink -f -- "$BASH_SOURCE")"
5
6 1093 aaronmk
set -o pipefail
7
8 1091 aaronmk
test -t 2 && ccTty= || ccTty=1 # cc the tty if stderr is a log file
9
{
10 1090 aaronmk
store=
11
test _"$1" = _- && { store=1; shift;}
12 131 aaronmk
13 1090 aaronmk
if ! test "$#" -ge 1; then
14 1091 aaronmk
    echo "Usage: . $self env_var [desc] (note initial \".\")"|fold -s
15 1090 aaronmk
    exit 2
16
fi
17 251 aaronmk
18 1090 aaronmk
desc="$2"
19
test -n "$desc" || desc="the $1 password"
20
21 290 aaronmk
function trace()
22
{
23 1091 aaronmk
    { for arg in "$@"; do printf "%q " "$arg"; done; echo;}
24 290 aaronmk
}
25
26 1090 aaronmk
if test -n "$store" -o -z "${!1+t}"; then # env var with name $1 is unset
27
    if test -n "$store"; then
28 1091 aaronmk
        echo " To change the saved value for $desc, run: "
29 1090 aaronmk
    else
30 1091 aaronmk
        echo " You must first store $desc. At the shell, run: "
31 292 aaronmk
    fi
32 293 aaronmk
    trace . "$self" - "$@"
33
34 1090 aaronmk
    test -z "$store" && exit 1 # just direct user how to store password
35 1091 aaronmk
    read -s -p "Enter $desc: "; echo
36 131 aaronmk
    export "$1"="$REPLY"
37
fi
38 1093 aaronmk
} 2>&1|tee ${ccTty:+/dev/tty} >&2 || exit